Bug 11396 - objcopy adds IMAGE_FILE_RELOCS_STRIPPED for PIE
Summary: objcopy adds IMAGE_FILE_RELOCS_STRIPPED for PIE
Status: RESOLVED FIXED
Alias: None
Product: binutils
Classification: Unclassified
Component: binutils (show other bugs)
Version: 2.21
: P2 normal
Target Milestone: ---
Assignee: unassigned
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2010-03-17 22:31 UTC by H.J. Lu
Modified: 2010-03-18 12:28 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description H.J. Lu 2010-03-17 22:31:26 UTC
According to PE-COFF spec, IMAGE_FILE_RELOCS_STRIPPED means

--
Image only, Windows CE, and Microsoft Windows NT® and later.
This indicates that the file does not contain base relocations
and must therefore be loaded at its preferred base address.
If the base address is not available, the loader reports an
error. The default behavior of the linker is to strip base
relocations from executable (EXE) files.
--

An empty .reloc section is used to create an PIE file:

[hjl@gnu-6 pr959]$ cat foo.s
        .text
        .globl _start
_start:
        .section .reloc
[hjl@gnu-6 pr959]$ make
./as   -o foo.o foo.s
./ld -pie -o foo.exe foo.o
./objdump -f foo.exe

foo.exe:     file format pei-x86-64
architecture: i386:x86-64, flags 0x00000133:
HAS_RELOC, EXEC_P, HAS_SYMS, HAS_LOCALS, D_PAGED
start address 0x0000000000401000

But objcopy adds IMAGE_FILE_RELOCS_STRIPPED even if
it doesn't strip any relocations:

./objcopy foo.exe bar.exe
./objdump -f bar.exe

bar.exe:     file format pei-x86-64
architecture: i386:x86-64, flags 0x00000132:
EXEC_P, HAS_SYMS, HAS_LOCALS, D_PAGED
start address 0x0000000000401000

[hjl@gnu-6 pr959]$
Comment 1 H.J. Lu 2010-03-17 22:34:53 UTC
It has nothing to do with the empty .reloc section. It is because -pie.
Comment 2 H.J. Lu 2010-03-17 23:23:25 UTC
A patch is posted at

http://sourceware.org/ml/binutils/2010-03/msg00237.html
Comment 3 Sourceware Commits 2010-03-18 12:27:35 UTC
Subject: Bug 11396

CVSROOT:	/cvs/src
Module name:	src
Changes by:	hjl@sourceware.org	2010-03-18 12:27:15

Modified files:
	bfd            : ChangeLog libcoff-in.h libcoff.h peXXigen.c 

Log message:
	Don't add IMAGE_FILE_RELOCS_STRIPPED for PIE.
	
	2010-03-18  H.J. Lu  <hongjiu.lu@intel.com>
	
	PR binutils/11396
	* libcoff-in.h (pe_tdata): Add dont_strip_reloc.
	* libcoff.h: Regenerated.
	
	* peXXigen.c (_bfd_XXi_only_swap_filehdr_out): Clear F_RELFLG
	if dont_strip_reloc is set.
	(_bfd_XX_bfd_copy_private_bfd_data_common): Set
	dont_strip_reloc on output if there is no .reloc and
	IMAGE_FILE_RELOCS_STRIPPED isn't set in input.

Patches:
http://sourceware.org/cgi-bin/cvsweb.cgi/src/bfd/ChangeLog.diff?cvsroot=src&r1=1.4959&r2=1.4960
http://sourceware.org/cgi-bin/cvsweb.cgi/src/bfd/libcoff-in.h.diff?cvsroot=src&r1=1.40&r2=1.41
http://sourceware.org/cgi-bin/cvsweb.cgi/src/bfd/libcoff.h.diff?cvsroot=src&r1=1.57&r2=1.58
http://sourceware.org/cgi-bin/cvsweb.cgi/src/bfd/peXXigen.c.diff?cvsroot=src&r1=1.61&r2=1.62

Comment 4 H.J. Lu 2010-03-18 12:28:07 UTC
Fixed.