Bug 29954 - bpf sim tests fail due to linker triggering LOAD segment with RWX permissions test by default
Summary: bpf sim tests fail due to linker triggering LOAD segment with RWX permissions...
Status: RESOLVED FIXED
Alias: None
Product: gdb
Classification: Unclassified
Component: sim (show other bugs)
Version: HEAD
: P2 normal
Target Milestone: ---
Assignee: Jose E. Marchesi
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2023-01-03 05:32 UTC by Mike Frysinger
Modified: 2023-01-05 01:55 UTC (History)
4 users (show)

See Also:
Host:
Target: bpf
Build:
Last reconfirmed:
Project(s) to access:
ssh public key:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Mike Frysinger 2023-01-03 05:32:30 UTC
using the bpf target, the sim testsuite fails now due to linker warnings ever since this change:
https://sourceware.org/git/?p=binutils-gdb.git;a=commit;h=ba951afb99912da01a6e8434126b8fac7aa75107
Author: Nick Clifton <nickc@redhat.com>
Date:   Tue May 3 11:42:24 2022 +0100
Add a linker warning when creating potentially dangerous executable segments.  Add tests, options to disabke and configure switches to choose defaults.

simple test:
$ cat test.s
.bss
.space 64
_stack:

.data
pass:
.string "pass\n"

.text
.global _start
_start:
.byte 0x00

$ ./gas/as-new test.s -o test.o
$ ./ld/ld-new test.o -Ttext=0x0 -o test
./ld/ld-new: warning: test has a LOAD segment with RWX permissions

i know the use of -Ttext=0 is a bit odd, but it's what the bpf sim testsuite is doing.  i'll leave it to Jose to figure out what the right approach is here.
Comment 1 Nick Clifton 2023-01-03 15:18:22 UTC
Hi Jose,

  In case it helps I have proposed a patch for a similar problem for the MN10300
  sim:

    https://sourceware.org/bugzilla/attachment.cgi?id=14546&action=edit

Cheers
  Nick
Comment 2 Jose E. Marchesi 2023-01-03 17:53:27 UTC
Hi Nick.

I tried that locally and definitely works.  But I don't think it is the right fix in this case.

When linked without -Ttext=0x0, the following segments are created in the resulting executable:

 Elf file type is EXEC (Executable file)                                                                        
 Entry point 0x400000                                                                                           
 There are 2 program headers, starting at offset 64                                                             
                                                                                                               
 Program Headers:                                                                                               
   Type           Offset             VirtAddr           PhysAddr                                                
                  FileSiz            MemSiz              Flags  Align                                           
   LOAD           0x0000000000100000 0x0000000000400000 0x0000000000400000                                      
                  0x0000000000000648 0x0000000000000648  R E    0x100000                                        
   LOAD           0x0000000000101648 0x0000000000801648 0x0000000000801648                                      
                  0x0000000000000006 0x0000000000000006  RW     0x100000                                        
                                                                                                               
  Section to Segment mapping:                                                                                   
   Segment Sections...                                                                                          
    00     .text                                                                                                
    01     .data                                                                

Which is perfectly fine.  But then it is the simulator that fails with:

  spawn ./bpf/run --memory-size=4Mb /home/jemarch/gnu/src/binutils-gdb/build-bpf/sim/alu.s.x
  core: 8 byte read to unmapped address 0x400000 at 0x400000

Meaning the simulator is somehow not loading the segments at the right VMA.
Comment 3 Guillermo E. Martinez 2023-01-03 21:36:20 UTC
(In reply to Jose E. Marchesi from comment #2)
> Hi Nick.
> 
> I tried that locally and definitely works.  But I don't think it is the
> right fix in this case.
> 
> When linked without -Ttext=0x0, the following segments are created in the
> resulting executable:
> 
>  Elf file type is EXEC (Executable file)                                    
> 
>  Entry point 0x400000                                                       
> 
>  There are 2 program headers, starting at offset 64                         
> 
>                                                                             
> 
>  Program Headers:                                                           
> 
>    Type           Offset             VirtAddr           PhysAddr            
> 
>                   FileSiz            MemSiz              Flags  Align       
> 
>    LOAD           0x0000000000100000 0x0000000000400000 0x0000000000400000  
> 
>                   0x0000000000000648 0x0000000000000648  R E    0x100000    
> 
>    LOAD           0x0000000000101648 0x0000000000801648 0x0000000000801648  
> 
>                   0x0000000000000006 0x0000000000000006  RW     0x100000    
> 
>                                                                             
> 
>   Section to Segment mapping:                                               
> 
>    Segment Sections...                                                      
> 
>     00     .text                                                            
> 
>     01     .data                                                            
> 
> 
> Which is perfectly fine.  But then it is the simulator that fails with:
> 
>   spawn ./bpf/run --memory-size=4Mb
> /home/jemarch/gnu/src/binutils-gdb/build-bpf/sim/alu.s.x
>   core: 8 byte read to unmapped address 0x400000 at 0x400000
> 
> Meaning the simulator is somehow not loading the segments at the right VMA.

Increasing `--memory-size' in the simulator from 4Mb to 10MB SIGSEGV is gone, because .data start at address: 0x801648,

Program Headers:
  Type           Offset             VirtAddr           PhysAddr
                 FileSiz            MemSiz              Flags  Align
  LOAD           0x0000000000100000 0x0000000000400000 0x0000000000400000
                 0x0000000000000648 0x0000000000000648  R E    0x100000
  LOAD           0x0000000000101648 0x0000000000801648 0x0000000000801648
                 0x0000000000000006 0x0000000000000006  RW     0x100000
Comment 4 Mike Frysinger 2023-01-05 01:55:26 UTC
merged Guillermo's change to increase the sim memory size & drop the -T flag