linker
Folders and files
Name | Name | Last commit date | ||
---|---|---|---|---|
parent directory.. | ||||
# Using ld directly is not trivial. $ cc -o a.o -c a.c $ cc -o b.o -c b.c $ ls a.c a.o b.c b.o $ ld ld: no input files $ ld a.o b.o ld: warning: cannot find entry symbol _start; defaulting to 0000000000401000 ld: a.o: in function `main': a.c:(.text+0x19): undefined reference to `printf' $ ld -lc a.o b.o ld: warning: cannot find entry symbol _start; defaulting to 0000000000401020 $ echo $? 0 $ ./a.out -bash: ./a.out: No such file or directory $ ls a.c a.o a.out* b.c b.o # The compiler can do it for you though. $ cc a.c b.c $ ./a.out 13 # Building a library. $ cc -shared -o libmy.so libmy.c $ ls a.c b.c main.c libmy.c libmy.so* README # Using the library. $ cc -lmy main.c /usr/lib/gcc/x86_64-pc-linux-gnu/8.3.0/../../../../x86_64-pc-linux-gnu/bin/ld: cannot find -lmy collect2: error: ld returned 1 exit status $ cc -lmy -L. main.c $ ./a.out ./a.out: error while loading shared libraries: libmy.so: cannot open shared object file: No such file or directory # The library name is in the executable but with no additional information on # where to find it (so only system directories are searched). $ ldd a.out linux-vdso.so.1 (0x00007fff61188000) libmy.so => not found libc.so.6 => /lib64/libc.so.6 (0x00007fc39ea3b000) /lib64/ld-linux-x86-64.so.2 (0x00007fc39ecc9000) # You can provide information through environment variables (that is dynamic # linker). $ LD_LIBRARY_PATH=. ./a.out called myfn $ cc -lmy -L. -Xlinker -R . main.c $ ./a.out called myfn $ readelf -d a.out Dynamic section at offset 0x2dd8 contains 28 entries: Tag Type Name/Value 0x0000000000000001 (NEEDED) Shared library: [libmy.so] 0x0000000000000001 (NEEDED) Shared library: [libc.so.6] # this followin one is from those "-Xlinker -R." options 0x000000000000001d (RUNPATH) Library runpath: [.] 0x000000000000000c (INIT) 0x1000 0x000000000000000d (FINI) 0x1220 0x0000000000000019 (INIT_ARRAY) 0x3dc8 0x000000000000001b (INIT_ARRAYSZ) 8 (bytes) 0x000000000000001a (FINI_ARRAY) 0x3dd0 0x000000000000001c (FINI_ARRAYSZ) 8 (bytes) 0x000000006ffffef5 (GNU_HASH) 0x2e8 0x0000000000000005 (STRTAB) 0x3d0 0x0000000000000006 (SYMTAB) 0x310 0x000000000000000a (STRSZ) 168 (bytes) 0x000000000000000b (SYMENT) 24 (bytes) 0x0000000000000015 (DEBUG) 0x0 0x0000000000000003 (PLTGOT) 0x4000 0x0000000000000002 (PLTRELSZ) 48 (bytes) 0x0000000000000014 (PLTREL) RELA 0x0000000000000017 (JMPREL) 0x578 0x0000000000000007 (RELA) 0x4b8 0x0000000000000008 (RELASZ) 192 (bytes) 0x0000000000000009 (RELAENT) 24 (bytes) 0x000000006ffffffb (FLAGS_1) Flags: PIE 0x000000006ffffffe (VERNEED) 0x488 0x000000006fffffff (VERNEEDNUM) 1 0x000000006ffffff0 (VERSYM) 0x478 0x000000006ffffff9 (RELACOUNT) 3 0x0000000000000000 (NULL) 0x0 $ cd / $ ~/unix-linux-prog-in-c-src/linker/a.out /afs/ms/u/p/pechanec/unix-linux-prog-in-c-src/linker/a.out: error while loading shared libraries: libmy.so: cannot open shared object file: No such file or directory