2 ms·
All but the most trivial programs require a linker to resolve references among object files. And while "int main() {}" might seem like a trivial C program, it'
by wyldfire 2y ago
All but the most trivial programs require a linker to resolve references among object files. And while "int main() {}" might seem like a trivial C program, it's not (by that definition, at least).
Your favorite toolchain will often include archives and objects that you might take for granted like crt0.o, init.o, fini.o, libgcc/clang_rt.builtins and more.
The compiler's design is simplified by not having to resolve references among symbols. The assembler can do this for references within a section and linkers can do it among sections. Linkers might have to add trampolines/thunks for relocations that span a distance longer than the opcode could reach.
Loaders do this symbol resolution and relocation at runtime.
- high_na_euv 2y agoSo, if hello world is not trivial Then you can easily generate e.g web assembly from your own lang input without linker and it will run fine Aint the same viable for x86?
- wyldfire 2y ago> So, if hello world is not trivial Note that the nontrivial C program I showed above was not "hello world". Emitting output is yet more complicated still. > you can easily generate e.g web assembly from your own lang input without linker IIRC some things that generate wasm generate object files (e.g. C translation units). Those wasm objects require a linker to resolve symbol references to create the executable. > it will run fine ...on a wasm virtual machine. IIRC Java doesn't have anything like a traditional linker either. > Aint the same viable for x86? I mean, you can do anything you want inventing a new compiler that does much more than traditional compilers. But consider perhaps that there's a lot of value in the status quo that you might appreciate more if you were more familiar with the design of a toolchain. It's not that linkers are an absolute drop-dead necessity, it's that having one allows you to have distinct assembler and compiler functions that are much simpler to design by focusing on one kind of transformation.