Bug 3015 - -z relro may create PT_NULL segment
Summary: -z relro may create PT_NULL segment
Status: RESOLVED FIXED
Alias: None
Product: binutils
Classification: Unclassified
Component: ld (show other bugs)
Version: 2.17
: P2 normal
Target Milestone: ---
Assignee: unassigned
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2006-08-09 06:11 UTC by Marcus Meissner
Modified: 2006-09-05 16:27 UTC (History)
3 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 Marcus Meissner 2006-08-09 06:11:45 UTC
I enabled -z relro by default (link_info.relro = TRUE; in ldmain.c) 
 
This causes some of the testcases in ld to fail: 
 
FAIL: strip -z max-page-size=0x200000 (maxpage1) 
FAIL: strip -z max-page-size=0x200000 -z common-page-size=0x100000 (maxpage1) 
FAIL: strip -z max-page-size=0x100000 (maxpage1) 
FAIL: strip -z max-page-size=0x100000 -z common-page-size=0x1000 (maxpage1) 
FAIL: strip  (maxpage1) 
FAIL: objcopy  (maxpage1) 
FAIL: objcopy  (tbss1) 
FAIL: objcopy -shared (tbss1) 
FAIL: objcopy -z max-page-size=0x100000 (tbss1) 
FAIL: objcopy -z max-page-size=0x100000 -z common-page-size=0x1000 (tbss1) 
FAIL: objcopy  (tdata1) 
FAIL: objcopy -shared (tdata1) 
FAIL: objcopy -z max-page-size=0x100000 (tdata1) 
FAIL: objcopy -z max-page-size=0x100000 -z common-page-size=0x1000 (tdata1) 
FAIL: objcopy  (tbss2) 
FAIL: objcopy -z max-page-size=0x100000 (tbss2) 
FAIL: objcopy -z max-page-size=0x100000 -z common-page-size=0x1000 (tbss2) 
FAIL: objcopy -z max-page-size=0x100000 (tdata2) 
FAIL: objcopy  (tdata2) 
FAIL: objcopy -shared (tdata2) 
FAIL: objcopy -z max-page-size=0x100000 -z common-page-size=0x1000 (tdata2) 
 
 
The problem with those testcase it, that ld generates 
a PT_NULL section which is discard by strip and objcopy: 
 
--- tmpdir/maxpage1.exp 2006-08-09 06:10:42.000000000 +0000 
+++ tmpdir/maxpage1.out 2006-08-09 06:10:42.000000000 +0000 
@@ -1,16 +1,14 @@ 
  
 Elf file type is EXEC (Executable file) 
 Entry point 0x10000094 
-There are 3 program headers, starting at offset 52 
+There are 2 program headers, starting at offset 52 
  
 Program Headers: 
   Type           Offset   VirtAddr   PhysAddr   FileSiz MemSiz  Flg Align 
   LOAD           0x000000 0x10000000 0x10000000 0x00098 0x00098 R E 0x10000 
   LOAD           0x001000 0x10011000 0x10011000 0x00004 0x00004 RW  0x10000 
-  NULL           0x000000 0x00000000 0x00000000 0x00000 0x00000     0 
  
  Section to Segment mapping: 
   Segment Sections... 
    00     .text  
    01     .data  
-   02      
 
 
The PT_NULL section is generated by 
assign_file_positions_for_non_load_sections(). 
 
ld should probably not emit PT_NULL sections.
Comment 1 H.J. Lu 2006-08-10 15:34:30 UTC
I would suggest you create a separate testcase with -z relro so that this linker
bug is caught.
Comment 2 H.J. Lu 2006-08-14 23:20:41 UTC
A testcase to show that -z relro may create PT_NULL segment:

[hjl@gnu-16 tmp]$ cat x.s
        .globl main
        .globl start
        .globl _start
        .globl __start
        .text
main:
start:
_start:
__start:
        .long   0
[hjl@gnu-16 tmp]$ gcc -c x.s
[hjl@gnu-16 tmp]$ ld  -z relro x.o
[hjl@gnu-16 tmp]$ readelf -l a.out

Elf file type is EXEC (Executable file)
Entry point 0x4000b0
There are 2 program headers, starting at offset 64

Program Headers:
  Type           Offset             VirtAddr           PhysAddr
                 FileSiz            MemSiz              Flags  Align
  LOAD           0x0000000000000000 0x0000000000400000 0x0000000000400000
                 0x00000000000000b4 0x00000000000000b4  R E    200000
  NULL           0x0000000000000000 0x0000000000000000 0x0000000000000000
                 0x0000000000000000 0x0000000000000000         0

 Section to Segment mapping:
  Segment Sections...
   00     .text
   01

Comment 3 H.J. Lu 2006-08-15 00:42:08 UTC
A patch is posted at

http://sourceware.org/ml/binutils/2006-08/msg00166.html
Comment 4 H.J. Lu 2006-08-15 18:55:13 UTC
Well, my patch is incorrect. It isn't easy to remove the PT_NULL segment.
Comment 5 H.J. Lu 2006-08-16 22:00:22 UTC
This patch

http://sourceware.org/ml/binutils/2006-08/msg00187.html

works much better.
Comment 6 H.J. Lu 2006-08-17 02:06:11 UTC
Fixed.
Comment 7 H.J. Lu 2006-08-31 15:41:58 UTC
It is not fixed for all targets. "-z relro" doesn't work on Linux/ia64. Instead
of ignoring the option, a PT_NULL segment is created. This patch should fix
it:

http://sourceware.org/ml/binutils/2006-08/msg00263.html
Comment 8 John David Anglin 2006-09-04 19:25:04 UTC
I'm seeing these fails on hppa-unknown-linux-gnu:

FAIL: strip -z relro -shared (maxpage1)
FAIL: objcopy -z relro -shared (maxpage1)
Comment 9 H.J. Lu 2006-09-04 20:29:29 UTC
The patch fixing Linux/ia64 may also work for Linux/hppa.
Comment 10 dave@hiauly1.hia.nrc.ca 2006-09-05 01:37:52 UTC
Subject: Re:  -z relro may create PT_NULL segment

> The patch fixing Linux/ia64 may also work for Linux/hppa.

It works.

Dave
Comment 11 H.J. Lu 2006-09-05 16:26:45 UTC
Fixed.
Comment 12 H.J. Lu 2006-09-05 16:27:13 UTC
Fixed for Linux/ia64 and Linux/hppa.