链接脚本文件中定义的符号怎么在c源文件,汇编.s文件中引用

参考文档:
the Gnu Linker ld
 3.5.4 Source Code Reference

Accessing a linker script defned variable from source code is not intuitive. In particular a
linker script symbol is not equivalent to a variable declaration in a high level language, it
is instead a symbol that does not have a value.

从源文件中访问链接脚本中定义的变量不能直接访问,链接脚本中的变量是一个符号而不是一个具体的值

Before going further, it is important to note that compilers often transform names in the
source code into different names when they are stored in the symbol table. For example,
Fortran compilers commonly prepend or append an underscore, and C++ performs extensive
‘name mangling’. Therefore there might be a discrepancy between the name of a variable
as it is used in source code and the name of the same variable as it is defned in a linker
script

For example in C a linker script variable might be referred to as:

extern int foo;

But in the linker script it might be defned as:

_foo = 1000;

however it is assumed that no name transformation has taken place.

编译器通常会把源代码中的name(变量名或者函数名)进行转换然后在存放在symbol table,例如fortran通常会在name的前或后加下划线,因此相同的变量在源代码中和链接脚本中名字可能不一样

When a symbol is declared in a high level language such as C, two things happen. The frst is that the compiler reserves enough space in the program’s memory to hold the value of
the symbol. The second is that the compiler creates an entry in the program’s symbol table
which holds the symbol’s address. ie the symbol table contains the address of the block of memory holding the symbol’s value. So for example the following C declaration, at fle scope

int foo = 1000;

creates a entry called ‘foo’ in the symbol table. This entry holds the address of an ‘int’
sized block of memory where the number 1000 is initially stored.

在c语言里面声明一个symbol,首先编译器会保留足够的空间去存储这份symbol的值,然后会在符号表中添加一个条目来保存symbol的值,例子说的很明确,首先symbol table中会添加一个entry,名字叫做foo,这个entry保存着foo变量的地址,地址里面存的内容是1000

When a program references a symbol the compiler generates code that frst accesses the
symbol table to fnd the address of the symbol’s memory block and then code to read the
value from that memory block. So:
//程序引用一个symbol时首先在symbol table里面查找symbol的地址,然后在把这个地址里面的数据读取出来

foo = 1;

looks up the symbol ‘foo’ in the symbol table, gets the address associated with this symbol
and then writes the value 1 into that address.
//这个例子说明对变量foo的访问首先去 symbol table获取foo地址,然后往改地址写入1
Whereas:

int * a = & foo;

looks up the symbol ‘foo’ in the symbol table, gets it address and then copies this address
into the block of memory associated with the variable ‘a’
//foo的地址 --->a变量

Linker scripts symbol declarations, by contrast, create an entry in the symbol table but donot assign any memory to them. Thus they are an address without a value. So for example the linker script defnition:
//链接脚本里声明变量的话只会在symbol table里面添加entry,不会为其分配内存.

foo = 1000;

creates an entry in the symbol table called ‘foo’ which holds the address of memory location
1000, but nothing special is stored at address 1000. This means that you cannot access the
value of a linker script defned symbol - it has no value - all you can do is access the address
of a linker script defned symbol.

上面的例子只是在symbol table创建了一个foo,地址是1000,1000为地址的内存里面什么内容都没有,所以你不能访问foo(1000里面的内容),你只能获取foo的地址(1000)

Hence when you are using a linker script defned symbol in source code you should always
take the address of the symbol, and never attempt to use its value. For example suppose
you want to copy the contents of a section of memory called .ROM into a section called
.FLASH and the linker script contains these declarations:

start_of_ROM = .ROM;
end_of_ROM = .ROM + sizeof (.ROM) - 1;
start_of_FLASH = .FLASH;

Then the C source code to perform the copy would be:

extern char start_of_ROM, end_of_ROM, start_of_FLASH;
memcpy (& start_of_FLASH, & start_of_ROM, & end_of_ROM - & start_of_ROM);

