4
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

gccでCのソースコードからセクションのアドレスを参照する。

Last updated at Posted at 2016-08-27

gccでCのソースコードから、リンカスクリプトで定義するセクションのアドレスを参照する手順を調べました。

gccでBSS領域のクリアをするコードを書く場合に、セクションの開始アドレスと終了アドレスを参照する必要があります。

gccでセクションのアドレスを参照する

リンカスクリプトの例
BSSの最初と最後にラベルを配置します。 .はカレント位置のアドレスを示します。

MEMORY
{
  ROM(rxai) : ORIGIN = 0, LENGTH = 64k
  RAM(wxai) : ORIGIN = 0x00ffe000, LENGTH = 4k
}

SECTIONS
{
  .text : {} > ROM
  .rodata : {} > ROM

  .data : {} > RAM
  . = ALIGN(4);	
  __bss_start = .;
  .bss : {} > RAM
  __bss_end = .;
}

Cのソースコードから、上記の__bss_start と __bss_endのアドレスを参照したい場合はこのように書きます。

extern const void * __bss_start;
extern const void * __bss_end;

void clear_bss(void)
{
    /* clear BSS */
    memset( &__bss_start, 0x00, (size_t) (&__bss_end - &__bss_start));
}

コンパイル時にリンカスクリプトを指定します

arm-eabi-none-gcc -o target.elf -Ttarget.ld  main.c
  • リンカスクリプトにある名前でextern const のポインタ変数を定義する
  • ポインタ変数のアドレスを参照するコードを書く。
  • リンカがリンク時にポインタ変数の名前からアドレスを解決してくれる。

参考

4
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?