1

I am new to LLVM. I built LLVM and Clang on windows by GnuStep yesterday.

LLVM+CLang:3.2
GCC:4.6.1(GnuStep)
OS:Win7 64

I can compile Objective-c source file to both bitcode and exe. The exe works, but when I tried to execute the bitcode, I got this error:

LLVM ERROR: Could not resolve external global address: 
    _OBJC_CLASS_NSConstantString

Questions:

How can I load dll or lib files in llvm? 
How can I link lib files(ex: libobjc.dll.a) to bitcode? Is that possible?

hello.m

#import <Foundation/Foundation.h>
int main(int argc, char**argv)
{
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    NSLog(@"Hello Objective-C\n");
    [pool release];
    return 0;
}

Makefile

CC=gcc
CCFLAGS=-fconstant-string-class=NSConstantString -ID:/GNUstep/GNUstep/System/Library/Headers
LDFLAGS=-LD:/GNUstep/GNUstep/System/Library/Libraries/ -lobjc -lgnustep-base

CLANG=clang
CLANG_FLAG=-c -fobjc-runtime=gcc -emit-llvm

LLC=llc
LLI=lli
LLI_FLAG=-load=D:\GNUstep\GNUstep\System\Tools\objc-4.dll -load=D:\GNUstep\GNUstep\System\Tools\gnustep-base-1_24.dll
#LLI_FLAG=-load=D:\GNUstep\GNUstep\System\Library\Libraries\libgnustep-base.dll.a -load=D:\GNUstep\GNUstep\System\Library\Libraries\libobjc.dll.a

all:hello.obj hello1.exe

hello.exe: hello.o
     $(CC) -o hello.exe hello.o $(LDFLAGS)

hello.obj: hello.bc
    $(LLC) -filetype=obj hello.bc

hello.bc:hello.m
    $(CLANG) -o hello.bc hello.m $(CLANG_FLAG) $(CCFLAGS)

hello1.exe: hello.m
    $(CLANG) hello.m -o hello1.exe $(CCFLAGS) $(LDFLAGS)

run:
#Err
    $(LLI)  $(LLI_FLAG) -force-interpreter=false hello.bc
#OK
    hello.exe
#OK
    hello1.exe

clean:
    rm *.o
    rm *.exe
    rm *.bc
neohope
  • 1,822
  • 15
  • 29
  • I don't know if this is helping. On my GNU/Linux it works with -fobjc-runtime=gnustep. I have gnustep-base built with --enable-objc-nonfragile-abi. – Fred Frith-MacDonald Dec 28 '12 at 14:17
  • Thanks Fred, I will try it latter – neohope Dec 29 '12 at 05:39
  • It seems that, the simbol of NSConstantString in test.bc is "_OBJC_CLASS_NSConstantString" while the simbol in gnustep-base-1_24.dll is "__objc_class_name_NSConstantString". llvm cannot find the right simbol. – neohope Dec 31 '12 at 10:51
  • Mine have both; nm /usr/local/lib/libgnustep-base.so|grep NSConstantString|grep -i objc_class ==> 007019b0 D _OBJC_CLASS_NSConstantString 00773f84 B __objc_class_name_NSConstantString 006f0fb0 V __objc_class_ref_NSConstantString – Fred Frith-MacDonald Dec 31 '12 at 12:02

1 Answers1

0

You likely need at least one hard, static reference to a symbol in each DLL. In your main(), try adding [NSConstantString class]; (add a declaration, if needed).

The windows dynamic linker is quite proactive about not loading DLLs without a hard symbol reference. This conflicts with Objective-C's dynamic lookup.

(This is a guess based on experience. Been a decade since I've done ObjC on Windows, but seemingly little has changed.)

arrowd
  • 33,231
  • 8
  • 79
  • 110
bbum
  • 162,346
  • 23
  • 271
  • 359
  • Thanks bbum, I will try it latter – neohope Dec 29 '12 at 05:38
  • NSConstantString is a weak ref. Donot know how to deal with it. Maybe I have to build gnustep on windows.[366](sec 0)(fl 0x00)(ty 0)(scl 105) (nx 1) 0x00000000 __OBJC_CLASS_NSConstantString AUX lnno 2 size 0x0 tagndx 368 [368](sec -1)(fl 0x00)(ty 0)(scl 2) (nx 0) 0x00000000 .weak.__OBJC_CLASS_NSConstantString.default – neohope Dec 31 '12 at 12:34
  • OK -- sounds like the linker or compiler is generating code that still weakly references classes. Find an exported function symbol from the library and set up some code that calls it (doesn't have to actually be executed, but make sure the optimizer doesn't eliminate it). BTW: We all had to play these kinds of games when developing WebObjects apps on Windows NT. – bbum Jan 01 '13 at 18:14