[Linker] GNU linker LD 사용하기 – Linker Scripts (.ld)

그냥 내용이나 조금 보려고 했는데, 보다보니 번역을 하고 있다.. 왜지…..

찾아보니 korea.gnu.org에 이미 번역된 문서(http://korea.gnu.org/manual/release/ld/ld-mahajjh/ld_3.html#SEC6)가 있다. 내건 부끄러우니 이걸 보도록 하자.

이 문서는 “GNU Free Documentation License”를 따른다.

 

Linker Scripts

원문: https://sourceware.org/binutils/docs/ld/Scripts.html#Scripts

번역: 양정석 (dasomoli@gmailREMOVETHIS.com)

모든 link는 linker script로 제어된다. 이 스크립트는 linker script language로 쓰여진다.

linker script의 주목적은 input file 내의 section을 output file로 어떻게 map해야 하는지, 그리고 output file의 memory layout을 어떻게 해야 하는지 설명하는 거다. 대부분의 linker script는 이 이상을 하지 않는다. 그러나 필요하면 linker script는 linker에게 아래에 설명된 command들을 사용해서 다른 동작을 수행할 수 있다.

linker는 언제나 linker script를 사용한다. 제공되지 않으면 linker는 linker executable 내부에 compile된 default script를 사용한다. '--verbose' command line option을 사용해서 default linker script를 볼 수 있다. '-r' 이나 '-N' 같은 command line option은 default linker script에 영향을 준다.

자신만의 linker script를 '-T' command line option을 사용해서 줄 수 있다. 이렇게 하면 그 linker script가 default linker script를 대신한다.

또한, linker에게 그것들이 link될 파일들이라도 input 파일로 그들을 이름 붙여서 암묵적으로 linker scripts를 사용할 수도 있다. Implicit Linker Scripts를 보라.

Linker Script 기본 개념

linker script language를 설명하기 위해서 기본적인 개념과 용어를 define할 필요가 있다.

링커는 input file들을 하나의 output file로 합친다. output file과 각 input file은 object file format이라고 알려진 special data format으로 되어 있다. 그래서 각 파일을 object file이라고 부른다. output file은 executable이라고 자주 부르지만, 우리의 목적에 따라 object file이라고도 부를거다. object file은 무엇보다도 sections들의 리스트를 가진다. input file 안에 있는 section을 input section이라고 하고, output file 안에 있는 section을 output section이라고 한다.

각 object file 안의 section은 하나의 name과 하나의 size를 갖는다. 거의 모든 섹션은 section contents라고 하는 data의 associated block도 갖는다. loadable로 표시된 section은 output file이 실행될 때 memory로 load된다. content가 없는 section은 allocatable일 수 있는데 이는 메모리는 set되지만 거기에 특별한 게 load되지는 않는다(특정 경우에 이 메모리는 0으로 초기화된다)는 뜻이다. loadable도 allocatable도 아닌 섹션은 일반적으로 디버깅 정보를 갖는다.

모든 loadable 또는 allocatable output section은 두가지 address를 갖는다. 먼저 VMA(virtual memory address)는 output file이 실행될 때 갖을 address이다. 다음으로 LMA(load memory address)는 그 section이 load될 address이다. 대부분의 경우에 두 address는 같다. 다를 수 있는 예는 data section이 ROM에 load되고 program이 시작될 때 RAM으로 복사될 때이다(이 테크닉은 ROM based system에서 global variables을 초기화할 때 자주 쓰인다). 이 경우 ROM address는 LMA가 되고, RAM address는 VMA가 될 것이다.

objdump프로그램의 '-h' 옵션으로 object file의 section들을 볼 수 있다.

모든 object file은 symbol table이라고 부르는 symbols의 list를 갖는다. symbol은 defined 이거나 undefined일 수 있다. 각 symbol은 이름을 갖고, 각 defined symbol은 다른 정보들보다 address를 갖는다. C/C++을 object file로 컴파일하면 모든 defined function과 global/static variable의 defined symbol을 얻게 된다. input file에서 참조되는 모든 undefined function과 global variable은 undefined symbol이 될 거다.

nm이나 objdump 프로그램의  '-t' option로 object file의 symbol들을 볼 수 있다.

Linker Script 형식

Linker script는 text file이다.

command를 연결해서 linker script를 작성한다. 각 command는 argument가 붙을 수 있는 keyword이거나 또는 symbol로의 assignment 둘 중 하나다. ';'으로 각 command를 구분할 수 있다. white space는 일반적으로 무시된다.

file이나 format name같은 string은 직접 들어갈 수 있다. file name에 다른 file name을 구별할 때 쓰이는 ','같은 문자가 포함되어 있으면, ""로 감쌀 수 있다. “”를 파일 이름으로 쓸 수 있는 방법은 없다.

C처럼 linker script안에 '/*', '*/'로 comment를 달 수도 있다. C에서처럼 comment는 문법적으로 whitespace와 동등하다.

간단한 Linker Script 예제

많은 linker script들은 정말 간단하다.

가능한 가장 간단한 linker script는 딱 하나의 'SECTIONS' command를 갖는 것이다. 'SECTIONS' command는 output file의 memory layout을 설명하는데 쓰인다.

'SECTIONS' command는 강력한 command이다. 여기서 우리는 가장 간단히 사용하는 것을 설명할거다. 여러분의 프로그램이 code, initialized data, uninitialized data로만 구성되어 있다고 하면 거기에는 각각 '.text', '.data', '.bss' section이 있게 된다. 이에 더해서 여러분의 input files안에 있는 section들만 있다고 하자.

이 예제를 위해 그 code가 address 0x10000에 load되어야 하고, 그 data는 address 0x8000000에서 시작되어야 한다고 하자.

아래가 이를 위한 link script이다.

SECTIONS
{
  . = 0x10000;
  .text : { *(.text) }
  . = 0x8000000;
  .data : { *(.data) }
  .bss : { *(.bss) }
}

'SECTIONS' command는 'SECTIONS' 키워드, 그 뒤에 symbol assignments과 output section 설명을 ‘{‘, ‘}’로 감싸서 쓴다.

위 예제의 첫 줄은 location counter인 special symbol ‘.’을 지정한다. 다른 방법(이건 뒤에 설명)으로 output section의 주소를 지정하지 않으면, location counter의 현재 값으로부터 그 address가 지정된다. 그러고 나면 location counter는 output section의 size만큼 더해진다. 'SECTIONS' command를 시작할 때  location counter는 값 '0'을 갖는다.

두번째 줄은 output section ‘.text’를 define한다. ':'이 필요하다. '{', '}' 안에, output section name 뒤에 이 output section에 두어야 할 input section들의 이름을 나열한다. '*'는 모든 파일 이름에 매치되는 wildcard이다. 표현식 '*(.text)' 은 모든 input file들 안의 '.text' input section을 뜻한다.

location counter가 '0x10000'이었기 때문에, output section '.text'가 defined될 때, linker는 output file내의 '.text' section의 주소를 '0x10000'으로 지정할 거다.

뒤의 줄들은 output file의 '.data''.bss'를 define한다. linker는 '.data' output section을 '0x8000000'에 둔다. 그러고나면 location counter는 '0x8000000' + '.data' output section의 size가 된다. 그 결과 linker는 '.bss' output section을 메모리 내에서 '.data' output section 바로 뒤에 두게 된다.

linker는 필요하다면 location counter를 더해서 각각의 output section에 필요한 alignment를 맞춘다. 이 예제에서는 '.text''.data' section들의 지정된 주소들은 아마 alignment constraint를 만족할 것이고 linker는 '.data''.bss' section 사이에 작은 갭을 만들어야 했을 수 있다.

이게 다다. 간단하고 완전한 하나의 linker script다.

간단한 Linker Script Command

간단한 linker script command들을 설명한다.

  • Entry Point: entry point를 설정
  • File Command들: 파일을 다루는 command들
  • Format Command들: object file format을 다루는 command들
  • REGION ALIAS: memory region에 별칭 붙이기
  • 기타 Command들: 다른 linker script command들

Entry point 설정

프로그램 내의 실행되는 첫번째 instruction을 entry point라고 부른다. ENTRY linker script command로 이 entry point를 설정할 수 있다. 그 argument는 symbol name이다:

ENTRY(symbol)

entry point를 설정하는 여러 방법이 있다. linker는 다음 방법들을 순서대로 실행하면서 entry point를 설정하고, 그 중 하나가 성공하면 멈춘다:

  • '-e' entry command-line 옵션;
  • linker script 안의 ENTRY(symbol) command;
  • defined되면, target-specific symbol의 값; 많은 target들에 이는 start지만, 예를 들면 PE- 그리고 BeOS-based system은 가능한 entry symbols의 list를 check해서 처음 맞는 것을 찾는다.
  • '.text' section이 있다면 그 첫 byte의 address;
  • address 0.

파일을 다루는 command들

여러 linker script command들이 파일을 다룬다.

INCLUDE filename

linker script filename 을 이 지점에 include한다. 현재 디렉토리에서 찾고 나서 -L 옵션으로 지정된 디렉토리 내에서 찾는다. INCLUDE는 10 레벨까지 중첩 호출이 가능하다.

MEMORY 또는 SECTIONS commands 안에, 또는 output section descriptions 안에, top level에서 INCLUDE 지시자를 둘 수 있다.

INPUT(filefile, ...)

INPUT(file file ...)

INPUT command 는 linker에게 그 link 내에 command line 상에서 이름을 정한 것처럼 이름 붙인 file들을 include하도록 지시한다.

예를 들어, 'subr.o'를 link할 때마다 include하고 싶지만 매번 command line에 이를 입력하고 싶지는 않을 때 'INPUT (subr.o)'를 linker script 내에 적을 수 있다.

사실 원한다면 linker script 안에 모든 input files를 적어두고, linker를 아무 것도 없이 '-T' 옵션으로 실행할 수 있다.

sysroot prefix가 설정되어 있고, 그 filename이 '/' character로 시작하고, 처리될 그 script가 sysroot prefix 내부에 있는 경우, 그 filename을 sysroot prefix 내에서 찾을 거다. 아니라면, linker는 먼저 현재 디렉토리 내에서 그 파일을 열 수 있는지 시도해보고, 없으면, archive library search path를 통해 찾는다. 또한 sysroot prefix는 filename path안의 첫번째 character로 = 를 지정하거나 $SYSROOT를 filename path 앞에 붙임으로써 강제할 수 있다. Command Line Options section 내의 '-L' 의 설명을 한번 보라.

'INPUT (-lfile)'을 쓰면 ld 는 command line argument '-l'로 한 것 처럼 libfile.a로 그 이름을 바꾼다.

implicit linker script 내에서 INPUT command를 사용하면, 그 파일들은 그 link상의 linker script file이 included된 그 지점에서 include될 것이다. 이는 archive searching에 영향을 준다.

GROUP(filefile, ...)

GROUP(file file ...)

GROUP command는 named file들이 모두 archive되어야만 한다는 것, 그리고 새로운 undefined reference가 만들어지기 전까지 반복적으로 찾아진다는 것을 제외하면 INPUT과 비슷하다. Command Line Options section 내의 '-(' 의 설명을 보라.

AS_NEEDED(filefile, …)

AS_NEEDED(file file …)

이 construct는 INPUT이나 GROUP commands 안에서만 다른 filenames와 나올 수 있다. 나열된 files들은 ELF shared libraries의 그들이 진짜 필요할 때만 추가되는 예외를 제외하면, 그 INPUT이나 GROUP commands 안에 직접 나온 것처럼 처리될 거다. 이 construct는 본질적으로 그 안에 나열된 모든 files들에 대해 --as-needed 옵션을 enable하고, --no-as-needed 셋팅 이후에 이전의 --as-needed를 각각 복구한다.

OUTPUT(filename)

OUTPUT command는 output file의 이름을 정한다. linker script 안에서 OUTPUT(filename) 을 사용하는 것은 command line (Command Line Options section을 보라) 상에서 '-o filename' 을 사용하는 것과 정확히 같다. 둘 다 사용되면, command line option이 더 우선된다.

OUTPUT command는 보통 기본 값인 'a.out' 말고 다른 file을 기본 값으로 define하려고 할 때 사용할 수 있다.

SEARCH_DIR(path)

SEARCH_DIR command는 ld가 library들을 archive할 때 보는 경로의 리스트에 path를 추가한다. SEARCH_DIR(path)를 사용하는 것은 command line (Command Line Options section을 보라) 상에서 '-L path'를 사용하는 것과 정확히 같다. 둘 다 사용되면, linker는 두 path 모두 찾는다. command line option에서 지정된 path를 먼저 찾는다.

STARTUP(filename)

STARTUP command는 command line 상에서 먼저 정해졌던 것처럼 그 filename이 link될 첫 input file이 되는 것을 제외하면 INPUT command와 똑같다. 이는 entry point가 언제나 첫번째 file의 시작인 시스템을 사용할 때 유용할 거다.

Object file format을 다루는 command들

두 linker script command가 object file format을 다룬다.

OUTPUT_FORMAT(bfdname)

OUTPUT_FORMAT(defaultbiglittle)

OUTPUT_FORMAT command는 output file을 위해 사용하는 BFD format을 지정한다. (BFD를 보라). OUTPUT_FORMAT(bfdname)은 command line 상의 '--oformat bfdname'(Command-line Options를 보라)을 사용하는 것과 정확히 같다. 둘 다 사용되면, command line option이 우선된다.

OUTPUT_FORMAT'-EB''-EL' command-line options에 기초하는 다른 foramts을 사용하도록 세 개의 arguments로 사용할 수 있다. 이는 linker script가 원하는 endianness 상의 output format으로 set되도록 한다.

'-EB' 또는 '-EL' 둘 다 사용되지 않으면, output format은 첫번째 argument인 default가 될 거다. '-EB'가 사용되면 output format은 두번째 argument인 big이 될 거다. '-EL'이 사용되면 output format은 세번째 argument인 little이 될거다.

예를 들어, MIPS ELF target의 default linker script는 다음 command를 사용한다:

OUTPUT_FORMAT(elf32-bigmips, elf32-bigmips, elf32-littlemips)

이는 그 output file을 위한 그 default format이 'elf32-bigmips'지만, 사용자가 '-EL' command-line option을 사용하면 그 output file은 'elf32-littlemips' format으로 만들어질 것을 이야기한다.

TARGET(bfdname)

TARGET command는 input file을 읽을 때 사용하는 BFD format의 이름을 지정한다. 이는 이어지는 INPUTGROUP commands에 영향을 미친다. 이 command는 command line 상의 '-b bfdname'(Command-line Options를 보라)의 사용과 같다. TARGET command가 사용되지만 OUTPUT_FORMAT이 없으면, 마지막 TARGET command가 그 output file을 위한 format을 set하는데도 사용된다. BFD를 보라.

memory region에 별칭 붙이기

MEMORY command로 생성한 existing memory regions에 별칭을 붙일 수 있다. 각 이름은 하나의 memory region에만 해당한다.

REGION_ALIAS(alias, region)

REGION_ALIAS function은 memory region region에 별칭 alias를 만든다. 이는 memory regions로의 유연한 output section의 mapping을 할 수 있도록 한다. 다음에 예제가 나온다.

다양한 memory storage devices를 갖고 있는 embedded system을 위한 application이 있다고 가정하다. 뭐든 code 실행 또는 data 저장을 할 수 있는 일반 목적의 volatile memory RAM을 갖는다. 어떤 것은 code 실행과 read-only data access를 할 수 있는 read-only, non-volatile memory ROM을 갖을 수 있다. 마지막 variant는 read-only data access와 code 실행을 할 수 없는 read-only, non-volatile memory ROM2이다. 우리는 4가지 output section을 갖는다:

  • .text program code:
  • .rodata read-only data;
  • .data read-write initialized data;
  • .bss read-write zero initialized data.

목적은 output section을 정의하는 system independent한 부분과 그 시스템 상의 available한 memory regions로 output sections를 mapping하는 system dependent한 부분을 포함하는 linker command file을 제공하는 것이다. 우리의 embedded system은 3가지 다른 memory setups A, B 그리고 C로 된다.

Section Variant A Variant B Variant C
.text RAM ROM ROM
.rodata RAM ROM ROM2
.data RAM RAM/ROM RAM/ROM2
.bss RAM RAM RAM

RAM/ROM 이나 RAM/ROM2 표기는 이 section이 region ROM 이나 region ROM2로 각각 loaded된다는 뜻이다. .data section의 load address가 .rodata section의 끝에서 모든 세가지 variants가 시작함에 주의하라.

base linker script는 아래의 output section으로 처리한다. memory layout을 describe하는system dependent한 linkcmds.memory file을 포함한다.

INCLUDE linkcmds.memory

SECTIONS
  {
    .text :
      {
        *(.text)
      } > REGION_TEXT
    .rodata :
      {
        *(.rodata)
        rodata_end = .;
      } > REGION_RODATA
    .data : AT (rodata_end)
      {
        data_start = .;
        *(.data)
      } > REGION_DATA
    data_size = SIZEOF(.data);
    data_load_start = LOADADDR(.data);
    .bss :
      {
        *(.bss)
      } > REGION_BSS
  }

이제 memory regions들과 별칭을 define하는 세가지 다른 linkcmds.memory file이 필요하다. 세가지 다른 variants A, B C를 위한 linkcmds.memory의 내용은 다음과 같다:

A

여기선 모두 RAM내로 간다.

MEMORY
  {
    RAM : ORIGIN = 0, LENGTH = 4M
  }

REGION_ALIAS("REGION_TEXT", RAM);
REGION_ALIAS("REGION_RODATA", RAM);
REGION_ALIAS("REGION_DATA", RAM);
REGION_ALIAS("REGION_BSS", RAM);

 

B

Program code와 read-only data는 ROM내로 간다. Read-write data는 RAM내로 간다. initialized data의 image는 ROM내에 load되고 system이 시작되는 동안 RAM내로 copy될 거다.

MEMORY
  {
    ROM : ORIGIN = 0, LENGTH = 3M
    RAM : ORIGIN = 0x10000000, LENGTH = 1M
  }

REGION_ALIAS("REGION_TEXT", ROM);
REGION_ALIAS("REGION_RODATA", ROM);
REGION_ALIAS("REGION_DATA", RAM);
REGION_ALIAS("REGION_BSS", RAM);

 

C

Program code는 ROM내로 간다. Read-only data는 ROM2내로 간다. Read-write data는 RAM내로 간다. initialized data는 ROM2내에 load되고, system이 start하는 동안 RAM내로 copy될 거다..

MEMORY
  {
    ROM : ORIGIN = 0, LENGTH = 2M
    ROM2 : ORIGIN = 0x10000000, LENGTH = 1M
    RAM : ORIGIN = 0x20000000, LENGTH = 1M
  }

REGION_ALIAS("REGION_TEXT", ROM);
REGION_ALIAS("REGION_RODATA", ROM2);
REGION_ALIAS("REGION_DATA", RAM);
REGION_ALIAS("REGION_BSS", RAM);

 

필요하다면, ROM이나 ROM2로부터 RAM내로 .data section을 복사하는 common system initialization routine을 작성할 수 있다.

#include <string.h>

extern char data_start [];
extern char data_size [];
extern char data_load_start [];

void copy_data(void)
{
  if (data_start != data_load_start)
    {
      memcpy(data_start, data_load_start, (size_t) data_size);
    }
}

다른 linker script command들

다른 linker script command들이 몇 있다.

ASSERT(expmessage)

exp 가 0이 아님을 확인한다. 0이라면, linker가 error code와 message를 찍으면서 종료된다.

모든 assertions들은 linking의 final stage가 시작되기 전에 checked된다. 이는 section definitions 내에 symbols PROVIDE와 관련된 expression은 user가 이들 symbols의 값을 set하지 않았다면 fail됨을 의미한다. 이 rule의 단 하나의 예외는 .을 reference하는 PROVIDE symbol이다. 그래서 다음과 같은 assertion은 __stack_size가 다른 곳에 defined되지 않았다면 실패한다:

  .stack :
  {
    PROVIDE (__stack = .);
    PROVIDE (__stack_size = 0x100);
    ASSERT ((__stack > (_end + __stack_size)), "Error: No room left for the stack");
  }

section definitions 밖의 symbols PROVIDE는 미리 evaluated되므로 ASSERT 안에서 사용될 수 있다. 그래서 다음은 동작한다:

  PROVIDE (__stack_size = 0x100);
  .stack :
  {
    PROVIDE (__stack = .);
    ASSERT ((__stack > (_end + __stack_size)), "Error: No room left for the stack");
  }

EXTERN(symbol symbol ...)

output file에 들어갈 symbol을 undefined symbol로 강제한다. 예를 들면 이는 standard library들로부터 추가 모듈들의 linking을 trigger할 수 있다. 각 EXTERN마다 여러 symbol들을 나열할 수 있고, EXTERN은 여러 번 사용할 수 있다. 이 command는 '-u' command-line option과 같은 효과를 갖는다.

FORCE_COMMON_ALLOCATION

이 command는 '-d' command-line option과 같은 효과를 갖는다. relocatable output file이 지정 ('-r') 되었다 하더라도, common symbol들에 ld가 space를 assign하도록 만든다.

INHIBIT_COMMON_ALLOCATION

이 command는 ‘–no-define-common’ command-line option과 같은 효과를 갖는다: ld가 non-relocatable output file조차 common symbols로의 address assignment를 생략하게 만든다.

FORCE_GROUP_ALLOCATION

이 command는 ‘–force-group-allocation’ command-line option과 같은 효과를 갖는다: ld가 보통 input sections같은 members를 section group으로 두도록, 그리고 relocatable output file이 지정 ('-r') 되었다 하더라도 그 section groups를 delete하도록 만든다.

INSERT [ AFTER | BEFORE ] output_section

이 command는 일반적으로 '-T' argument로 default SECTIONS으로 예를 들면 overlays로 지정하는 script내에서 사용된다. 이는 output section 후에 (또는 전에) 모두에 우선하는 linker script statements를 삽입하고, 또한 default linker script를 override하지 않도록 '-T'를 야기한다. 정확한 삽입 지점은 이름 없는 sections을 위한 것과 같다. Location Counter를 보라. 그 삽입은 linker가 output sections들로 input sections들을 mapped한 후에 일어난다. 삽입 전에 ‘-T’ scripts는 default linker script 전에 parsed되고, ‘-T’ script 내의 statements들이 default linker script statements 전에 그 script의 internal linker representation 내에서 일어난다. 특별히 input section assignments는 default script 내의 그것들 전에 ‘-T’ output sections로 만들어질 거다. 여기 ‘-T’ script가 INSERT를 어떻게 사용하는지 볼 수 있는 예제다:

SECTIONS
{
  OVERLAY :
  {
    .ov1 { ov1*(.text) }
    .ov2 { ov2*(.text) }
  }
}
INSERT AFTER .text;

NOCROSSREFS(section section ...)

이 command는 ld에게 어떤 output section들 사이의 모든 reference들에 관한 error를 올리라고 말하는데 사용될 수 있다.

어떤 종류의 program에서, 특히 임베디드 시스템 상에서 overlay가 사용될 때, 한 section이 메모리로 load되면, 다른 section은 아닐 것이다. 두 section 사이의 모든 direct reference는 error가 된다. 예를 들어, 한 section의 code가 다른 section에 defined된 함수를 호출한다면 에러일 거다.

NOCROSSREF command는 output section 이름의 list를 가져온다. ld가 section들 사이에 어떤 cross references가 발견하면 error를 보고하고, 0이 아닌 exit status로 return한다. NOCROSSREF command는 input section name이 아닌 output section name을 사용한다.

NOCROSSREFS_TO(tosection fromsection …)

이 command는 ld에게 다른 sections의 list로부터 한 section으로의 어떠한 reference라도 error로 올리도록 이야기할 때 쓸 수 있다.

NOCROSSREFS command는 둘 이상의 output sections이 전부 independent하지만 한 방향의 de[endency가 필요한 경우를 보장할 때 유용하다. 예를 들어 multi-core application에서 각 core로부터 호출될 수 있는 code를 공유하지만 안전을 위해 call back을 사용되지 않아야 할 수 있다.

NOCRESSREFS_TO command는 output section names의 list를 취한다. 첫번째 section은 다른 어떤 sections로부터도 referenced될 수 없다. ld가 다른 어떤 sections으로부터 첫번째 section으로의 어떤 references라도 탐지하면, error를 report하고 non-zero exist status를 return한다. NOCRESSREFS_TO command는 input section names가 아닌, output section names를 사용함을 주의하라.

OUTPUT_ARCH(bfdarch)

특정 output machine architecture를 지정한다. argument는 BFD library (BFD section을 보라) 에 의해 사용되는 이름 중 하나다. objdump'-f' 옵션을 사용해서 object file의 architecture를 볼 수 있다.

LD_FEATURE(string)

이 command는 ld의 behavior를 변경하도록 만들 수 있다. string이 “SANE_EXPR”이면 script 내의 absolute symbols와 numbers는 모든 곳에서 간단히 numbers로 취급된다. Expression Section절을 보라.

Symbol에 값 assign하기

linker script 안에서 symbol에 값을 assign할 수 있다. 이는 symbol을 global symbol로 define한다.

간단한 assignment

C assignment operators의 하나를 사용해서 symbol을 assign할 수 있다.

symbol = expression ;

symbol += expression ;

symbol -= expression ;

symbol *= expression ;

symbol /= expression ;

symbol <<= expression ;

symbol >>= expression ;

symbol &= expression ;

symbol |= expression ;

첫번째 경우는 expression의 값으로 symbol을 define한다. 다른 모든 경우는, symbol이 이미 defined되어 있어야 하고, 값이 그에 따라 조정될 것이다.

special symbol name '.' 은 location counter를 나타낸다. SECTIONS command 내에서만 이를 사용할 거다.

expression 뒤에 ';'을 꼭 써야 한다.

Expression들은 아래에 defined되어 있다. Linker Script내의 expression 절을 보라.

symbol assignment는 command들의 오른쪽에 command처럼, SECTIONS command 내의 statement처럼, SECTIONS command 내의 output section description의 일부처럼 작성될 수 있다.

symbol의 section은 그 expression의 section으로부터 정해진다. Expression의 Section 절을 보라.

여기 symbol assignment가 사용될 수 있는 세가지 다른 곳을 보여주는 예제가 있다.

floating_point = 0;
SECTIONS
{
  .text :
    {
      *(.text)
      _etext = .;
    }
  _bdata = (. + 3) & ~ 4;
  .data : { *(.data) }
}

이 예제에서 symbol 'floating_point'는 0으로 defined될 것이다. symbol '_etext'는 ‘.text’ input section 뒤의 address로 defined될 것이다. symbol '_bdata'는 ‘.text’ output section 뒤에 4 byte boundary로 올려서 aligned된 address로 defined될 것이다.

HIDDEN

ELF target의 포팅을 위해, 숨겨지고, export되지 않는 symbol을 define하라. syntax는 HIDDEN(symbol = expression) 이다.

여기 간단한 Assignments로부터 HIDDEN을 사용해서 재작성한 예제다:

HIDDEN(floating_point = 0);
SECTIONS
{
  .text :
    {
      *(.text)
      HIDDEN(_etext = .);
    }
  HIDDEN(_bdata = (. + 3) & ~ 3);
  .data : { *(.data) }
}

이 경우 세 symbol 중 어떤 것도 이 모듈 밖에서 보이지 않을 거다.

PROVIDE

어떤 경우에 어떤 symbol이 reference되긴 하지만 그 link에 포함된 어떤 object에도 defined되어 있지 않을 때 linker script가 그 symbol만 define하고 싶을 수 있다. 예를 들면, symbol 'etext'가 defined된 traditional linker들 말이다. 그러나 ANSI C는 error 없이 'etext'를 function name처럼 사용할 수 있어야 함을 요구한다. PROVIDE keyword는 reference되지만, defined되지는 않는 'etext'같은 symbol을 define하는데 사용될 수 있다. syntax는 PROVIDE(symbol = expression) 이다.

여기 'etext'를 define하는 PROVIDE 사용 예제다.

SECTIONS
{
  .text :
    {
      *(.text)
      _etext = .;
      PROVIDE(etext = .);
    }
}

이 예제에서 프로그램이 '_etext'를 (앞에 _를 붙여서) define한다면, linker는 multiple definition error가 날 거다. 다르게 말하면, 그 프로그램이 'etext'를 (앞에 _를 붙이지 않고) define한다면, linker는 프로그램에서 그 definition을 조용히 사용할 것이다. program이 'etext'를 reference하긴 하지만, define하지는 않았다면, linker는 그 linker script 안의 definition을 사용할 것이다.

주의 – PROVIDE directive는 그런 symbol이 PROVIDE가 만드는 symbol과 combined될 수 있다하더라도 defined되는 common symbol로 취급된다. 이는 common symbols로 자주 defined되는 '__CTOR_LIST__'같은 constructor와 destructor list symbols를 고려할 때 특히 중요하다.

PROVIDE_HIDDEN

PROVIDE와 비슷하다. ELF target의 포팅을 위해, 그 symbol은 숨겨지고, export되지 않을 거다.

SECTIONS command

SECTIONS command는 linker에게 input section을 어떻게 output sections으로 map할지, 그리고 output section을 메모리 내에 어떻게 둘지를 알린다.

SECTIONS command의 형식은 다음과 같다.

SECTIONS
{
  sections-command
  sections-command
  ...
}

sections-command는 다음 중 하나가 될 수 있다.

  • ENTRY command (entry point 설정하기 절을 보라)
  • symbol assignment (Symbol에 값 assign하기)
  • output section description
  • overlay description

ENTRY command와 symbol assignment는 편의를 위해 이들 command들 내에 location counter를 사용해서 SECTIONS command 내에 있을 수 있다. 이는 또한 output file의 layout 내에서 의미 있는 지점에서 이들 command를 사용할 수 있기 때문에 linker script를 이해하기 쉽게 만들어 준다.

Output section description과 overlay description은 아래에서 설명한다.

linker script에서 SECTIONS command를 사용하지 않으면, linker는 각 input section을 input file에서 처음 나오는 section 순서대로 이름이 같은 output section내에 둘 것이다. 예를 들어, 첫번째 파일 내에 모든 input section들이 있으면 output file 내의 section들의 순서는 첫번째 파일 내의 순서와 같을 거다. 첫 section은 address 0에 있을 것이다.

Output section description

output section의 전체 description은 다음과 같다.

section [address] [(type)] : [AT(lma)]
  {
    output-section-command
    output-section-command
    ...
  } [>region] [:phdr :phdr ...] [=fillexp]

대부분의 output section은 거의 대부분의 output section attribute를 사용하지 않는다.

section 앞 뒤로 whitespace가 있어야 하므로 section 이름은 모호하지 않게 된다. ‘:’과 ‘{‘, ‘}’ 또한 있어야 한다. 줄을 바꾸거나 다른 white space는 맘대로 하면 된다.

output-section-command는 다음 중 하나일 수 있다.

  • symbol assignment (Symbol에 값 assign하기 절을 보라)
  • input section description (Input section description 절을 보라)
  • 직접 포함하는 data 값 (Output section data 절을 보라)
  • special output section keyword (Output section keyword 절을 보라)

Output section 이름

output section의 이름은 section이다. section은 output format에 따라 제약 사항을 만족해야 한다. 적은 수의 section만을 지원하는 format에서는 a.out같은 이름이 그 format에 의해 지원되는 이름 중의 하나여야 한다(예를 들어, a.out'.text', '.data' 또는 '.bss'만 허용한다). output format이 (Oasys 처럼) 이름이 아닌 숫자로만 모든 section을 지원하면 그 이름은 따옴표로 둘러싼 숫자로된 string으로만 넣어야 한다. section 이름은 문자들로 아무렇게나 구성될 수 있지만 ‘,’같은 일반적인 문자가 있는 이름은 따옴표로 감싸야 한다.

output section 이름 '/DISCARD/'는 좀 특별하다; Output section 폐기 절 참고

Output section address

address는 output section의 VMA (virtual memory address) 에 대한 expression이다. address를 주지 않으면, linker는 region이 있다면 이를 기준으로 설정하고, 아니면 location counter의 현재 값을 기준으로 설정한다.

address를 주면 output section의 address는 그걸로 정확히 설정된다. addressregion 둘 다 주지 않으면, output section의 address는 location counter의 현재 값을 output section의 alignment 요구사항에 맞춰 align된 값으로 설정된다. output section의 alignment 요구사항은 output section 내에 포함된 모든 input section 중 가장 엄격한 alignment가 적용된다.

예를 들어,

.text . : { *(.text) }

.text : { *(.text) }

는 미묘하게 다르다. 첫번째 것은 '.text' output section의 address를 location counter의 현재 값으로 설정할 것이다. 두번째 것은 location counter의 현재 값이 '.text' input section의 가장 엄격한 alignment로 aligned된 값으로 설정된다.

address는 임의의 expression이 될 수 있다; Linker Script내의 Expression 절을 참고. 예를 들어, 0x10 boundary 상에 section을 align하길 원하면, 그 section address의 마지막 4 비트는 0이 되고, 다음처럼 하면 된다.

.text ALIGN(0x10) : { *(.text) }

이는 ALIGN이 현재 location counter를 지정된 값으로 올려서 aligned해서 return하므로 잘 동작한다.

section에 address를 지정하는 것은 location counter의 값을 바꾼다.

Input section description

가장 일반적인 output section command는 input section description이다.

input section description은 가장 기본적인 linker script operation이다. linker에게 메모리 내에 프로그램을 어떻게 lay out할지를 이야기하기 위해 output sections을 사용한다. linker에게 memory layout내로 input file들을 어떻게 map할지를 이야기하기 위해 input section description을 사용한다.

Input section 기본

input section description은 file name으로 구성된다. file name 뒤에는 괄호로 둘러싸인 section name들의 list가 option으로 붙을 수 있다.

file name과 section name은 아래에서 더 설명될 wildcard pattern(Input section wildcard pattern)이 될 수 있다.

가장 일반적인 input section description은 모든 input section을 output section내의 특정 name으로 포함시키는 것이다. 예를 들어, 모든 input '.text' section을 포함시키려면, 아래처럼 쓸 수 있다:

*(.text)

여기서 '*'은 모든 file name에 매치되는 wildcard이다.

둘 이상의 section을 포함하는 두가지 방법이 있다.

*(.text .rdata)
*(.text) *(.rdata)

이 둘의 차이점은 '.text''.rdata' input section이 output section에서 나타날 순서다. 첫번째 예제에서는 이들이 섞인다. 두번째 예제에서는 모든 '.text' input section이 먼저 나타나고 그 다음에 모든 '.rdata' input section들이 뒤따른다.

특정 파일로부터 section들을 포함시킬 때 file name을 지정할 수 있다. 하나 또는 그 이상의 file들이 메모리 내의 특정 위치에 있을 필요가 있는 special data를 갖고 있다면 예를 들어 이렇게 쓸 수 있다.

data.o(.data)

section의 list 없이 file name을 사용한다면, input file내의 이들 모든 section들이 output section 안에 포함될 것이다. 이는 보통 하진 않지만 가끔은 유용할 수 있다. 예를 들면 다음과 같다.

data.o

wild card character가 들어있지 않은 file name을 사용하면 linker는 먼저 linker command line이나 INPUT command 안에 file name이 같이 지정되어 있는지를 먼저 볼 거다. 아니라면 linker는 command line 상에 있을 때처럼 input file로 그 file을 열려고 할거다. 이는 linker가 archive search path에서 그 file을 찾지 않으므로 INPUT command와는 차이가 있다는 것에 주의해라.

Input section wildcard 패턴

input section description에서 file name 또는 section name 아니면 둘 다 wildcard pattern이 될 수 있다.

많은 예제 내에서 봤던 '*'의 file name은 file name에 대한 간단한 wildcard pattern이다.

wildcard pattern은 Unix shell에서 사용되는 것과 비슷하다.

'*'

어떤 수의 character들과도 match

'?'

어떤 single character와 match

'[chars]'

chars의 어떤 single instance와 match; '-' 문자는 소문자를 match하기 위한 '[a-z]'에서처럼 문자의 range를 정하는데 사용된다.

'\'

이 뒤의 다음 character를 quote한다.

file name이 wildcard와 match될 때 wildcard characters는 (Unix 상에서 directory를 나누는데 사용되는) '/' character와는 match되지 않는다. '*'  character를 포함하는 pattern은 예외다; 이는 '/'이 들어 있든 말든 항상 어떤 file name과도 match된다. section 이름에서는 wildcard character가 '/' character와 match된다.

file name wildcard pattern은 command line이나 INPUT command 상에서 명확히 지정된 file들만 match한다. linker는 wildcard를 확장해서 directory를 찾지 않는다.

어떤 file이 하나 또는 그 이상의 wildcard pattern과 match되거나, 또는 어떤 file이 그 file name이 명확히 나타나면서 하나의 wildcard pattern에도 맞으면, linker는 linker script 내에서 먼저 match되는 것을 사용한다.  예를 들어, 다음 순서의 input section description은 'data.o' rule이 사용되지 않기 때문에 아마 error일 거다.

.data : { *(.data) }
.data1 : { data.o(.data) }

보통 linker는 link하는 동안 보인 순서대로 wildcard에 맞는 file과 section을 둔다. 이를 괄호로 싸인 wildcard pattern 전에 (예를 들면, SORT(.text*)처럼) SORT keyword를 써서 바꿀 수 있다. SORT keyword를 쓰면 linker는 output file 안에 file과 section을 두기 전에 name으로 오름차순 정렬한다.

input section들이 가는데가 헷갈린다면 map file을 생성하는 '-M' 링커 옵션을 써라. 이 map 파일은 input section이 output section으로 정확히 어떻게 mapped되는지를 보여준다.

다음 예제는 wildcard pattern이 file을 나누는데 어떻게 사용되는지를 보여준다. 이 linker script는 linker에게 '.text' section을 '.text'안에, 모든 '.bss' section을 '.bss'안에 두도록 지시한다. linker는 대문자로 시작하는 모든 파일로부터의 '.data' section을 '.DATA' 안에 둘 거다. 다른 모든 파일은 linker가 '.data' 안에 '.data'를 둘 거다.

SECTIONS {
  .text : { *(.text) }
  .DATA : { [A-Z]*(.data) }
  .data : { *(.data) }
  .bss : { *(.bss) }
}

common symbol에 대한 input section

common symbol을 위해서는 특별한 표기법이 필요하다. 왜냐하면 많은 object file format에서 common symbol들은 특정 input section을 갖지 않기 때문이다. linker는 common symbol들을 그이름이 'COMMON'인 input section 내에 있는 것처럼 다룬다.

다른 input section들로 하는 것처럼 그냥 'COMMON' section과 file name을 사용할 수 있다. 다른 input file들로부터의 common symbol들을 또 다른 section 내에 두는 것에 비해 한 section 내에 특정 input file로부터의 common symbol들을 두는데 이를 사용할 수 있다.

대부분의 경우에 input file내의 common symbol들은 output file내의 '.bss' section내에 두게 된다. 예를 들면:

.bss { *(.bss) *(COMMON) }

어떤 object file format들은 하나 이상의 common symbol 타입을 갖는다. 예를 들면, MIPS ELF object file format은 standard common symbol들과 small common symbol을 구별한다. 이 경우, linker는 다른 common symbol 타입을 위한 다른 특별한 section name과 '.scommon'을 small common symbol을 위해 사용할 거다. 이는 common symbol의 다른 타입을 메모리의 다른 위치에 map할 수 있도록 한다.

오래된 linker scripts에서는 '[COMMON]'을 볼 수도 있다. 이 표기는 지금 구식으로 취급된다. 이는 '*(COMMON)'과 동등하다.

Input section과 garbage collection

link-time garbage collection이 사용중이면 ('--gc-sections'), 제거되지 말아야할 section들을 표시하는 것이 꽤 유용하다. 이는 KEEP(*(.init)) 이나 KEEP(SORT(*)(.ctors)) 처럼 input section의 wildcard entry를 KEEP()으로 둘러쌈으로써 할 수 있다.

Input section 예제

다음 예제는 완전한 linker script이다. 이는 linker에게 file 'all.o'로부터 모든 section들을 읽어서 그들을 ‘0x10000’위치에서 시작하는 output section 'outputa'의 시작에 두도록 이야기 한다. 그 바로 뒤에 'foo.o' file로부터의 section '.input1' 전부가 같은 output section내에 따른다. 'foo.o'로부터의 section '.input2' 전부는 output section 'outputb'내로 가고, 그 뒤에 'foo1.o'로부터의 section '.input1'이 따른다. 남은 다른 모든 파일로부터의 '.input1''.input2' section들 전부가 output section 'outputc'로 쓰여진다.

SECTIONS {
  outputa 0x10000 :
    {
    all.o
    foo.o (.input1)
    }
  outputb :
    {
    foo.o (.input2)
    foo1.o (.input1)
    }
  outputc :
    {
    *(.input1)
    *(.input2)
    }
}

Output section data

output section command로 output section 내에 BYTE, SHORT, LONG, QUAD 또는 SQUAD를 사용해서 byte 수를 정하는 data를 넣을 수 있다. 각 keyword 뒤에는 괄호 안에 저장할 값을 쓴 expression이 따른다 (Linker Scripts안의 Expression절을 보라). expression의 값은 location counter의 현재 값에 저장된다.

BYTE, SHORT, LONG과 QUAD command는 1, 2, 4, 그리고 8 bytes를 (각각) 저장한다. Byte들을 저장하고 나서 location counter는 저장된 byte의 수만큼 증가한다.

예를 들어, 다음은 byte 1을 저장하고 symbol ‘addr’의 4 byte 값이 뒤에 온다:

BYTE(1)
LONG(addr)

64 bit host 또는 target을 사용하면 QUADSQUAD는 같다; 둘 다 8 byte 또는 64 bit 값을 저장한다. host와 target 모두 32 bit를 사용하면, expression은 32 bit로 계산된다. 이 경우 QUAD는 64 bit로 zero extended된 32 bit 값을 저장하고, SQUAD는 64 bit로 sign extended된 32 bit 값을 저장한다.

output file의 보통의 경우처럼 output file format이 endianness를 명시하면, 그 값은 그 endianness에 따라 저장된다. object file format이 endianness를 명시하지 않으면, 예를 들어 S-records가 true인 경우, 그 값은 첫 input object file의 endianness에 따라 저장된다.

현재 section의 fill pattern을 설정하려면 FILL command를 사용할 수 있다. 이것 뒤에는 괄호 안의 expression이 온다. 그 section 내의 다른 모든 지정되지 않은 memory regions (예를 들어, input sections에 요구되는 alignment로 인해 남는 gap들)은 그 expression의 two least significant bytes로 필요하면 반복하면서 채워진다. FILL statement는 그 section definition 내에 나타나는 지점 이후의 memory 위치에 적용된다. 하나 이상의 FILL statement를 넣으므로써 output section의 다른 부분에 다른 fill pattern을 쓸 수 있다.

다음 예제는 값 '0x9090'으로 지정되지 않는 메모리 region을 어떻게 채우는지를 보여준다.

FILL(0x9090)

FILL command는 ‘=fillexp‘ output section attribute와 비슷하지만 (Output section 채우기 절을 보라), 전체 section이 아닌, FILL command 다음의 section 부분에만 영향을 미친다. 둘 다 사용되면 FILL command가 우선된다.

Output section keywords

output section commands처럼 쓸 수 있는 두가지 keyword가 있다.

CREATE_OBJECT_SYMBOLS

이 command는 linker에게 각 input file을 위한 symbol을 만들라고 이야기한다. 각 symbol의 이름은 해당하는 input file의 이름이 된다. 각 symbol의 section은 CREATE_OBJECT_SYMBOLS command가 있는 그 output section이 된다. 이는 a.out object file format에 편리하다. 다른 object file에서는 거의 사용되지 않을 것이다.

CONSTRUCTORS

a.out object file format을 사용해서 linking할 때 linker는 C++ global constructors와 destructors를 지원하기 위해 unusual set construct를 사용한다. ECOFF나 XCOFF 같은 arbitrary section을 지원하지 않는 object file format들을 linking할 때 linker는 알아서 그 이름으로 C++ global constructors와 destructors를 인식한다. 이들 object file format들을 위해서 CONSTRUCTORS command가 linker에게 CONSTRUCTORS command가 나오는 output section 내에 constructor information을 두도록 알려준다. CONSTRUCTORS command는 다른 object file format들에서는 무시된다. symbol __CTOR_LIST__ 는 global constructors의 시작을 표시하고, symbol __DTOR_LIST는 그 끝을 표시한다. list 내의 첫번째 word는 entries의 수이고, 그 뒤에 각 consructor나 destructor의 address가, 그 뒤에 zero word가 하나 붙는다. compiler는 실제로 code를 실행하기 위헤서 조정을 해야 한다. 이들 object file format들을 위해 GNU C++은 보통 subroutine __main으로부터 constructors를 호출한다; __main의 호출은 자동으로 main을 위한 startup code안으로 들어간다. GNU C++은 보통 destructors를 atexit를 사용하거나 아니면 exit로부터 그 funtion을 직접 호출해서 실행한다. arbitrary section name을 지원하는 COFF나 ELF 같은 object file format들에는 GNU C++은 .ctors.dtors sections 안에 global constructors와 destructors의 addresses를 두도록 조정할 것이다. 다음 sequence를 linker script에 두는 것은 GNU C++ runtime code가 보는 table을 build할 것이다.

      __CTOR_LIST__ = .;
      LONG((__CTOR_END__ - __CTOR_LIST__) / 4 - 2)
      *(.ctors)
      LONG(0)
      __CTOR_END__ = .;
      __DTOR_LIST__ = .;
      LONG((__DTOR_END__ - __DTOR_LIST__) / 4 - 2)
      *(.dtors)
      LONG(0)
      __DTOR_END__ = .;

initialization priority에 global constructors가 실행되는 순서 상의 control을 제공하는 GNU C++ support를 사용하고 있다면, 반드시 정확한 순서로 그들이 실행됨을 보장하기 위해 link time에 constructors를 sort해야 한다. CONSTUCTORS command를 사용하려면, 대신 'SORT(CONSTRUCTORS)'를 사용하라. .ctors.dtors section을 사용하려면, 그냥 '*(.ctors)''*(.dtors)' 대신에 '*(SORT(.ctors))''*(SORT(.dtors))'를 사용하라. 보통 compiler와 linker는 이들 이슈를 알아서 처리할 것이고, 이에 대해 신경쓸 필요가 없을 것이다. 그러나 C++을 사용하고 linker scripts를 작성한다면 이를 고려할 필요가 있을 수 있다.

Output section discarding

linker는 아무 내용도 없는 output section을 만들지 않을 것이다. 이는 어떤 input files내에 있을 수 있는, 혹은 없을 수 있는 input section들을 참조하는데 편리하다. 예를 들어:

.foo { *(.foo) }

는 ‘.foo’ section이 적어도 하나의 input file내에 있을 때만 ‘.foo’ section을 output file 내에 만들거다.

input section description보다는 symbol assignment같은 output section command를 사용한다면, 맞는 input section들이 없어도 output section은 언제나 만들어 질 것이다.

special output section name ‘/DISCARD/’는 input sections을 discard할 때 사용될 수 있다. 이름이 ‘/DISCARD/’인 output section으로 assigned되는 모든 input section들은 output file내에 포함되지 않는다.

Output section attributes

다음처럼 생긴 output section의 full description을 위에서 보았다.

section [address] [(type)] : [AT(lma)]
  {
    output-section-command
    output-section-command
    ...
  } [>region] [:phdr :phdr ...] [=fillexp]

이미 section, address, output-section-command에 대해 설명했다. 이번 절에서는 남은 section attributes를 설명할 거다.

Output section type

각 output section은 type을 가질 수 있다. 이 type은 괄호 안의 keyword이다. 다음 type들이 정의된다:

NOLOAD

그 section은 not loadable로 표시되어야 한다. 그래서 프로그램이 실행될 때 memory로 loaded되지 않을 거다.

DSECT

COPY

INFO

OVERLAY

이들 type 이름들은 backward compatibility를 위해서 지원된다. 잘 쓰이진 않는다. 모두 같은 효과를 갖는다. 그 section은 not allocatable로 표시되어야 한다. 그래서 프로그램이 실행될 때 그 section을 위한 memory가 allocated되지 않는다.

linker는 보통 그에 map되는 input section에 기초해서 output section의 attributes를 set한다. section type을 사용해서 이를 override할 수 있다. 예를 들어, 아래의 script sample에서 ‘ROM’ section은 memory location ‘0’에 지정되고, 프로그램이 실행될 때 loaded될 필요는 없다. ‘ROM’ section의 내용은 linker output file내에 평소처럼 나올거다.

SECTIONS {
  ROM 0 (NOLOAD) : { ... }
  ...
}

Output section LMA

모든 section은 virtual address (VMA)와 load address (LMA)를 갖는다; 기본적인 개념 절을 보라. output section description에 있을 수 있는 address expression은 VMA를 set한다 (Output section address절을 보라).

linker는 보통 LMA를 VMA와 같도록 set한다. 이를 AT keyword를 사용해서 바꿀 수 있다. AT keyword 뒤의 expression lma는 그 section의 load address를 지정한다.

이 기능은 ROM image를 쉽게 build하도록 design되었다. 예를 들어, 다음 linker script는 3 output section을 만든다: 0x1000에서 시작하는 '.text', VMA는 0x2000에서 시작하지만 '.text' section의 끝에 load되는 '.mdata', 그리고, address 0x3000에 uninitialized data를 잡는 '.bss'. symbol _data0x2000으로 정의되는데 이는 location counter가 LMA 값이 아닌 VMA 값을 담는다는 것을 보여준다.

SECTIONS
  {
  .text 0x1000 : { *(.text) _etext = . ; }
  .mdata 0x2000 : 
    AT ( ADDR (.text) + SIZEOF (.text) )
    { _data = . ; *(.data); _edata = . ;  }
  .bss 0x3000 :
    { _bstart = . ;  *(.bss) *(COMMON) ; _bend = . ;}
}

이 linker script로 generate되는 프로그램을 위한 실제 run-time initialization code는 ROM image로부터 initialized data를 그 runtime address로 복사하기 위해서 다음과 같은 것을 포함할거다. 이 코드가 linker script가 define하는 symbol들을 어떻게 유용하게 쓰는지에 주목하라.

extern char _etext, _data, _edata, _bstart, _bend;
char *src = &_etext;
char *dst = &_data;

/* ROM has data at end of text; copy it. */
while (dst < &_edata) {
  *dst++ = *src++;
}

/* Zero bss */
for (dst = &_bstart; dst< &_bend; dst++)
  *dst = 0;

Output section region

'>region'을 사용해서 한 section을 이전에 정의된 memory region으로 assign할 수 있다. MEMORY command 절을 보라.

여기 간단한 예제다:

MEMORY { rom : ORIGIN = 0x1000, LENGTH = 0x1000 }
SECTIONS { ROM : { *(.text) } >rom }

Output section phdr

':phdr'을 사용해서 한 section을 이전에 define된 program segment로 assign할 수 있다. PHDRS command 절을 보라. 한 섹션이 하나 또는 그 이상의 segments로 assign되면, 명시적으로 :phdr modifier가 사용되기 전까지 모든 subsequent allocated sections들이 그들 segments로 assign될 것이다. :NONE으로 linker에게 모든 segment에 그 section을 두지 않을 것임을 알려줄 수 있다.

여기 간단한 예제다:

PHDRS { text PT_LOAD ; }
SECTIONS { .text : { *(.text) } :text }

Output section fill

'=fillexp'를 사용해서 전체 section을 위한 fill pattern을 set할 수 있다. fillexp는 expression이다 (Linker Scripts에서 Expression절을 보라). 그 output section 내의 memory의 모든 unspecified regions (예를 들어, input sections의 required alignment로 인해 남는 gaps) 은 그 값의 2 least significant bytes로 필요하면 반복적으로 채워질거다.

output section commands내에 FILL command로 fill value를 바꿀 수 있다; Output section data 절을 보라.

여기 간단한 예제다:

SECTIONS { .text : { *(.text) } =0x9090 }

Overlay description

overlay description은 single memory image의 일부로 load되지만 같은 memory address에서 실행되는 section들을 describe하는 쉬운 방법을 제공한다. run time에 어떤 overlay manager가 overlaid sections를 요구사항에 맞게 아마 간단한 addressing bit를 조작해서 runtime memory address의 안과 밖에 copy할 거다. 이 방식은 예를 들면 특정 memory region이 다른 곳보다 빠를 때 유용할 수 있다.

Overlays는 OVERLAY command를 사용해서 describe한다. OVERLAY comand는 output section description처럼 SECTIONS command 안에서 사용된다. OVERLAY command의 full syntax는 다음과 같다:

OVERLAY [start] : [NOCROSSREFS] [AT ( ldaddr )]
  {
    secname1
      {
        output-section-command
        output-section-command
        ...
      } [:phdr...] [=fill]
    secname2
      {
        output-section-command
        output-section-command
        ...
      } [:phdr...] [=fill]
    ...
  } [>region] [:phdr...] [=fill]

OVERLAY (키워드) 를 빼고 모두 옵션이고, 각 section은 name (위에서 secname1secname2)을 가져야 한다. OVERLAY construct 내의 section definitions은 OVERLAY 내에 section들을 위해 define되는 address와 memory regions들이 없다는 것을 제외하면 일반적인 SECTIONS contruct (SECTIONS command를 보라)내의 것들과 동일하다.

그 sections들은 모두 같은 starting address로 defined된다. 그 sections들의 load addresses는 전체에 OVERLAY를 위해 사용되는 load address에서 시작하는 memory 내에 consecutive하게 조정된다 (보통 section definitions처럼 load address는 옵션이고 default는 start address이다; start address 또한 옵션이고, default는 현재 location counter 값이다).

NOCROSSREFS keyword가 사용되면 sections 사이에 어떤 reference가 있다면, linker는 error로 report할 거다. 같은 address에서 section 모두가 실행되면, 보통 한 section이 다른 section에 직접 refer하는 것은 부적절하다. 다른 linker script command 절을 보라.

OVERLAY 내의 각 모든 section에 대해 linker는 자동으로 2개의 symbol을 define한다. symbol __load_start_secname이 그 section의 starting load address로 defined된다. symbol __load_stop_secname이 그 section의 final load address로 defined된다. C identifiers내에 있지 않은 secname 내의 모든 문자는 제거된다. C (또는 assembler) code는 이들 symbol을 필요에 따라 overlaid section 근처로 옮기는데 사용할 수 있다.

overlay의 끝에서 location counter의 값은 overlay의 address + 가장 큰 section의 size가 된다.

여기 예제다. SECTION construct 안에 이게 있어야 함을 기억해라.

  OVERLAY 0x1000 : AT (0x4000)
   {
     .text0 { o1/*.o(.text) }
     .text1 { o2/*.o(.text) }
   }

이는 address 0x1000에서 시작하는 '.text0''.text1' 모두를 define한다. '.text0'는 address 0x4000에 load되고, '.text1''.text0' 바로 뒤에 load될 것이다. 다음 symbol들이 defined된다: __load_start_text0__load_stop_text0__load_start_text1__load_stop_text1.

overlay .text1을 overlay area로 복사하는 코드는 다음과 같을 것이다.

  extern char __load_start_text1, __load_stop_text1;
  memcpy ((char *) 0x1000, &__load_start_text1,
          &__load_stop_text1 - &__load_start_text1);

OVERLAY command는 단지 유용한 문법임을 모든 것은 좀 더 많은 basic command를 사용해서 할 수 있음을 알아둬라. 위의 예제는 아래처럼 똑같이 쓰여질 수 있다.

  .text0 0x1000 : AT (0x4000) { o1/*.o(.text) }
  __load_start_text0 = LOADADDR (.text0);
  __load_stop_text0 = LOADADDR (.text0) + SIZEOF (.text0);
  .text1 0x1000 : AT (0x4000 + SIZEOF (.text0)) { o2/*.o(.text) }
  __load_start_text1 = LOADADDR (.text1);
  __load_stop_text1 = LOADADDR (.text1) + SIZEOF (.text1);
  . = 0x1000 + MAX (SIZEOF (.text0), SIZEOF (.text1));

MEMORY command

linker의 default configuration은 모든 available memory의 allocation을 허용한다. MEMORY command를 사용해서 이를 override할 수 있다.

MEMORY command는 target 내의 memory block의 location과 size를 describe한다. 이걸로 linker에게 어떤 memory regions들은 사용할지, 어떤 memory regions들은 사용하지 말아야 할지를 describe할 수 있다. 그러고 나면 sections들을 특정 memory regions에 assign할 수 있다. linker는 section addresses를 memory regions에 기초해서 set하고. 너무 꽉 차는 regions들에 대해 경고할 것이다. linker는 available regions내로 맞추기 위해서 section들을 섞지 않는다.

linker script는 최대 하나의 MEMORY command를 사용할 수 있다. 하지만, 원하는 만큼 그 안에 많은 memory의 blocks를 define할 수 있다. 문법은 다음과 같다:

MEMORY 
  {
    name [(attr)] : ORIGIN = origin, LENGTH = len
    ...
  }

name은 linker script 안에서 그 region을 참조하는 이름이다. region name은 linker script 밖에서는 의미가 없다. Region names들은 분리된 name space 안에 저장되고, symbol names, file names, 또는 section names와 conflict이 일어나지 않는다. 각 memory region은 구별되는 name을 가져야 한다.

attr string은 특정 명시적으로 linker script 내에서 mapped되지 않는 input section을 위한 memory region을 사용할지를 지정하는 attributes의 optional list이다. SECTION command 절에서 설명한대로, input section에 대해 output section을 지정하지 않으면, linker는 input section과 같은 이름으로 output section을 만들 것이다. region attributes를 define하면, linker는 생성되는 output section위한 memory region을 선택하는데 그것들을 사용할 거다.

attr string은 다음 characters 중 하나로 구성되어야 한다:

R'</samp>
<p style="padding-left: 40px;">Read-only section</p>
<samp>
W’

Read/write section

X'</samp>
<p style="padding-left: 40px;">Executable section</p>
<samp>
A’

Allocatable section

I'</samp>
<p style="padding-left: 40px;">Initialized section</p>
<samp>
L’

`I’와 같다

`!’

앞에 붙은 attributes 모두를 뒤집기

unmapped section이 '!' 아닌 listed attributes 중 어느 하나에 match되면, memory region내에 두게 된다. '!' attributes는 이 test를 뒤집는다. 그래서 unmapped section이 listed attributes 중 어느 하나에도 match되지 않으면 그 memory region내에 두게 된다.

origin은 memory region의 start address를 위한 expression이다. 그 expression은 memory allocation을 하기 전에 constant로 evaluate해야 한다. 이는 즉, relative symbol을 사용할 수 없다는 뜻이다. keyword ORIGINorg 또는 o로 줄여 쓸 수 있다(그러나 ORG같은건 안된다).

len은 memory region의 bytes로 된 size를 위한 expression이다. origin expression과 같이 쓰면, 그 expression은 memory allocation을 하기 전에 constant로 evaluate해야 한다. keyword LENGTHlen이나 l로 줄여 쓸 수 있다.

다음 예제에서 allocation을 위해 available한 2개의 memory regions이 있다고 지정한다: 하나는 '0'에서 시작하는 256 kilobytes 하나, 4 megabytes인 '0x40000000'에서 시작하는 다른 하나. linker는 명시적으로 memory region 내로 mapped하지 않는, read-only 또는 excutable 중 하나인 모든 section울 'rom' memory region 내로 둘 거다. linker는 명시적으로 memory region내로 mapped하지 않는 다른 sections들을 'ram' memory region 내에 둘 것이다.

MEMORY 
  {
    rom (rx)  : ORIGIN = 0, LENGTH = 256K
    ram (!rx) : org = 0x40000000, l = 4M
  }

memory region을 define하고 나면, '>region' output section attributes를 사용해서 linker에게 그 memory region내로 지정한 output section을 두도록 지시할 수 있다. 예를 들어, 'mem'이라는 memory region이 있으면, 그 output section definition 내에 '>mem'를 사용할 수 있다. Output section region 절을 보라. output section에 아무 address도 지정되어 있지 않으면, linker는 그 memory region 내의 다음 available address로 그 address를 set할 거다. 한 memory region으로 direct된 combined output section이 그 region에 비해 너무 크면, linker는 error message를 올릴 거다.

PHDRS command

ELF object file format은 segments로 알려진 program headers를 사용한다. program headers는 어떻게 program이 memory 내로 load되어야 하는지를 describe한다. objdump 프로그램에 ‘-p’ 옵션을 사용해서 이를 출력할 수 있다.

native ELF system 상에서 ELF program을 실행하면, system loader는 그 program을 load할지를 살펴보기 위해서 program headers를 읽는다. 이는 그 program header가 맞게 set되어있어야만 동작한다. 이 매뉴얼은 어떻게 system loader가 program header를 interpret하는지 세부사항은 describe하지 않는다; 더 자세히는 ELF ABI를 보라.

linker는 default로 reasonable한 program headers를 생성한다. 그러나 어떤 경우에 더 세부적인 program headers를 지정할 필요가 있을 수 있다. 이 목적으로 PHDRS command를 사용할 수 있다. linker가 PHDRS command를 linker script안에서 보면, 지정된 것 외에 어떤 program headers도 만들지 않을 거다.

linker는 ELF output file을 generate할 때만 PHDRS command를 신경쓴다. 다른 때는 PHDRS를 그냥 무시할거다.

PHDRS command의 문법은 다음과 같다. words PHDRS, FILEHDR, AT 그리고 FLAGS가 keywords이다.

PHDRS
{
  name type [ FILEHDR ] [ PHDRS ] [ AT ( address ) ]
        [ FLAGS ( flags ) ] ;
}

name은 linker script의 SECTIONS command 내에서 참조를 위해서만 사용된다. 이를 output file내에 두지는 않는다. Program header names은 분리된 name space에 저장되고, symbol names, file names, 또는 section names와 conflict이 일어나지 않는다. 각 program header는 구별되는 이름을 가져야 한다.

특정 program header types들은 system loader가 file로부터 load할 memory의 segments를 describe한다. linker script내에서 그 segments내에 allocatable output sections를 둠으로써 이들 segments의 contents를 지정한다. 특정 segment내에 section을 두는데는 ':phdr' output section attribute를 사용한다. Output section phdr 절을 보라.

하나 이상의 segment 내에 특정 sections들을 두는 것이 보통이다. 이는 그냥 다른 것을 하나의 memory segment가 포함함을 의미한다. section을 포함할 각 segment마다 한번씩 사용해서 ':phdr'를 반복할 수 있다.

':phdr'를 사용해서 하나 이상의 segments 내에 한 section을 두면, linker는 같은 segments 내에 ':phdr'을 지정하지 않은 모든 이어지는 allocatable sections들을 둘 거다. 이는 일반적으로 single segment 내에 contiguous sections 전체를 두기 때문에 편의를 위한 것이다. default segment를 override하기 위해서, 그리고 linker에게 어떤 segment에도 그 section을 전혀 두지 않겠다고 알리기 위해서 :NONE을 사용할 수 있다.

segment의 contents를 더 많이 describe하기 위해서 program header type 뒤에 FILEHDRPHDRS keyword를 사용할 수 있다. FILEHDR keyword는 그 segment가 ELF file header를 포함해야 함을 의미한다. PHDRS keyword는 그 segment가 ELF program headers 그 자체를 포함해야 함을 의미한다.

type은 다음 중 하나일 수 있다. 숫자는 keyword의 값을 나타낸다.

PT-NULL (0)

unused program header를 나타냄.

PT_LOAD (1)

이 program header는 file로부터 load되는 segment를 describe함을 나타냄.

PT_DYNAMIC (2)

dynamic linking information이 있을 수 있는 segment를 나타냄

PT_INTERP (3)

program interpreter의 name이 있을 수 있는 segment를 나타냄

PT_NOTE (4)

note information을 담고 있는 segment를 나타냄.

PT_SHLIB (5)

ELF ABI에 의해 지정되지 않지만 defined된 reserved program header type

PT_PHDR (6)

program header가 있을 수 있는 segment를 나타냄

expression

program header의 numeric type을 주는 expression. 이는 위에서 defined되지 않은 type을 위해 사용될 수 있음.

AT expression을 사용해서 memory 내의 특정 address에 load되어야 하는 segment를 지정할 수 있다. 이는 output section attribute로 사용되는 AT command와 완전히 같다 (Output section LMA 절을 보라). program header를 위한 AT command는 output section attribute를 override한다.

linker는 보통 segment를 구성하는 section에 기초해서 segment flags를 set할거다. segment flags를 명시적으로 지정하기 위해서 FLAGS keyword를 사용할 수 있다. flags의 값은 integer여야 한다. 이는 program header의 p_flags field를 set하는데 사용될 수 있다.

여기 PHDRS의 예제다. 이는 native ELF system 상에서 사용되는 program header의 전형적인 set을 보여준다.

PHDRS
{
  headers PT_PHDR PHDRS ;
  interp PT_INTERP ;
  text PT_LOAD FILEHDR PHDRS ;
  data PT_LOAD ;
  dynamic PT_DYNAMIC ;
}

SECTIONS
{
  . = SIZEOF_HEADERS;
  .interp : { *(.interp) } :text :interp
  .text : { *(.text) } :text
  .rodata : { *(.rodata) } /* defaults to :text */
  ...
  . = . + 0x1000; /* move to a new page in memory */
  .data : { *(.data) } :data
  .dynamic : { *(.dynamic) } :data :dynamic
  ...
}

VERSION command

(생략)

Linker Scripts내의 Expression

linker script language 내의 expression의 syntax는 C expression의 그것과 완전히 같다. 모든 expressions는 integer로 evaluated된다. 모든 expression은 host와 target이 모두 32 bits면 32 bits로, 아니면 64 bits로, 모두 같은 size로 evaluated된다.

expressions내의 symbol values를 set하고 사용할 수 있다.

linker는 expression내의 사용을 위해 여러 특별 목적의 builtin functions을 define한다.

  • Constants: Constants
  • Symbols: Symbol Names
  • Location Counter: Location Counter
  • Operators: Operators
  • Evaluation: Evaluation
  • Expression Section: Expression의 Section
  • Builtin Functions: Builtin Functions

Constants

모든 constants는 integers다.

C에서처럼 linker는 '0'으로 시작하는 integer는 8진수이고, '0x' 또는 '0X'로 시작하는 integer는 16진수다. linker는 다른 integers는 10진수로 생각할 거다.

추가로 suffixes KM을 각각 constant의 scale로 사용할 수 있다. 예를 들어 다음 모두는 같은 값을 참조한다:

  _fourk_1 = 4K;
  _fourk_2 = 4096;
  _fourk_3 = 0x1000;

Symbol Names

quoted되지 않는 한 symbol name은 letter, '_', 또는 '.'으로 시작하고, letters, digits, '_', '.', '-'을 포함할 수 있다. Unquoted symbol names는 어떤 keywords와도 conflict되지 않아야 한다. odd characters를 포함하거나 keyword와 같은 이름을 가진 symbol을 double quoted로 둘러싸서 지정할 수 있다:

  "SECTION" = 9;
  "with a space" = "also with a space" + 10;

symbol이 많은 non-alphabetic characters를 포함할 수 있기 때문에, spaces로 symbol들을 구분하는게 안전하다. 예를 들어, ‘A – B’는 뻴셈 expression이지만, ‘A-B’는 하나의 symbol이다.

Location Counter

특별한 linker variable dot '.' 은 언제나 현재 output location counter를 담는다. .이 언제나 output section 내의 한 위치를 가리키므로, 이는 SECTION command 안의 expression 에서만 보일 수 있다. . symbol은 expression 내에 보통 symbol이 있을 수 있는 곳 어디에나 있을 수 있다.

. 에 값을 assign하는 것은 location counter를 옮기게 한다. 이는 output section에 holes를 만들 때 사용될 수 있다. location counter는 뒤로 옮겨지진 않을 거다.

SECTIONS
{
  output :
    {
      file1(.text)
      . = . + 1000;
      file2(.text)
      . += 1000;
      file3(.text)
    } = 0x1234;
}

위의 예제에서 'file1'으로부터의 '.text' section은 output section 'output'의 시작에 위치한다. 그 뒤에 1000 byte gap이 붙는다. 그리고 'file2'로부터의 '.text' section이 나오고, 또 1000 byte gap이 'file3'로부터의 '.text' 앞에 붙는다. '= 0x1234' 표기는 gap들에 쓰여질 data를 지정한다 (Output section fill 절을 보라).

Operators

linker는 표준 C의 arithmetic operators를 같은 standard binding과 우선순위 레벨로 인식한다:

우선순위         associativity   Operators                Notes
(highest)
1               left            !  -  ~                  (1)
2               left            *  /  %
3               left            +  -
4               left            >>  <<
5               left            ==  !=  >  <  <=  >=
6               left            &
7               left            |
8               left            &&
9               left            ||
10              right           ? :
11              right           &=  +=  -=  *=  /=       (2)
(lowest)

Notes: (1) Prefix operators (2) Assignments를 보라

Evaluation

linker는 lazy하게 expression을 evaluate한다. 전적으로 필요할 때만 expression의 값을 계산한다.

linker는 어떤 linking이든 전부 하기 위해서 첫번째 section의 start address 값, memory regions의 origin과 길이 같은 information을 필요로 한다. 이들 값은 linker가 linker scripts내에서 읽는대로 바로 계산된다. 그러나 (symbol values 같은) 다른 값들은 storage allocation 후까지 필요하지 않거나 모른다. 이런 값들은 나중에 (output section의 size 같은) 다른 information이 symbol assignment expression 내에서 사용되기 위해 available할 때 evaluated된다.

sections의 size는 allocation 이후까지 알 수 없기 때문에 assignments는 allocation 이후까지 수행되지 않는 것에 의존한다.

location counter '.'에 의존하는 것 같은 어떤 expression은 section allocation 중에 evaluated되어야 한다.

expression의 결과가 필요하지만, 그 값이 available하지 않으면 error가 난다. 예를 들어 다음과 같은 script는

SECTIONS
  {
    .text 9+this_isnt_constant : 
      { *(.text) }
  }

error message `non constant expression for initial address'를 내게 한다.

Expression의 Section

linker가 expression을 evaluate하면 그 결과는 absolute이거나 어떤 section에 relative하거나 둘 중 하나다. relative expression은 section의 base로부터 fixed offset으로 표현된다.

linker script내의 expression의 위치가 그게 absolute인지 relative인지를 결정한다. output section definition 내에 있는 expression은 그 output section의 base에서 relative이다. 다른 곳에 있는 expression은 absolute가 된다.

'-r' 옵션을 사용해서 relocatable output을 요청하면 relative expression으로의 symbol set은 relocatable일 거다. 이는 그 이상의 link operation이 symbol의 값을 바꿀 수 있음을 의미한다. 그 symbol의 section은 relative expression의 그 section이 될 거다.

absolute expression으로의 symbol set은 모든 그 이상의 link operation을 통해 같은 값으로 유지될 거다. 그 symbol은 absolute가 될 거고 다른 특정 연관된 section을 갖지 않을 거다.

builtin function ABSOLUTE를 사용해서 지정하지 않았다면 relative가 될 때, 그 expression이 absolute가 되도록 강제할 수 있다. 예를 들어 output section '.data'의 end address로 absolute symbol set을 만드려면:

SECTIONS
  {
    .data : { *(.data) _edata = ABSOLUTE(.); }
  }

'ABSOLUTE'가 쓰이지 않았다면 '_edata''.data' section의 relative가 됐을 거다.

Builtin Functions

linker script language는 linker script expression 내에 쓸 수 있는 많은 builtin function을 갖고 있다.

ABSOLUTE(exp)

expression exp의 absolute (non-negative와 반대인 non-relocatable) 값을 return. section definition 내에서 symbol 값이 보통 relative가 되는 데서 symbol로 absolute 값을 assign하는데 주로 유용함. Expression 절을 보라.

ADDR(section)

section 이름의 address (VMA)를 return. 사용 전에 그 section의 location을 defined했어야 한다. 다음 예제에서 symbol_1.output1 section의 relative가 되고 다른 둘은 absolute가 되는 것을 제외하면 start_of_output1, symbol_1symbol_2는 동등한 값이 assigned됨:

SECTIONS { …
  .output1 :
    {
    start_of_output_1 = ABSOLUTE(.);
    …
    }
  .output :
    {
    symbol_1 = ADDR(.output1);
    symbol_2 = start_of_output_1;
    }
… }

ALIGN(align)

ALIGN(exp,align)

다음 align boundary로 aligned된 location counter (.) 나 임의의 expression을 return. operand가 하나인 ALIGN은 location counter의 값을 바꾸지 않는다. 그 값에 arithmetic을 할 뿐이다. operand가 둘인 ALIGN은 임의의 expression이 위로 aligned되도록 할 수 있다 (ALIGN(align)ALIGN(ABSOLUTE(.), align)과 동등하다).

여기 앞의 section 이후에 output .data section을 다음 0x2000 byte boundary로 align하고, input section 이후에 다음 0x8000 boundary로의 section 내에 variable을 set하는 예제다:

SECTIONS { ...
  .data ALIGN(0x2000): {
    *(.data)
    variable = ALIGN(0x8000);
  }
... }

이 예제에서의 ALIGN의 첫번째 용도는 section definition의 optional address attribute로 사용되었기 때문에 section의 위치를 지정하는 거다 (Output Section Address절을 보라). ALIGN의 두번째 용도는 symbol의 값을 define하는데 사용하는 거다.

builtin function NEXTALIGN과 매우 관련이 있다.

ALIGNOF(section)

그 section이 allocated되었다면, section이 이름인 것의 bytes로의 alignment를 return. 그 section이 evaluated될 때 allocated되지 않았다면, linker는 error를 report할거다. 다음 예제에서 .output section의 alignment가 그 section내의 첫번째 값으로 저장된다.

 

SECTIONS{ …
  .output {
    LONG (ALIGNOF (.output))
    …
    }
… }

BLOCK(exp)

이는 오래된 linker script의 compatibility를 위한 ALIGN의 다른 이름이다. output section의 address를 setting할 때 자주 보인다.

DATA_SEGMENT_ALIGN(maxpagesizecommonpagesize)

 이는 다음 둘 중 하나와 동등하다.

(ALIGN(maxpagesize) + (. & (maxpagesize - 1)))
또는
(ALIGN(maxpagesize)
 + ((. + commonpagesize - 1) & (maxpagesize - commonpagesize)))

뒤의 것이 (이 expression의 결과와 DATA_SEGMENT_END 사이의 area인) data segment를 위해 더 작은 commonpagesize size의 pages를 사용하는지에 따라 앞의 것인지 아닌지가 달려 있다. 뒤의 form이 사용되면, runtime memory의 commonpagesize bytes가 on-disk file 내의 최대 commonpagesize로 낭비되는 bytes까지 포함해서 save될 것임을 의미한다.

이 expression은 SECTIONS command들 내에 직접 사용될 수 있지만, 모든 output section description에는 있을 수 없고, linker script에 딱 한번만 나올 수 있다. 최대 maxpagesize system page sizes 상에서 실행되는 동안, commonpagesize는 maxpagesize 이하여야 하고, object가 optimized되기를 원하는 system page size여야 한다. 그러나 system page size가 commonpagesize보다 크면 '-z relro' protection이 영향을 미치지 않음을 주의하라.

예제:

  . = DATA_SEGMENT_ALIGN(0x10000, 0x2000);

DATA_SEGMENT_END(exp)

이는 DATA_SEGMENT_ALIGN evaluation 목적의 data segment의 끝을 define한다.

  . = DATA_SEGMENT_END(.);

DATA_SEGMENT_RELRO_END(offsetexp)

이는 '-z relro'옵션을 사용했을 때 PT_GNU_RELRO segment의 끝을 define한다. '-z relro'옵션이 없으면, DATA_SEGMENT_RELRO_END가 아무것도 하지 않지만, 다른 때는 DATA_SEGMENT_ALIGN이 padded되어서 DATA_SEGMENT_ALIGN에 주어진 commonpagesize argument로 exp + offset이 aligned된다. linker script내에 두려면, DATA_SEGMENT_ALIGN과 DATA_SEGMENT_END 사이에 이를 두어야 한다. 두번째 argument로 인한 PT_GNU_RELRO segment의 끝에서 두번째 argument + 필요한 모든 padding을 evaluate하라.

  . = DATA_SEGMENT_RELRO_END(24, .);

DEFINED(symbol)

symbol이 linker global symbol table안에 defined되어 있으면 1을, 아니면 0을 return. symbol의 default values를 제공하는데 이 function을 사용할 수 있다. 예를 들어, 다음 script 조각은 global symbol 'begin''.text' section 내의 첫번째 위치로 어떻게 set하는지를 보여준다–그러나 'begin'이란 symbol이 이미 있다면, 그 값이 유지된다:

SECTIONS { ...
  .text : {
    begin = DEFINED(begin) ? begin : . ;
    ...
  }
  ...
}

LENGTH(memory)

이름이 memory인 memory region의 length를 return

LOADADDR(section)

이름 section의 absolute LMA를 retrun

LOG2CEIL(exp)

무한 쪽으로 올림된 exp의 밑이 2인 log.  LOG2CEIL(0)는 0을 return

MAX(exp1exp2)

exp1exp2 중 더 큰 걸 return

MIN(exp1exp2)

exp1exp2 중 더 작은 걸 return

NEXT(exp)

exp의 multiple인 다음 unallocated address를 return. 이 함수는 ALIGN(exp)와 매우 관련이 깊다; output file의 discontinuous memory를 define하는데 MEMORY command를 사용하기 전까지, 두 functions은 동등하다.

ORIGIN(memory)

이름이 memory인 memory region의 origin을 return

SEGMENT_START(segmentdefault)

이름이 segment인 것의 base address를 return. 이 segment에 명시적인 값이 (command-line '-T' option으로) 주어지지 않았다면 그 값이 return되고, 아니면 그 값은 default가 될거다. 현재, '-T' command-line option은 “text”, “data”, 그리고 “bss” section의 base address를 set하는데만 사용될 수 있지만, 어떤 segment name이든 SEGMENT_START를 사용할 수 있다.

SIZEOF(section)

그 섹션이 allocated되었다면, section이 이름인 것의 bytes로 된 size를 return. 그 section이 evaluate될 때 allocated되지 않았다면, linker는 error를 report할 거다. 다음 예제에서, symbol_1symbol_2는 같은 값이 assigned된다.

SECTIONS{ …
  .output {
    .start = . ;
    …
    .end = . ;
    }
  symbol_1 = .end - .start ;
  symbol_2 = SIZEOF(.output);
… }

SIZEOF_HEADERS

sizeof_headers

output file의 header의 bytes로 된 size를 return. 이는 output file의 시작에서 나오는 information이다. paging이 가능하도록 골랐다면, 첫번째 section의 start address를 setting할 때 이 수를 사용할 수 있다.

ELF output file을 생성할 때 linker script가 SIZEOF_HEADERS builtin function을 사용하면, linker는 모든 section addresses와 sizes를 결정하기 전에 program headers의 수를 계산해야 한다. linker가 additional program headers가 필요함을 발견하면, error ‘not enough room for program headers’를 report할 거다. 이 에러를 피하려면, SIZEOF_HEADERS function의 사용을 피하거나, linker가 additional program headers를 강제로 사용하는 것을 피하도록 linker script를 재작성하거나, PHDRS command를 사용해서 (PHDRS를 보라) program headers 자체를 define해야 한다.

Implicit Linker Scripts

linker script로 linker가 인식할 수 없는 object file이나 archive file을 지정하면, linker script로 그 파일을 읽으려고 시도할거다. 그 file이 linker script로 parsed될 수 없으면, linker는 error를 report할 거다.

Implicit linker script는 default linker script를 대신하지 않는다.

일반적으로 implicit linker script는 symbol assignment, 또는 INPUT, GROUP이나 VERSION commands만 포함할거다.

implicit linker script는 implicit linker script가 read된 command line의 위치에서 read되므로 어떤 input files이든 읽는다. 이는 archive searching에 영향을 준다.

 

One thought to “[Linker] GNU linker LD 사용하기 – Linker Scripts (.ld)”

답글 남기기

이메일 주소는 공개되지 않습니다. 필수 필드는 *로 표시됩니다