彙編入門三 給各個 section指定位址

2021-09-06 15:34:40 字數 1894 閱讀 9906

root@iz2zee0spkwcgvz4do5kt2z:~/docker_use/free_study/ld_study/ld_asm# cat hello2.s

.text

.global _start

_start:

movl $len,%edx

movl $msg,%ecx

movl $1,%ebx

movl $4,%eax

int $0x80

movl $0,%ebx

movl $1,%eax

int $0x80

.data

msg:

.ascii "hello,world\n"

len=.-msg

.ascii "after hello\n"

len2=.-msg

先 as 一下,生成物件檔案 hello2.o

as -o hello2.o hello2.s
正常情況下,接下來直接鏈結一下就可以生成可執行檔案ld -o hello2.elf hello2.o, 但是這樣話程式的資料段和**段這些位址都是由編譯器給定的 ,可能不是我們想要的位址,如果要給各個段指定位址話,可以用如下鏈結指令碼:

root@iz2zee0spkwcgvz4do5kt2z:~/docker_use/free_study/ld_study/ld_asm# cat linker2.script

/* * linker script for the factorial

*/output(hello2.elf)

output_format("elf64-x86-64")

input(hello2.o)

sections

. = 0x600000;

.data :

}

root@iz2zee0spkwcgvz4do5kt2z:~/docker_use/free_study/ld_study/ld_asm# ld -t linker2.script
root@iz2zee0spkwcgvz4do5kt2z:~/docker_use/free_study/ld_study/ld_asm# readelf -s ./hello2.elf

symbol table '.symtab' contains 9 entries:

num: value size type bind vis ndx name

0: 0000000000000000 0 notype local default und

1: 0000000000400000 0 section local default 1

2: 0000000000600000 0 section local default 2

3: 0000000000000000 0 file local default abs hello2.o

4: 000000000000000c 0 notype local default abs len

5: 0000000000600000 0 notype local default 2 msg

6: 0000000000000018 0 notype local default abs len2

7: 0000000000000000 0 file local default abs

8: 0000000000400000 0 notype global default 1 _start

彙編入門指南

2.介紹 常見概念 3.推薦資源 本教程按照作者親身經歷和 x86組合語言 從實模式到保護模式 寫成 為什麼要學彙編?眾所周知,彙編是比較早期的一種程式語言和低階語言,雖然比不上高階語言的可讀性和效率,但這種語言作為最接近機器碼的語言可以很好的幫助我們了解最底層相關機制。mov指令是資料傳送指令 如...

彙編入門總結(5)

四 串指令 ds si 源串段暫存器 源串變址.es di 目標串段暫存器 目標串變址.cx 重複次數計數器.al ax 掃瞄值.d標誌 0表示重複操作中si和di應自動增量 1表示應自動減量.z標誌 用來控制掃瞄或比較操作的結束.movs 串傳送.movsb 傳送字元.movsw 傳送字.movs...

mips 彙編入門 helloworld

原始碼如下 hello.s vb view plain copy text segment text globl main main execution starts here la a0,str put string address into a0 li v0,4 system call to p...