Note the use of the ‘&’ operators. These are correct.

c代码中使用变量见上方,需要用extern声明.然后在取地址.

asm文件里面怎么使用呢?好像是可以直接就使用.word把__bss_start的值存在标号_bss_start处相当于*_bss_start = __bss_start的值

.globl _bss_start
_bss_start:
    .word __bss_start

另外一个自己做实验的例子
Makefile


exam.bin:exam1.o
    arm-linux-ld -Texam.lds -o exam1_elf $<
    arm-linux-objdump -D -m arm exam1_elf > exam1_dis


%.o:%.c
    arm-linux-gcc -c $< 

%.o:%.S
    arm-linux-gcc -c $< 


clean:  
    rm *.o *.bin *_dis *_elf

exam.lds链接脚本

SECTIONS {
    .text : {*.text}
    __bss_start = .;
    __bss_end   = .;
    __dd        = .;
}

exam1.S文件,我直接使用lds脚本里的__dd,取其值存入r3,编译竟然能通过.也没有报错说没有找到__dd.

.global _start
.global _bss_start
_bss_start:

    .word __bss_start
.global abcd

abcd:
    .word 0x1234
.global _dd
_dd:
    .word __dd
_start:
    mov r0, #1
    nop
    nop
    adr r1, _bss_start
    adr r2, _dd
    nop
    ldr r3, =__dd

查看.text段有多大,大小是0x2c,反汇编看一下最终r3寄存器是不是0x2c

book@book-desktop:~/exam/inline_asm$ objdump -h exam1_elf 

exam1_elf:     file format elf32-little

Sections:
Idx Name          Size      VMA       LMA       File off  Algn
  0 .text         0000002c  00000000  00000000  00008000  2**2
                  CONTENTS, ALLOC, LOAD, READONLY, CODE
  1 .glue_7       00000000  0000002c  0000002c  0000802c  2**2
                  CONTENTS, ALLOC, LOAD, READONLY, CODE
  2 .glue_7t      00000000  0000002c  0000002c  0000802c  2**2
                  CONTENTS, ALLOC, LOAD, READONLY, CODE
  3 .data         00000000  0000002c  0000002c  0000802c  2**0
                  CONTENTS, ALLOC, LOAD, DATA
  4 .bss          00000000  0000002c  0000002c  0000802c  2**0
                  ALLOC

exam1_dis反汇编文件
24: e51f3004 ldr r3, [pc, #-4] ; 28
r3确实是0x2c


exam1_elf:     file format elf32-littlearm

Disassembly of section .text:

00000000 <_bss_start>:
   0:   0000002c    andeq   r0, r0, ip, lsr #32

00000004 <abcd>:
   4:   00001234    andeq   r1, r0, r4, lsr r2

00000008 <_dd>:
   8:   0000002c    andeq   r0, r0, ip, lsr #32

0000000c <_start>:
   c:   e3a00001    mov r0, #1  ; 0x1
  10:   e1a00000    nop         (mov r0,r0)
  14:   e1a00000    nop         (mov r0,r0)
  18:   e24f1020    sub r1, pc, #32 ; 0x20
  1c:   e24f201c    sub r2, pc, #28 ; 0x1c
  20:   e1a00000    nop         (mov r0,r0)
  24:   e51f3004    ldr r3, [pc, #-4]   ; 28 <.text+0x28>
  28:   0000002c    andeq   r0, r0, ip, lsr #32

把反汇编的代码拿到keil里去仿真,结果与上面分析的一致

keil仿真结果
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 206,378评论 6 481
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 88,356评论 2 382
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 152,702评论 0 342
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 55,259评论 1 279
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 64,263评论 5 371
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 49,036评论 1 285
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 38,349评论 3 400
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,979评论 0 259
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 43,469评论 1 300
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,938评论 2 323
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,059评论 1 333
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,703评论 4 323
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,257评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,262评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,485评论 1 262
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 45,501评论 2 354
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,792评论 2 345

推荐阅读更多精彩内容