Comments (4)
Thank you for this issue! This is indeed an error.
The right sequence should be:
$ nasm -f elf64 -o main.o main.asm
$ nasm -f elf64 -o libso.o libso.asm
$ ld -shared -o libso.so libso.o
$ ld -o main main.o -d libso.so --dynamic-linker=/lib64/ld-linux-x86-64.so.2
The --dynamic-linker
should be provided on the final stage when the executable file is produced, not when creating the shared library itself.
If you compare readelf
output on the object file you've created with the new one you will see a difference:
$ # Without dynamic linker
$ ld -o main main.o -d libso.so $ ./main bash: no such file or directory: ./main
$ readelf -l main
Elf file type is EXEC (Executable file)
Entry point 0x400270
There are 5 program headers, starting at offset 64
Program Headers:
Type Offset VirtAddr PhysAddr
FileSiz MemSiz Flags Align
PHDR 0x0000000000000040 0x0000000000400040 0x0000000000400040
0x0000000000000118 0x0000000000000118 R E 8
INTERP 0x0000000000000158 0x0000000000400158 0x0000000000400158
0x000000000000000f 0x000000000000000f R 1
[Requesting program interpreter: /lib/ld64.so.1]
LOAD 0x0000000000000000 0x0000000000400000 0x0000000000400000
0x0000000000000288 0x0000000000000288 R E 200000
LOAD 0x0000000000000288 0x0000000000600288 0x0000000000600288
0x0000000000000130 0x0000000000000130 RW 200000
DYNAMIC 0x0000000000000288 0x0000000000600288 0x0000000000600288
0x0000000000000110 0x0000000000000110 RW 8
Section to Segment mapping:
Segment Sections...
00
01 .interp
02 .interp .hash .dynsym .dynstr .rela.plt .plt .text
03 .dynamic .got.plt
04 .dynamic
$ # Now with dynamic linker
$ ld -o main main.o -d libso.so --dynamic-linker=/lib64/ld-linux-x86-64.so.2
$ ./main
Shared object
$ readelf -l main sayon@antheus
Elf file type is EXEC (Executable file)
Entry point 0x400280
There are 5 program headers, starting at offset 64
Program Headers:
Type Offset VirtAddr PhysAddr
FileSiz MemSiz Flags Align
PHDR 0x0000000000000040 0x0000000000400040 0x0000000000400040
0x0000000000000118 0x0000000000000118 R E 8
INTERP 0x0000000000000158 0x0000000000400158 0x0000000000400158
0x000000000000001c 0x000000000000001c R 1
[Requesting program interpreter: /lib64/ld-linux-x86-64.so.2]
LOAD 0x0000000000000000 0x0000000000400000 0x0000000000400000
0x0000000000000298 0x0000000000000298 R E 200000
LOAD 0x0000000000000298 0x0000000000600298 0x0000000000600298
0x0000000000000130 0x0000000000000130 RW 200000
DYNAMIC 0x0000000000000298 0x0000000000600298 0x0000000000600298
0x0000000000000110 0x0000000000000110 RW 8
Section to Segment mapping:
Segment Sections...
00
01 .interp
02 .interp .hash .dynsym .dynstr .rela.plt .plt .text
03 .dynamic .got.plt
04 .dynamic
Without -dynamic-linker
you are going to silently get a "default" dynamic loader, whose path is hardcoded in ld
:
[Requesting program interpreter: /lib/ld64.so.1]
Most of us do not have a dynamic loader stored with such a name in /lib
. That is why the explicit -dynamic-linker
option is mandatory in order to obtain a working executable. It changes the relevant field in the resulting ELF program header so that it stores a correct path to the dynamic loader:
[Requesting program interpreter: /lib64/ld-linux-x86-64.so.2]
GCC resolves the loader path for you before calling linker internally.
You will read more on that in Chapter 15 which is entirely dedicated to writing dynamic libraries in C and assembly.
Again thank you and I am going to correct that in my next commit.
from low-level-programming.
Fixed in #13
from low-level-programming.
Thanks for the solusion and explanation.
The same correction needs to be done for the make files also. I'll create a pull request for that.
from low-level-programming.
Makefiles fixed in pull request #14.
from low-level-programming.
Related Issues (20)
- Debian Virtual Machine: git clone: could not resolve host github.com HOT 2
- Question / Answers: Combined HOT 1
- Question 44 typo HOT 2
- SSH into VM not working HOT 2
- typo in errata HOT 2
- error when run on Mac HOT 1
- VirtualBox display resolution HOT 3
- Q34 HOT 1
- Q39 HOT 1
- Q40 HOT 1
- Q70 HOT 1
- Assignment: Dictionary
- Section 1.3.1 HOT 2
- Docker setup HOT 3
- Missing answers to questions HOT 1
- There is no need in having `pyc` files present in the repository
- Python scripts compatibility
- Virtual Machine
- Question 22 HOT 2
- Nasm can't create output file
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from low-level-programming.