3

I've seen both .rodata and .rdata being used as segments. Which one should I use and what are the differences between them?

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
Ark
  • 73
  • 2
  • 8
  • 1
    It depends on the object file format. – fuz Jan 16 '21 at 01:05
  • Windows PE format – Ark Jan 16 '21 at 01:06
  • 1
    Using `.rdata` on Windows, `.rodata` on Linux/Unix systems (at least in ELF object files, IDK about MacOS Mach-O). – Peter Cordes Jan 16 '21 at 01:07
  • All that matters is the consumer of the file, if the consumer is the software that loads binaries to execute on an operating system (and perhaps version) then you would want to conform to that. If you are making your own tools to consume or dont care (bare metal for example) then at least with gnu tools it is trivial to change the names to whatever you want .bob, .ted, .foo, .bar... – old_timer Jan 16 '21 at 05:08
  • Use existing (working) tools for that consumer and see what those tools generate and use that. Once you know that works you can certainly experiment and see if the other form works as well. – old_timer Jan 16 '21 at 05:09
  • What happened when you tried each of these forms? – old_timer Jan 16 '21 at 05:10

2 Answers2

4

RODATA is used in ELF and RDATA is used in COFF. The closest equivalent in Mach-O would be __TEXT,__const.

Olsonist
  • 2,051
  • 1
  • 20
  • 35
2

What is the difference between .rodata and .rdata

The main (only?) difference is cultural.

For linkers and loaders (and object file formats), you can use whatever name you like. It doesn't matter as long as (during linking) things that should be in the same section use the same name for that section, and the properties of that section are adequately described (e.g. using a linker script, including the default linker script for the OS). There also isn't anything preventing you from having multiple separate sections (e.g. maybe "rdata_initialization", "rdata_frequently_used" and "rdata_rarely_used" to improve cache locality) if you're willing to deal with the extra hassle.

However, for convenience (given that most people don't do anything special with sections), to ensure tools use the same name for the same section compilers use whatever is "normal" for a certain OS as the default, and will assume ".rdata" for Windows (because that's normal for Windows) and "rodata" for *nix clones (because that's normal for Unix).

Brendan
  • 35,656
  • 2
  • 39
  • 66
  • Wouldn't rodata mean "read only?" If you have a string ("hello world"), that's not code, so it's data. But it's data that isn't meant to be writable. Given that some architectures support marking memory pages as readable/writable/executable, pages that contain data that isn't meant to be modified could go in "read only" pages. – David Wohlferd Jan 16 '21 at 03:50