This is the mail archive of the binutils@sourceware.org mailing list for the binutils project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

.data.rel Section Contains Both Data and Relocatable Symbols


I am writing position independent code for a MIPS embedded system.  When
our embedded bootloader loads our code, it fixes up the .data.rel
section by adding the base address of the code to every 4-byte word in
that section.  In other words, it treats everything in that section as a
symbol that needs to be relocated.  However, this means that only
relocatable symbols can exist in that .data.rel section.  But what
happens when  we have a global structure that contains both plain data
and relocatable symbols?  The entire structure will be put in the
.data.rel section, and the data part of the structure will incorrectly
be modified by the bootloader, which thinks the data is a relocatable
symbol.  How do we know what part of the .data.rel section needs to be
relocated and which part is data?  Is this an issue with GCC?  Is there
another way to do this?

Here is how I am compiling the example code below (using gcc version
4.1.1 and ld version 2.17):

mips-elf-gcc -mips4 -G0 -mabicalls -fno-builtin -Wall -nostdlib
-nostartfiles
-Wl,-N,-q,-E,--warn-multiple-gp,--whole-archive,--script=link.ld -o
testreloc.elf testreloc.c

Here is the example:

// simple function type
typedef void (*BarFunc)();

// structure that contains both plain data (foo) and relocatable
symbols (bar)
typedef struct
{
    int foo;
    BarFunc barFunc;
} FooBar;

// instance of structure that contains both data and relocatable
symbols
// NOTE: this is placed in the .data.rel section
static FooBar aFooBar = { 456, aBarFunc };

// simple function
void aBarFunc()
{
    volatile int* p = (int*) 0x87654321;
    *p = 0x123;
}

int
main()
{
    volatile int* p = (int*) 0x12345678;
    *p = aFooBar.foo;
    aFooBar.barFunc();
    return 0;
}

For this example, my link.ld file was as follows:

ENTRY(main)
SECTIONS
{
    . = 0;
}


>>mips-elf-objdump -h testreloc.elf    

testreloc.elf:     file format elf32-bigmips

Sections:
Idx Name          Size      VMA       LMA       File off  Algn
  0 .text         000000a8  00000000  00000000  00000060  2**2
                  CONTENTS, ALLOC, LOAD, RELOC, CODE
  1 .data.rel     00000008  000000a8  000000a8  00000108  2**2
                  CONTENTS, ALLOC, LOAD, RELOC, DATA
  2 .got          00000020  000000b0  000000b0  00000110  2**4
                  CONTENTS, ALLOC, LOAD, DATA
  3 .reginfo      00000018  00000000  00000000  00000130  2**2
                  CONTENTS, READONLY, LINK_ONCE_DISCARD
  4 .pdr          00000040  00000000  00000000  00000148  2**2
                  CONTENTS, RELOC, READONLY
  5 .comment      00000012  00000000  00000000  00000188  2**0
                  CONTENTS, READONLY



>>mips-elf-objdump -s testreloc.elf    

testreloc.elf:     file format elf32-bigmips

Contents of section .text:
 0000 3c1c0000 279c0000 0399e021 27bdffd8  <...'......!'...
 0010 afbf0024 afbe0020 03a0f02d afbc0010  ...$... ...-....
 0020 3c021234 34425678 afc20018 8f8200b8  <..44BVx........
 0030 8c4300a8 8fc20018 ac430000 8f8200b8  .C.......C......
 0040 244200a8 8c590004 0320f809 00000000  $B...Y... ......
 0050 8fdc0010 0000102d 03c0e82d 8fbf0024  .......-...-...$
 0060 8fbe0020 27bd0028 03e00008 00000000  ... '..(........
 0070 27bdffe8 afbe0010 03a0f02d 3c028765  '..........-<..e
 0080 34424321 afc20008 8fc30008 24020123  4BC!........$..#
 0090 ac620000 03c0e82d 8fbe0010 27bd0018  .b.....-....'...
 00a0 03e00008 00000000                    ........        

Contents of section .data.rel:
 00a8 000001c8 00000070                    .......p        

Contents of section .got:
 00b0 00000000 80000000 00000000 00000000  ................
 00c0 00000000 00000000 00000000 00000000  ................

Contents of section .reginfo:
 0000 f200000c 00000000 00000000 00000000  ................
 0010 00000000 00000000                    ........        

Contents of section .pdr:
 0000 00000000 c0000000 fffffffc 00000000  ................
 0010 00000000 00000028 0000001e 0000001f  .......(........
 0020 00000070 40000000 fffffff8 00000000  ...p@...........
 0030 00000000 00000018 0000001e 0000001f  ................

Contents of section .comment:
 0000 00474343 3a202847 4e552920 342e312e  .GCC: (GNU) 4.1.
 0010 3100       



Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]