ld -r

Marco Atzeri marco.atzeri@gmail.com
Sun May 10 10:55:06 GMT 2020


Am 10.05.2020 um 11:00 schrieb Paul Edwards via Binutils:
> Why does adding "-r" to "ld" allow the link
> to complete without error, even when there
> are undefined symbols? Is there an option I
> can use to get the link to fail as expected?
> Note that I am using "-r" to produce
> relocatable executables. See below for example.
> 
> Thanks. Paul.
> 
> 
> 
> C:\scratch\xxx3>type main.c
> int main(void)
> {
>     foo();
> }
> C:\scratch\xxx3>gcc -c main.c
> 
> C:\scratch\xxx3>ld -s main.o
> main.o:main.c:(.text+0x7): undefined reference to `__main'
> main.o:main.c:(.text+0xc): undefined reference to `foo'
> ld: main.o: bad reloc address 0x20 in section `.eh_frame'
> ld: final link failed: Invalid operation
> 
> C:\scratch\xxx3>ld -r -s main.o
> 
> C:\scratch\xxx3>ld -s main.o
> main.o:main.c:(.text+0x7): undefined reference to `__main'
> main.o:main.c:(.text+0xc): undefined reference to `foo'
> ld: main.o: bad reloc address 0x20 in section `.eh_frame'
> ld: final link failed: Invalid operation
> 
> C:\scratch\xxx3>ld --version
> GNU ld (GNU Binutils) 2.23.52.20130309
> Copyright 2013 Free Software Foundation, Inc.
> This program is free software; you may redistribute it under the terms of
> the GNU General Public License version 3 or (at your option) a later 
> version.
> This program has absolutely no warranty.
> 

the output with -r is NOT an executable file,
it is just a different type of object file
and as you have not specified an output name with -o
is using the default "a"

$ file main.o a.exe
main.o: data
a.exe:  data

$ objdump -x a.exe > a.txt

$ objdump -x main.o > main.txt

$ diff -uN main.txt a.txt
--- main.txt    2020-05-10 12:44:48.160274200 +0200
+++ a.txt       2020-05-10 12:44:59.089816200 +0200
@@ -1,6 +1,6 @@

-main.o:     file format pe-x86-64
-main.o
+a.exe:     file format pe-x86-64
+a.exe
  architecture: i386:x86-64, flags 0x00000039:
  HAS_RELOC, HAS_DEBUG, HAS_SYMS, HAS_LOCALS
  start address 0x0000000000000000
@@ -67,20 +67,22 @@

  Sections:
  Idx Name          Size      VMA               LMA               File 
off  Algn
-  0 .text         00000020  0000000000000000  0000000000000000 
00000104  2**4
+  0 .text         00000020  0000000000000000  0000000000000000 
0000012c  2**4
                    CONTENTS, ALLOC, LOAD, RELOC, READONLY, CODE
    1 .data         00000000  0000000000000000  0000000000000000 
00000000  2**4
                    ALLOC, LOAD, DATA
-  2 .bss          00000000  0000000000000000  0000000000000000 
00000000  2**4
-                  ALLOC
-  3 .xdata        0000000c  0000000000000000  0000000000000000 
00000124  2**2
+  2 .rdata        00000000  0000000000000000  0000000000000000 
00000000  2**4
+                  ALLOC, LOAD, READONLY, DATA
+  3 .rdata$zzz    00000020  0000000000000000  0000000000000000 
0000014c  2**4
                    CONTENTS, ALLOC, LOAD, READONLY, DATA
-  4 .pdata        0000000c  0000000000000000  0000000000000000 
00000130  2**2
+  4 .pdata        0000000c  0000000000000000  0000000000000000 
0000016c  2**2
                    CONTENTS, ALLOC, LOAD, RELOC, READONLY, DATA
-  5 .rdata$zzz    00000020  0000000000000000  0000000000000000 
0000013c  2**4
+  5 .xdata        0000000c  0000000000000000  0000000000000000 
00000178  2**2
                    CONTENTS, ALLOC, LOAD, READONLY, DATA
+  6 .bss          00000000  0000000000000000  0000000000000000 
00000000  2**4
+                  ALLOC

  SYMBOL TABLE:
-[  0](sec -2)(fl 0x00)(ty   0)(scl 103) (nx 1) 0x0000000000000000 main.c
+[  0](sec -2)(fl 0x00)(ty   0)(scl 103) (nx 1) 0x0000000000000010 main.c
  File
  [  2](sec  1)(fl 0x00)(ty  20)(scl   2) (nx 1) 0x0000000000000000 main
  AUX tagndx 0 ttlsiz 0x0 lnnos 0 next 0
@@ -88,13 +90,13 @@
  AUX scnlen 0x1d nreloc 2 nlnno 0
  [  6](sec  2)(fl 0x00)(ty   0)(scl   3) (nx 1) 0x0000000000000000 .data
  AUX scnlen 0x0 nreloc 0 nlnno 0
-[  8](sec  3)(fl 0x00)(ty   0)(scl   3) (nx 1) 0x0000000000000000 .bss
+[  8](sec  7)(fl 0x00)(ty   0)(scl   3) (nx 1) 0x0000000000000000 .bss
  AUX scnlen 0x0 nreloc 0 nlnno 0
-[ 10](sec  4)(fl 0x00)(ty   0)(scl   3) (nx 1) 0x0000000000000000 .xdata
+[ 10](sec  6)(fl 0x00)(ty   0)(scl   3) (nx 1) 0x0000000000000000 .xdata
  AUX scnlen 0xc nreloc 0 nlnno 0
  [ 12](sec  5)(fl 0x00)(ty   0)(scl   3) (nx 1) 0x0000000000000000 .pdata
  AUX scnlen 0xc nreloc 3 nlnno 0
-[ 14](sec  6)(fl 0x00)(ty   0)(scl   3) (nx 1) 0x0000000000000000 
.rdata$zzz
+[ 14](sec  4)(fl 0x00)(ty   0)(scl   3) (nx 1) 0x0000000000000000 
.rdata$zzz
  AUX scnlen 0x11 nreloc 0 nlnno 0
  [ 16](sec  0)(fl 0x00)(ty  20)(scl   2) (nx 0) 0x0000000000000000 __main
  [ 17](sec  0)(fl 0x00)(ty  20)(scl   2) (nx 0) 0x0000000000000000 foo


A program has a different footprint

  $ objdump -x /usr/bin/ls.exe

/usr/bin/ls.exe:     file format pei-x86-64
/usr/bin/ls.exe
architecture: i386:x86-64, flags 0x0000010a:
EXEC_P, HAS_DEBUG, D_PAGED
start address 0x0000000100401000

Characteristics 0x2f
         relocations stripped
         executable
         line numbers stripped
         symbols stripped
         large address aware
....


More information about the Binutils mailing list