5
2

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.

コンパイル・リンク・ロードの結果を一覧する

Last updated at Posted at 2018-03-31

「リンカ・ローダ実践開発テクニック」が面白かったので何か作りたくなり、ELFファイルのいろんな要素(セグメントとかセクションとかシンボル)を読み取って、実際に展開された後のメモリマップに合わせて並べる簡単なプログラムを作ってみました。
できたものは<こちら>です。
本書はFreeBSD、32bit、C言語だけど、自分の環境はLinux、64bit、C++です。

例えば下記の変数たちがどのセクションに行くかみたいな話はよく聞くけど、それがどのセグメントに行ってプロセスのメモリ上のどこに配置されるのかまで合わせて一覧したいというのがモチベーション。

# include <stdlib.h>

int global_val = 5;
int global_arr[1000] = {1,2,3};
const int global_const_val = 5;
const int global_const_arr[10000] = {1,2,3};
int global_empty_val;
int global_empty_arr[2000000];
char  global_char_arr[] = "global_char_arr";
char *global_char_ptr   = "global_char_ptr";

int global_func(int x){
    return x + 1;
}

int main(){
    int local_val = 1;
    int local_arr[1000] = {1,2,3};
    const int local_const_val = 1;
    const int local_const_arr[10000] = {1,2,3};
    int local_empty_val;
    int local_empty_arr[2000000];
    char  local_char_arr[] = "local_char_arr";
    char *local_char_ptr   = "local_char_ptr";
    static int local_static_val = 1;
    static int local_static_empty_val;
    static const int local_static_const_val = 1;
    int *local_heap;
    local_heap = malloc(100000);
    sleep(10000);
    free(local_heap);
    return 0;
}

結果の概要は下記。
一番左列がメモリのアドレス、一番右列がシンボル名です。

linker_loader.PNG

出力の完全版は<こちら>です。
セグメントが分割されて順序が変わったりなど想定外に複雑な箇所も…。
でもだいたい期待通り動いていそうだ。

以上

5
2
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
5
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?