Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Section alignment requirements and inserted padding #1399

Open
Mansindam opened this issue Jan 5, 2025 · 5 comments
Open

Section alignment requirements and inserted padding #1399

Mansindam opened this issue Jan 5, 2025 · 5 comments

Comments

@Mansindam
Copy link

I wasted a few hours finding out that sh_align is handled differently by ld then mold (and gold)
ld ignores it, mold respects it and will pad data with zeros

The thing is, programs cannot rely on either behavior (eg. rip-rela addressing into a .rodata), since if using the wrong linker it generates the worst kind of obscure, delayed failures.

Perhaps mold should emit a warning rather than silently insert padding between elements of a section

@Mansindam
Copy link
Author

I've also discovered that an alignment of 1 for a .rodata is capable of truncating/overwriting symbols explicitly sized 16

@rui314
Copy link
Owner

rui314 commented Jan 5, 2025

Can you give me an example to demonstrate the issue? As far as I know, GNU ld respects the section alignment.

@Mansindam
Copy link
Author

clang -fuse-ld=mold main.c test.o && ./a.out ; clang main.c test.o && ./a.out
KO
OK
#include <unistd.h>
#include <stdint.h>
#include <stdalign.h>
uint64_t ffilex(char const []);
int main() {
  uint64_t r = ffilex("showU64B ptr x = VWrite ptr (SubB (ReplicateB 48) (MovmB x)) %c\n");
  write(1 , r == 10235907949189046785ul ? "OK\n" : "KO\n", 3);
  return 0;
}```

Raw relocatable
test.txt

@rui314
Copy link
Owner

rui314 commented Jan 6, 2025

I have no idea what your ffilex function is supposed to do. Please share me an example that I can actually try myself. I need to reproduce the issue to debug.

@Mansindam
Copy link
Author

Mansindam commented Jan 6, 2025

On this input it returns a fixed number IFF the .rodata is setup correctly,
more importantly, why do you even want to run it? If you do just link it and run it..?
readelf shows the setup, you can also load it in gdb and inspect the .rodata memory layout, in particular symbols d0 and d1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants