3

I'm trying to write a C program for a machine with no virtual memory(or O.S. to be more precise), and I'm running into some difficulties with the .rodata section, or more precisely the stuff that goes there. The problem arises that although the sections are positioned at well defined addresses during linking, when the program goes into execution, it is being relocated.

e.g.: Let's say that my program starts is linked to start at 0x1000, when it is being executed it what should be at 0x1000 is relocated at 0xff1000.

These being said, my problem is that the stuff that usually goes into .rodata are "hardcoded" by the compiler(gcc), and i kinda lose said .rodata constants in memory because of the relocation and the gcc hardcoding their addresses so that they get an absolute offset instead of a relative one.

Is there a way that I can get the .rodata constants to have a relative offset instead of an absolute one. And by relative, I mean relative to any active process registers?

skyel
  • 713
  • 1
  • 6
  • 20
  • It might be as simple as generating position independent code (`-fPIC` with GCC). Is the code always relocated to `0xff1000` or is it variable? – user786653 Aug 17 '11 at 10:06
  • It might be variable. I mean for now it isn't but you never know what program loads this code. I managed to workaround this in the and after manually relocating the code to where it should be(this was actually the point where I should have gotten to, but it was far-fetched and the debugging was almost to inexistent). As for the fPIC, I really can't test it anymore, but after doing a cross reference between a -fPIC compiled source, and one without -fPIC it seems to have worked(though I wouldn't hold my breath, because it does some pretty weird stuff) – skyel Aug 25 '11 at 11:19
  • What target format are you compiling to? .rodata does, IIRC, contain relative addresses. It's the loader's job to add them to the code's base address and update the .text section accordingly. I wrote my own OS and application loader and that's what I can remember. – Unsigned Sep 09 '11 at 15:47
  • Since this question isn't answered yet: Please state the platform. `.rodata` contains the read-only data, but its location in memory is determined by the locator (usually part of the linker), which takes a configuration file specifying the memory areas available. Maybe you have a config file specifying a different location for `.rodata` than you would like/think/assume. I'd need more details, though. – Johan Bezem Nov 17 '11 at 16:30

1 Answers1

1

Depending on the architecture, It may be possible that the .rodata is arbitrally relocated to a specific memory area (a ROM for instance). This kind of information can be found in your machine datasheet. If you are in this case, you have to tell the link to put your .rodata section in the right area, using a linker script. A good overview of how GCC linker scripts work can be found here:

http://www.scoberlin.de/content/media/http/informatik/gcc_docs/ld_3.html

Moreover, you can easily find a lot of architecture-specific link scripts on the internet.

Hope that helped!

Nicolas Floquet
  • 424
  • 3
  • 11