主頁 > 軟體設計 > 為什么使用arm-none-eabi-ld聯結器更改目標檔案的順序會更改可執行行為?

為什么使用arm-none-eabi-ld聯結器更改目標檔案的順序會更改可執行行為?

2021-10-21 13:51:18 軟體設計

使用時我有不同的行為

arm-none-eabi-ld -T t.ld -o t.elf t.o ts.o

鏈接我的目標檔案,vs

arm-none-eabi-ld -T t.ld -o t.elf ts.o t.o

其中目標檔案“to”和“ts.o”在命令中被轉置。后一個版本會產生正確的行為,而較早的版本則不會。不同之處似乎是我的程式中的堆疊指標在第一個版本中設定不正確,我想知道為什么會這樣。

這是我正在使用的源檔案和聯結器腳本,以及要編譯的腳本。

t.ld

ENTRY(start) /* define start as the entry address */
SECTIONS
{
    . = 0x10000; /* loading address, required by QEMU */
    .text : { *(.text) }
    .data : { *(.data) }
    .bss : { *(.bss) }
    . =ALIGN(8);
        . =.   0x1000;
    stack_top =.;
}

tc

int g = 100; // un-initialized global

extern int sum(int a, int b, int c, int d, int e, int f);

int main() {
    int a, b, c, d, e, f; // local variables
    a = b = c = d = e = f = 1; // values do not matter
    g = sum(a, b, c, d, e, f); // call sum()
}

ts.s

/*
    Assembly file to define sum()
 */
    .global start, sum
start:
    ldr sp, =stack_top // set sp to stack top
    bl main // call main()

stop: b stop // loop

