背景
我正在研究一個 2015 CS61C (Berkeley) 課程專案,該專案是關于撰寫一個聯結器來鏈接從 MIPS 指令集的以下子集生成的目標檔案。
Add Unsigned: addu $rd, $rs, $rt
Or: or $rd, $rs, $rt
Set Less Than: slt $rd, $rs, $rt
Set Less Than Unsigned: sltu $rd, $rs, $rt
Jump Register: jr $rs
Shift Left Logical: sll $rd, $rt, shamt
Add Immediate Unsigned: addiu $rt, $rs, immediate
Or Immediate: ori $rt, $rs, immediate
Load Upper Immediate: lui $rt, immediate
Load Byte: lb $rt, offset($rs)
Load Byte Unsigned: lbu $rt, offset($rs)
Load Word: lw $rt, offset($rs)
Store Byte: sb $rt, offset($rs)
Store Word: sw $rt, offset($rs)
Branch on Equal: beq $rs, $rt, label
Branch on Not Equal: bne $rs, $rt, label
Jump: j label
Jump and Link: jal label
Load Immediate: li $rt, immediate
Branch on Less Than: blt $rs, $rt, label
從這個指令子集中,我認為需要重定位的是j, bne,beq指令(blt是偽指令),如果標簽不在同一個檔案中,則后兩條需要重定位。
執行指令重定位的 MIPS 函式的注釋讀取
#------------------------------------------------------------------------------
# function relocate_inst()
#------------------------------------------------------------------------------
# Given an instruction that needs relocation, relocates the instruction based
# on the given symbol and relocation table.
#
# You should return error if 1) the addr is not in the relocation table or
# 2) the symbol name is not in the symbol table. You may assume otherwise the
# relocation will happen successfully.
#
# Arguments:
# $a0 = an instruction that needs relocating
# $a1 = the byte offset of the instruction in the current file
# $a2 = the symbol table
# $a3 = the relocation table
#
# Returns: the relocated instruction, or -1 if error
請注意,重定位表包含相對于被鏈接目標檔案的開頭的地址,而符號表是所有被鏈接目標檔案的符號表的集合,并包含絕對地址。
問題
If the instruction to be relocated is a
jinstruction, since$a1contains the relative address of the instruction, we find the label that needs to be relocated in the relocation table, and then find the absolute address for that label in the symbol table. We can than add (absolute address >> 2) as the low 26 bits of the instruction.If the instruction to be relocated is
bne, orbeqhowever, I am not sure what to do, since the low order bits are supposed to be relative to PC 4, but we don't know what the absolute address of the instruction being relocated is, so we don't know what PC 4 is.
Looking at various solutions online, it seems that only j relocations are handled.
Am I missing something?
編輯:我們只考慮文本段。
uj5u.com熱心網友回復:
我的猜測是這個聯結器不處理外部標簽的分支指令(bne或)。beq
這將排除使用beq labelwhere label is external(全域和在另一個目標檔案中),但這只有在匯編中才能真正做到。
例如,編譯器輸出將在單個函式中同時包含分支指令和目標位置,該函式進入單個代碼塊。(模某些尾呼叫優化)。
有了這個限制,編譯器或匯編器已經使用 pc 相對尋址修復了所有指令bne和beq指令——這些指令不需要在重定位表中添加條目。
此外,分支 ( beq/ bne) 指令的范圍 ( /-128k) 比 for 短j,因此如果聯結器真的打算支持分支到外部標簽,它可能還必須提供引入分支島來處理的能力那些分支太遠的。
要擴展您的示例:
if ( a1 == a0 )
printf ("hello")
將是
bne a1, a0, endIf1
la a0, Lhello
jal printf
endIf1:
一些編譯器不知道哪個函式在什么 DLL 中,所以即使printf在 DLL 中,編譯器輸出看起來仍然相同。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/443219.html
