C言語学習NO 5:_attribute__((section("section_name")))


目次
 
前言
一、コンパイル時に変数にセグメントを指定する
二、コンパイル時に関数にセグメントを指定する
三、帯電リセット後0がはっきりしない.noinit変数
前言
__attribute__(「section_name」)は、「section_name」という名前の対応するセグメントに作用する関数またはデータを入れる役割を果たす.
__attribute__主に宣言または定義された関数またはデータの特性を変更するために使用されます.これには、作用オブジェクトの特性を変更するためのサブアイテムがたくさんあります.たとえば、関数では、nolineはインライン拡張を禁止し、noreturnは戻り値がないことを示し、pureは関数が戻り値を除いて、グローバル変数、ポインタなどの他の関数の外部に影響を与えないことを示します.
一、コンパイル時に変数にセグメントを指定する
Example
/* in RO section */
const int descriptor[3] __attribute__ ((section ("descr"))) = { 1,2,3 };
/* in RW section */
long long rw[10] __attribute__ ((section ("RW")));
/* in ZI section *
long long altstack[10] __attribute__ ((section ("STACK"), zero_init));/

Note:This variable attribute is a GNU compiler extension supported by the ARM compiler.(MDK)
二、コンパイル時に関数にセグメントを指定する
Example
In the following example, Function_Attributes_section_0 is placed into the RO section new_section rather than .text.
 
void Function_Attributes_section_0 (void)
    __attribute__ ((section ("new_section")));
void Function_Attributes_section_0 (void)
{
    static int aStatic =0;
    aStatic++;
}



In the following example, section function attribute overrides the #pragma arm section setting.
 
#pragma arm section code="foo"
  int f2()
  {
      return 1;
  }                                  // into the 'foo' area
  __attribute__ ((section ("bar"))) int f3()
  {
      return 1;
  }                                  // into the 'bar' area
  int f4()
  {
      return 1;
  }                                  // into the 'foo' area
#pragma arm section

三、帯電リセット後0がはっきりしない.noinit変数
.bssセグメントの変数はチップで起動する.init 4段階は0でクリアするが、定義は.bssのnoinitセグメントの変数は.Init 4フェーズはクリアされません.単片機が電気を落とさずにリセットすると、noinitセグメントのこれらの変数の値は依然として存在し、引き続き使用できます.
.noinit      :int bar __attribute__ ((section (".noinit")));
      ,        .bss 。

(検証:.noinitセグメント変数を定義し、開発ボードのリセットキーを押すと(単片機は電気を落とさずにリセット)、この変数の値は0にクリアされません.)