How to execute dynamically loaded .init section?
Posted: Tue Dec 17, 2024 9:37 am
I'm implementing dynamic loading of libraries. I have the .init and .fini sections loaded into memory.
.init_array and .fini_array are easy because they're just a list of functions, but .init and .fini appears to be code to execute:
I can't just call .init as if it were a function, because there is no `ret`, so it keeps on executing into .fini, and then into the next section (.plt) and eventually crashes.
I was thinking of copying this code somewhere and putting a `ret` opcode so it's a callable function, but "e8" is near call relative (because the dynamically loaded library is position independent code), so I'd have to patch up any PIC I'd copy.
Surely there is an easier way? Can I force Clang/LLD to make .init not be a naked function (so it returns to the caller?) How are dynamic loaders suppose to execute the code in .init?
.init_array and .fini_array are easy because they're just a list of functions, but .init and .fini appears to be code to execute:
Code: Select all
Disassembly of section .init:
000000000000ecbc <.init>:
ecbc: e8 af f8 ff ff call 0xe570 <__fixunssfti+0x80>
Disassembly of section .fini:
000000000000ecc1 <.fini>:
ecc1: e8 fa f8 ff ff call 0xe5c0 <__fixunssfti+0xd0>
I was thinking of copying this code somewhere and putting a `ret` opcode so it's a callable function, but "e8" is near call relative (because the dynamically loaded library is position independent code), so I'd have to patch up any PIC I'd copy.
Surely there is an easier way? Can I force Clang/LLD to make .init not be a naked function (so it returns to the caller?) How are dynamic loaders suppose to execute the code in .init?