Andes Workshop

It is currently Thu Sep 19, 2019 5:27 am

All times are UTC + 8 hours [ DST ]




Post new topic Reply to topic  [ 5 posts ] 
Author Message
 Post subject: 將function指定在固定位址上的範例(20150107更新)
PostPosted: Thu Nov 03, 2011 10:22 pm 
Offline
User avatar

Joined: Fri Mar 04, 2011 9:36 pm
Posts: 500
將function指定在固定位址上的範例如附件。
重點有幾個:
(1)程式裡要指定function在某個section上。
int __attribute__ ((section (".FUNCTIONA"))) mul2(int);
//int mul2(int);

int main(void) {
mul2(30);
puts("Completed!");
return EXIT_SUCCESS;
}

int mul2(int num){
return 2*num;
}


(2) 要在linker script裡指定該section的位址
.my_section 0x550000 :
{
*(.FUNCTIONA)
}

我們可以看到function mul2在位址0x550000的地方。

Code:
Disassembly of section .my_section:

00550000 <mul2>:
  550000:   3b ff fe 3c    smw.adm $sp,[$sp],$sp,#0x8    ! {$fp}
  550004:   51 cf 80 00    addi $fp,$sp,#0
  550008:   51 ff ff f4    addi $sp,$sp,#-12
  55000c:   14 0e 7f fe    swi $r0,[$fp+#-0x8]
  550010:   04 0e 7f fe    lwi $r0,[$fp+#-0x8]
  550014:   40 00 00 00    add $r0,$r0,$r0
  550018:   51 fe 00 00    addi $sp,$fp,#0
  55001c:   3b ff fe 04    lmw.bim $sp,[$sp],$sp,#0x8    ! {$fp}
  550020:   4a 00 78 20    ret $lp


2015.1.7更新,前一版的寫法linker script裡的名字容易造成誤解,
這一版更新。
下面是sample code
Attachment:
func_addr.zip [67.36 KiB]
Downloaded 244 times


Last edited by cindy on Wed Nov 16, 2011 7:16 pm, edited 2 times in total.

Top
 Profile Send private message E-mail  
 
 Post subject: Re: 將function指定在固定位址上的範例
PostPosted: Fri Nov 04, 2011 10:39 am 
Offline
User avatar

Joined: Fri Mar 04, 2011 9:36 pm
Posts: 500
如果是.o檔要指定擺在某些sections的寫法,
請參考 http://www.math.utah.edu/docs/info/ld_3.html#SEC14

Linker script寫法如下:

filename
You may simply name a particular input file to be placed in the current output section; all sections from that file are placed in the current section definition. If the file name has already been mentioned in another section definition, with an explicit section name list, then only those sections which have not yet been allocated are used. To specify a list of particular files by name:
.data : { afile.o bfile.o cfile.o }


Top
 Profile Send private message E-mail  
 
 Post subject: Re: 將function指定在固定位址上的範例
PostPosted: Mon Nov 14, 2011 2:55 pm 
Offline

Joined: Mon Nov 14, 2011 1:30 pm
Posts: 1
不好意思,我有二個問題想問:
1. 我想要把某個.o(在此是jump.o)的.text放在sections,之後在放入其餘未放入的.text,寫法如下,可是compiler後會產生錯誤(請看附件的圖,2011-11-14_132605.png),Link Script 的片段程式如下(詳見附件,nds32.rar)
SECTIONS
{
/* Read-only sections, merged into text segment: */
PROVIDE (__executable_start = ORIGIN(ROM)); . = ORIGIN(ROM);
.nds32_init ORIGIN(ROM) : { KEEP(*(.nds32_init)) } > ROM AT> ROM

PROVIDE (__text_begin = .);
.text :
{
jump.o (.text)
* (.text .stub .text.* .gnu.linkonce.t.*)
/* .gnu.warning sections are handled specially by elf32.em. */
*( .gnu.warning)
} > ROM

2. 第二個問題是:* (.text .stub .text.* .gnu.linkonce.t.*)的 ".text.*",這寫法是什麼的意思?


Attachments:
nds32.rar [1.84 KiB]
Downloaded 729 times
2011-11-14_132605.png
2011-11-14_132605.png [ 19.07 KiB | Viewed 11195 times ]
Top
 Profile Send private message E-mail  
 
 Post subject: Re: 將function指定在固定位址上的範例
PostPosted: Mon Nov 14, 2011 7:11 pm 
Offline
User avatar

Joined: Fri Mar 04, 2011 9:36 pm
Posts: 500
(1)我已複製出您的問題。
如附件程式,編譯時會有相同的錯誤訊息。
Attachment:
test_linker.zip [21.71 KiB]
Downloaded 659 times


在這裡有gnu ld的說明
http://sourceware.org/binutils/docs/ld/ ... ion-Basics
在linker裡寫
Code:
*(.text)

表示所有.o檔的.text段放在這裡。

error message是重覆定義,在linker script中。
Code:
  .text           :
  {
    func1.o (.text)
    *(.text .stub .text.* .gnu.linkonce.t.*)
    KEEP (*(.text.*personality*))
    /* .gnu.warning sections are handled specially by elf32.em.  */
    *(.gnu.warning)
  } =0

去掉 func1.o (.text) 這一行即可。


(2)同樣是上一篇LD說明的連結裡,下面這個寫法可以加入多個sections。
There are two ways to include more than one section:
*(.text .rdata)
*(.text) *(.rdata)

.text.*
通常是 -ffunction-sections 造出來的東西,那個 ‘*’ 會擺 function name
man gcc 的結果:
-ffunction-sections
-fdata-sections
Place each function or data item into its own section in the output file if the target supports arbitrary sections.
The name of the function or the name of the data item determines the section's name in the output file.
--------更詳細的說明如下------------
gcc provides -ffunction-sections or
-fdata-sections option to put each function or data
to its own section, for instance, there is a function
called unused func(), it goes to .text.unused func
section

The following two figures demonstrates the differences
between the typical object and the one with
-ffunction-sections:
Attachment:
figure.gif
figure.gif [ 29.13 KiB | Viewed 11170 times ]

參考文章
Tiny Linux Kernel Project: Section Garbage Collection Patchset
(到google打入文章名稱可以找到pdf檔)


Top
 Profile Send private message E-mail  
 
 Post subject: Re: 將function指定在固定位址上的範例(20111116更新)
PostPosted: Fri Jul 13, 2012 6:03 pm 
Offline
User avatar

Joined: Fri Mar 04, 2011 9:36 pm
Posts: 500
Code:
MEMORY
{
  ROM (rwx) : ORIGIN = 0x1000, LENGTH = 0x1000
  RAM (rwx) : ORIGIN = 0x4000, LENGTH = 0x1000
}

SECTIONS
{
   .text : {
      foo.o (.text)
      * (.text)
   } AT>ROM
}



這個寫法是可行的,不過在Andesight裡要改成用手寫的makefile。

請看example
Attachment:
test_ld.zip [5.12 KiB]
Downloaded 621 times


Top
 Profile Send private message E-mail  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC + 8 hours [ DST ]


Who is online

Users browsing this forum: No registered users and 3 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group