sum:
    // establish stack frame
    stmfd sp!, {fp, lr} // push lr and fp
    add fp, sp, #4 // fp -> saved lr on stack
    // compute sum of all 6 parameters
    add r0, r0, r1 // r0 = a   b
    add r0, r0, r2 // r0 = a   b   c
    add r0, r0, r3 // r0 = a   b   c   d
    ldr r3, [fp, #4] // r1 = e
    add r0, r0, r3 // r0 = a   b   c   d   e
    ldr r3, [fp, #8] // r1 = f
    add r0, r0, r3 // r0 = a   b   c   d   e   f
    // return
    sub sp, fp, #4 // point stack pointer to saved fp
    ldmfd sp!, {fp, pc} // return to caller

mk.sh(帶有產生預期結果的聯結器命令)

arm-none-eabi-as -o ts.o ts.s # assemble ts.s
arm-none-eabi-gcc -c t.c # cross-compile t.c into t.o
arm-none-eabi-ld -T t.ld -o t.elf ts.o t.o # link object files into t.elf
arm-none-eabi-objcopy -O binary t.elf t.bin # convert t.elf to t.bin

運行二進制檔案后

qemu-system-arm -M versatilepb -kernel t.bin -nographic -serial /dev/null

我得到以下資訊。堆疊指標(R13)正確

(qemu) info registers
R00=00000000 R01=00000001 R02=000100c0 R03=00000000
R04=00000000 R05=00000000 R06=00000000 R07=00000000
R08=00000000 R09=00000000 R10=00000000 R11=00000000
R12=00000000 R13=000110c8 R14=00010008 R15=00010008
PSR=400001d3 -Z-- A svc32
FPSCR: 00000000

使用帶有轉置目標檔案的聯結器命令對結果進行對比

(qemu) info registers
R00=00000000 R01=00000183 R02=00000100 R03=00000000
R04=00000000 R05=00000000 R06=00000000 R07=00000000
R08=00000000 R09=00000000 R10=00000000 R11=f3575ee4
R12=00000000 R13=f3575ec0 R14=00010060 R15=00010000
PSR=400001d3 -Z-- A svc32
FPSCR: 00000000

堆疊指標(R13)明顯超出程式的記憶體范圍。

uj5u.com熱心網友回復:

更簡單

flash.s

.global _start
_start:
    ldr sp,=0x11000
    bl main
    b .

閃存檔案

ENTRY(_start)

MEMORY
{
    ram : ORIGIN = 0x10000, LENGTH = 0x1000
}
SECTIONS
{
    .text   : { *(.text*)   } > ram
    .rodata : { *(.rodata*) } > ram
    .bss    : { *(.bss*)    } > ram
    .data   : { *(.data*)   } > ram
}

so.c

int  main ( void )
{   
    return 5;
}

建造

arm-none-eabi-as --warn --fatal-warnings  flash.s -o flash.o
arm-none-eabi-gcc -c -Wall -O2 -ffreestanding  so.c -o so.o
arm-none-eabi-ld -nostdlib -nostartfiles -T flash.ld flash.o so.o -o one.elf
arm-none-eabi-objdump -D one.elf > one.list
arm-none-eabi-objcopy -O binary one.elf one.bin
arm-none-eabi-ld -nostdlib -nostartfiles -T flash.ld so.o flash.o -o two.elf
arm-none-eabi-objdump -D two.elf > two.list
arm-none-eabi-objcopy -O binary two.elf two.bin

檢查

one.elf:     file format elf32-littlearm


Disassembly of section .text:

00010000 <_start>:
   10000:   e3a0da11    mov sp, #69632  ; 0x11000
   10004:   eb000000    bl  1000c <main>
   10008:   eafffffe    b   10008 <_start 0x8>

0001000c <main>:
   1000c:   e3a00005    mov r0, #5
   10010:   e12fff1e    bx  lr


two.elf:     file format elf32-littlearm


Disassembly of section .text:

00010000 <main>:
   10000:   e3a00005    mov r0, #5
   10004:   e12fff1e    bx  lr

00010008 <_start>:
   10008:   e3a0da11    mov sp, #69632  ; 0x11000
   1000c:   ebfffffb    bl  10000 <main>
   10010:   eafffffe    b   10010 <_start 0x8>

如果您將其作為 .bin 檔案運行,那么您的 C 引導程式代碼需要位于地址 0x10000 處。如果您沒有指定節或物件名稱,或者以某種方式告訴聯結器在那里專門放置一些東西,那么該工具會按照您在命令列上提供的內容進行處理,并按順序處理這些內容。因此,如果引導程式代碼首先在命令列上,那么該入口點將起作用,但是如果您將其他東西放在首位,那么它根本不會起作用,并且理想情況下會以某種方式崩潰。

Now qemu allows for elf files, and it may or may not support the entry point in the elf file and that might happen to work if you specify the entry point in the linker script, but of course when you then take the raw binary image version (-O binary..... .bin) version it will fail on hardware. Unless the code is being loaded by an elf loader or something similar (an operating system a sim environment like this that supports all of that cr@p) then just build the file correctly. (now understand for cortex-m sims qemu does/did look at the lsbit of the entry to properly start a cortex-m, so you NEED it there).

arm-none-eabi-nm -a one.elf | grep start
00010000 T _start
arm-none-eabi-nm -a two.elf | grep start
00010008 T _start

You should be able to remove the ENTRY in the above example and have one.bin just work. But two.bin will not. Maybe with the ENTRY() two.elf will work but not really what you should be relying on.

When building something baremetal you should always examine the entry point of the code based on the hardware (or sim) to see that you have built the binary correctly before trying to execute it. Any new project or any change in the build infrastructure...examine the toolchain output.

Note that if you are controlling the linker script then you do not need _start, even if you are not (something-ld -Ttext=0x1000 -Tdata=0x2000) you dont need it, it may give a warning (for the latter) but who cares. _start is defined as an entry point in the stock linker scripts, once you make your own and not use the stock ones, you pick the names of the entry point as desired and other things.

I find it wasteful because it is trivial to just get the command line right but you will see folks do this

flash.s

.section .init

    ldr sp,=0x11000
    bl main
    b .

.section .text

hello:
    b hello

flash.ld

MEMORY
{
    ram : ORIGIN = 0x10000, LENGTH = 0x1000
}
SECTIONS
{
    .init   : { *(.init*)   } > ram
    .text   : { *(.text*)   } > ram
    .rodata : { *(.rodata*) } > ram
    .bss    : { *(.bss*)    } > ram
    .data   : { *(.data*)   } > ram
}

build is the same

one.elf:     file format elf32-littlearm


Disassembly of section .init:

00010000 <.init>:
   10000:   e3a0da11    mov sp, #69632  ; 0x11000
   10004:   eb000001    bl  10010 <main>
   10008:   eafffffe    b   10008 <hello-0x4>

Disassembly of section .text:

0001000c <hello>:
   1000c:   eafffffe    b   1000c <hello>

00010010 <main>:
   10010:   e3a00005    mov r0, #5
   10014:   e12fff1e    bx  lr

two.elf:     file format elf32-littlearm


Disassembly of section .init:

00010000 <.init>:
   10000:   e3a0da11    mov sp, #69632  ; 0x11000
   10004:   eb000000    bl  1000c <main>
   10008:   eafffffe    b   10008 <main-0x4>

Disassembly of section .text:

0001000c <main>:
   1000c:   e3a00005    mov r0, #5
   10010:   e12fff1e    bx  lr

00010014 <hello>:
   10014:   eafffffe    b   10014 <hello>

You can see that hello and main swap based on the command line (.text) but .init was called out specifically in the linker script before .text.

I find this an ugly hack, YMMV. An even uglier hack is this

flash.s

ldr sp,=0x11000
bl main
b .

flash.ld

MEMORY
{
    ram : ORIGIN = 0x10000, LENGTH = 0x1000
}
SECTIONS
{
    .hello  : { flash.o (.text*)  } > ram
    .text   : { *(.text*)   } > ram
    .rodata : { *(.rodata*) } > ram
    .bss    : { *(.bss*)    } > ram
    .data   : { *(.data*)   } > ram
}

gives

one.elf:     file format elf32-littlearm


Disassembly of section .hello:

00010000 <.hello>:
   10000:   e3a0da11    mov sp, #69632  ; 0x11000
   10004:   eb000000    bl  1000c <main>
   10008:   eafffffe    b   10008 <main-0x4>

Disassembly of section .text:

0001000c <main>:
   1000c:   e3a00005    mov r0, #5
   10010:   e12fff1e    bx  lr

two.elf:     file format elf32-littlearm


Disassembly of section .hello:

00010000 <.hello>:
   10000:   e3a0da11    mov sp, #69632  ; 0x11000
   10004:   eb000000    bl  1000c <main>
   10008:   eafffffe    b   10008 <main-0x4>

Disassembly of section .text:

0001000c <main>:
   1000c:   e3a00005    mov r0, #5
   10010:   e12fff1e    bx  lr

As mentioned from the start. If you specifically call something out in the linker script it changes things otherwise it uses the command line (now there are exceptions to that I have seen). At the end of the day always examine the disassembly when creating a new project or changing the build to see that it is making a binary that will run. (entry point is at the right place if a fixed address, interworking is done right for the hand assembly parts, etc)

Note that

.text   : { *(.text*)   } > ram

the .text name on the left is whatever you want, most folks keep the name as it means something in a conventional way, but you can name these what you want on the left side. The compiler uses .text, .bss, .data or others so you have to get the right side one correct.

MEMORY
{
    ram : ORIGIN = 0x10000, LENGTH = 0x1000
}
SECTIONS
{
    .hello  : { flash.o (.text*)  } > ram
    .world  : { *(.text*)   } > ram
}

Disassembly of section .hello:

00010000 <.hello>:
   10000:   e3a0da11    mov sp, #69632  ; 0x11000
   10004:   eb000000    bl  1000c <main>
   10008:   eafffffe    b   10008 <main-0x4>

Disassembly of section .world:

0001000c <main>:
   1000c:   e3a00005    mov r0, #5
   10010:   e12fff1e    bx  lr

nm and readelf and others are just fine with this. Loader tools like an operating system or maybe qemu with an elf file may or may not want to see .bss, .data, etc...Have to deal with that on a case by case basis. Most folks just use the conventional names.

Note that the ram name on the memory sections is whatever you want to make it as well you could call it banana instead of ram or rom or flash or ... that you see other folks use.

轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/328584.html

標籤:集会 链接器 手臂 奇木 arm-none-eabi-gcc

上一篇:Python代理

下一篇:為什么要使用“ret”而不是“call”來呼叫方法?

標籤雲
其他(157675) Python(38076) JavaScript(25376) Java(17977) C(15215) 區塊鏈(8255) C#(7972) AI(7469) 爪哇(7425) MySQL(7132) html(6777) 基礎類(6313) sql(6102) 熊猫(6058) PHP(5869) 数组(5741) R(5409) Linux(5327) 反应(5209) 腳本語言(PerlPython)(5129) 非技術區(4971) Android(4554) 数据框(4311) css(4259) 节点.js(4032) C語言(3288) json(3245) 列表(3129) 扑(3119) C++語言(3117) 安卓(2998) 打字稿(2995) VBA(2789) Java相關(2746) 疑難問題(2699) 细绳(2522) 單片機工控(2479) iOS(2429) ASP.NET(2402) MongoDB(2323) 麻木的(2285) 正则表达式(2254) 字典(2211) 循环(2198) 迅速(2185) 擅长(2169) 镖(2155) 功能(1967) .NET技术(1958) Web開發(1951) python-3.x(1918) HtmlCss(1915) 弹簧靴(1913) C++(1909) xml(1889) PostgreSQL(1872) .NETCore(1853) 谷歌表格(1846) Unity3D(1843) for循环(1842)

熱門瀏覽
  • 面試突擊第一季,第二季,第三季

    第一季必考 https://www.bilibili.com/video/BV1FE411y79Y?from=search&seid=15921726601957489746 第二季分布式 https://www.bilibili.com/video/BV13f4y127ee/?spm_id_fro ......

    uj5u.com 2020-09-10 05:35:24 more
  • 第三單元作業總結

    1.前言 這應該是本學期最后一次寫作業總結了吧。總體來說,對作業的節奏也差不多掌握了,作業做起來的效率也更高了。雖然和之前的作業一樣,作業中都要用到新的知識,但是相比之前,更加懂得了如何利用工具以及資料。雖然之間卡過殼,但總體而言,這幾次作業還算完成的比較好。 2.作業程序總結 相比前兩個單元,此單 ......

    uj5u.com 2020-09-10 05:35:41 more
  • 北航OO(2020)第四單元博客作業暨課程總結博客

    北航OO(2020)第四單元博客作業暨課程總結博客 本單元作業的架構設計 在本單元中,由于UML圖具有比較清晰的樹形結構,因此我對其中需要進行查詢操作的元素進行了包裝,在樹的父節點中存盤所有孩子的參考。考慮到性能問題,我采用了快取機制,一次查詢后盡可能快取已經遍歷過的資訊,以減少遍歷次數。 本單元我 ......

    uj5u.com 2020-09-10 05:35:48 more
  • BUAA_OO_第四單元

    一、UML決議器設計 ? 先看下題目:第四單元實作一個基于JDK 8帶有效性檢查的UML(Unified Modeling Language)類圖,順序圖,狀態圖分析器 MyUmlInteraction,實際上我們要建立一個有向圖模型,UML中的物件(元素)可能與同級元素連接,也可與低級元素相連形成 ......

    uj5u.com 2020-09-10 05:35:54 more
  • 6.1邏輯運算子

    邏輯運算子 1. && 短路與 運算式1 && 運算式2 01.運算式1為true并且運算式2也為true 整體回傳為true 02.運算式1為false,將不會執行運算式2 整體回傳為false 03.只要有一個運算式為false 整體回傳為false 2. || 短路或 運算式1 || 運算式2 ......

    uj5u.com 2020-09-10 05:35:56 more
  • BUAAOO 第四單元 & 課程總結

    1. 第四單元:StarUml檔案決議 本單元采用了圖模型決議UML。 UML檔案可以抽象為圖、子圖、邊的邏輯結構。 在實作中,圖的節點包括類、介面、屬性,子圖包括狀態圖、順序圖等。 采用了三次遍歷UML元素的方法建圖,第一遍遍歷建點,第二、三次遍歷設定屬性、連邊,實作圖物件的初始化。這里借鑒了一些 ......

    uj5u.com 2020-09-10 05:36:06 more
  • 談談我對C# 多型的理解

    面向物件三要素:封裝、繼承、多型。 封裝和繼承,這兩個比較好理解,但要理解多型的話,可就稍微有點難度了。今天,我們就來講講多型的理解。 我們應該經常會看到面試題目:請談談對多型的理解。 其實呢,多型非常簡單,就一句話:呼叫同一種方法產生了不同的結果。 具體實作方式有三種。 一、多載 多載很簡單。 p ......

    uj5u.com 2020-09-10 05:36:09 more
  • Python 資料驅動工具:DDT

    背景 python 的unittest 沒有自帶資料驅動功能。 所以如果使用unittest,同時又想使用資料驅動,那么就可以使用DDT來完成。 DDT是 “Data-Driven Tests”的縮寫。 資料:http://ddt.readthedocs.io/en/latest/ 使用方法 dd. ......

    uj5u.com 2020-09-10 05:36:13 more
  • Python里面的xlrd模塊詳解

    那我就一下面積個問題對xlrd模塊進行學習一下: 1.什么是xlrd模塊? 2.為什么使用xlrd模塊? 3.怎樣使用xlrd模塊? 1.什么是xlrd模塊? ?python操作excel主要用到xlrd和xlwt這兩個庫,即xlrd是讀excel,xlwt是寫excel的庫。 今天就先來說一下xl ......

    uj5u.com 2020-09-10 05:36:28 more
  • 當我們創建HashMap時,底層到底做了什么?

    jdk1.7中的底層實作程序(底層基于陣列+鏈表) 在我們new HashMap()時,底層創建了默認長度為16的一維陣列Entry[ ] table。當我們呼叫map.put(key1,value1)方法向HashMap里添加資料的時候: 首先,呼叫key1所在類的hashCode()計算key1 ......

    uj5u.com 2020-09-10 05:36:38 more
最新发布
  • 【中介者設計模式詳解】C/Java/JS/Go/Python/TS不同語言實作

    * 中介者模式是一種行為型設計模式,它可以用來減少類之間的直接依賴關系,
    * 將物件之間的通信封裝到一個中介者物件中,從而使得各個物件之間的關系更加松散。
    * 在中介者模式中,物件之間不再直接相互互動,而是通過中介者來中轉訊息。 ......

    uj5u.com 2023-04-20 08:20:47 more
  • 露天煤礦現場調研和交流案例分享

    他們集團的資訊化公司及研究院在一個礦區正在做智能礦山的統一平臺的 試點,專案投資大概1億,包括了礦山的各方面的內容,顯示得我們這次交流有點多余。他們2年前開始做智能礦山的規劃,有很多煤礦行業專家的加持,他們的描述是非常完美,但是去年底應該上線的平臺,現在還沒有看到影子。他們確實有很多場景需求,但是被... ......

    uj5u.com 2023-04-20 08:20:25 more
  • 《社區人員管理》實戰案例設計&個人案例分享

    設計是一個讓人夢想成真程序,開始編碼、測驗、除錯之前進行需求分析和架構設計,才能保證關鍵方面都做正確 ......

    uj5u.com 2023-04-20 08:20:17 more
  • 軟體架構生態化-多角色交付的探索實踐

    作為一個技術架構師,不僅僅要緊跟行業技術趨勢,還要結合研發團隊現狀及痛點,探索新的交付方案。在日常中,你是否遇到如下問題 “ 業務需求排期長研發是瓶頸;非研發角色感受不到研發技改提效的變化;引入ISV 團隊又擔心質量和安全,培訓周期長“等等,基于此我們探索了一種新的技術體系及交付方案來解決如上問題。 ......

    uj5u.com 2023-04-20 08:20:10 more
  • 【中介者設計模式詳解】C/Java/JS/Go/Python/TS不同語言實作

    * 中介者模式是一種行為型設計模式,它可以用來減少類之間的直接依賴關系,
    * 將物件之間的通信封裝到一個中介者物件中,從而使得各個物件之間的關系更加松散。
    * 在中介者模式中,物件之間不再直接相互互動,而是通過中介者來中轉訊息。 ......

    uj5u.com 2023-04-20 08:19:44 more
  • 露天煤礦現場調研和交流案例分享

    他們集團的資訊化公司及研究院在一個礦區正在做智能礦山的統一平臺的 試點,專案投資大概1億,包括了礦山的各方面的內容,顯示得我們這次交流有點多余。他們2年前開始做智能礦山的規劃,有很多煤礦行業專家的加持,他們的描述是非常完美,但是去年底應該上線的平臺,現在還沒有看到影子。他們確實有很多場景需求,但是被... ......

    uj5u.com 2023-04-20 08:19:07 more
  • 《社區人員管理》實戰案例設計&個人案例分享

    設計是一個讓人夢想成真程序,開始編碼、測驗、除錯之前進行需求分析和架構設計,才能保證關鍵方面都做正確 ......

    uj5u.com 2023-04-20 08:18:57 more
  • 軟體架構生態化-多角色交付的探索實踐

    作為一個技術架構師,不僅僅要緊跟行業技術趨勢,還要結合研發團隊現狀及痛點,探索新的交付方案。在日常中,你是否遇到如下問題 “ 業務需求排期長研發是瓶頸;非研發角色感受不到研發技改提效的變化;引入ISV 團隊又擔心質量和安全,培訓周期長“等等,基于此我們探索了一種新的技術體系及交付方案來解決如上問題。 ......

    uj5u.com 2023-04-20 08:18:49 more
  • 05單件模式

    #經典的單件模式 public class Singleton { private static Singleton uniqueInstance; //一個靜態變數持有Singleton類的唯一實體。 // 其他有用的實體變數寫在這里 //構造器宣告為私有,只有Singleton可以實體化這個類! ......

    uj5u.com 2023-04-19 08:42:51 more
  • 【架構與設計】常見微服務分層架構的區別和落地實踐

    軟體工程的方方面面都遵循一個最基本的道理:沒有銀彈,架構分層模型更是如此,每一種都有各自優缺點,所以請根據不同的業務場景,并遵循簡單、可演進這兩個重要的架構原則選擇合適的架構分層模型即可。 ......

    uj5u.com 2023-04-19 08:42:41 more