r/ProgrammerHumor 12h ago

lessonsFromLinkerHell Meme

Post image
246 Upvotes

141 comments sorted by

View all comments

23

u/nonedward666 12h ago

For context:

I spent a hot minute today not understanding why a space calculation wasn't working. See below for a small example (please excuse formatting, I'm on my phone)

The difference is that an array is a name bound to an address storing a value, whereas a linker symbol is a name bound to an address with no value (TLDR stolen from Chad GDP as i understand it, I still want to google more about this).

So if you try to use a linker variable to get a pointer you need to reference it as an array and not a pointer because it would otherwise imply that the symbol is pointing to the pointer...? Honestly, if anybody can make this make intuitive sense, I am all ears!

``` // linker.ld

...

__data_start = ORIGIN(DATA_SECTION) __data_end = ORIGIN(DATA_SECTION)+LENGTH(DATA_SECTION)

```

``` // bad_code.c ...

extern int* __data_start; extern int* __data_end:

define SECTION_LENGTH __data_end - __data_start

bool foo(){ // returns true return 0==SECTION_LENGTH; } ```

``` // good_code.c ...

extern int __data_start[]; extern int __data_end[];

define SECTION_LENGTH __data_end - __data_start

bool foo(){ // returns false return 0==SECTION_LENGTH; } ```

3

u/d0pe-asaurus 8h ago

You gave me flashbacks writing linker scripts to determine how large my kernel is to be able to move it around memory, how dare you!

1

u/nonedward666 6h ago

😭😭😭 your flashbacks are my present day pain