# 先の例題サンプルプログラム
$ cat sample.c
main(){ puts("Linux");}
# オブジェクトファイル作成までを行う
# オブジェクトファイル名は .c を .o に代えた物
$ cc -c sample.c
# 実行ファイル作成
$ ld -o sample sample.o /usr/lib/libc.a
ld: warning: cannot find entry symbol _start; defaulting to 080480a0
$ ./sample
Linux!
セグメンテーション違反です
# Cプログラム実行に必要なライブラリを追加
$ ld -o sample sample.o /usr/lib/crt*.o /usr/lib/libc.a
$ ./sample
Linux!
$
$ ld --verbose -o sample sample.o /usr/lib/crt*.o /usr/lib/lib
GNU ld version 2.15.92.0.2 20040927
Supported emulations:
elf_i386
i386linux
using internal linker script:
==================================================
/* Script for -z combreloc: combine and sort reloc sections */
OUTPUT_FORMAT("elf32-i386", "elf32-i386",
"elf32-i386")
OUTPUT_ARCH(i386)
ENTRY(_start)
SEARCH_DIR("/usr/i386-asianux-linux/lib"); SEARCH_DIR("/usr/local/lib"); SEARCH_DIR("/lib"); SEARCH_DIR("/usr/lib");
/* Do we need any of these for elf?
__DYNAMIC = 0; */
SECTIONS
{
/* Read-only sections, merged into text segment: */
PROVIDE (__executable_start = 0x08048000); . = 0x08048000 + SIZEOF_HEADERS;
.interp : { *(.interp) }
.hash : { *(.hash) }
.dynsym : { *(.dynsym) }
.dynstr : { *(.dynstr) }
.gnu.version : { *(.gnu.version) }
.gnu.version_d : { *(.gnu.version_d) }
.gnu.version_r : { *(.gnu.version_r) }
.rel.dyn :
{
*(.rel.init)
: (中略)
/* SGI/MIPS DWARF 2 extensions */
.debug_weaknames 0 : { *(.debug_weaknames) }
.debug_funcnames 0 : { *(.debug_funcnames) }
.debug_typenames 0 : { *(.debug_typenames) }
.debug_varnames 0 : { *(.debug_varnames) }
/DISCARD/ : { *(.note.GNU-stack) }
}
: (中略)
==================================================
attempt to open sample.o succeeded
sample.o
attempt to open /usr/lib/crt1.o succeeded
/usr/lib/crt1.o
attempt to open /usr/lib/crti.o succeeded
/usr/lib/crti.o
attempt to open /usr/lib/crtn.o succeeded
/usr/lib/crtn.o
attempt to open /usr/lib/libc.a succeeded
(/usr/lib/libc.a)libc-start.o
(/usr/lib/libc.a)check_fds.o
: (中略)
(/usr/lib/libc.a)strncase_l.o
(/usr/lib/libc.a)dl-sym.o
copyright ycosSystems 2007, prgBasic/Body23.html