以下示例创建了一个支持库,其中列显了在 32 位链接编辑过程中处理的任何可重定位目标文件的节名。
$ cat support.c
#include <link.h>
#include <stdio.h>
static int indent = 0;
void
ld_start(const char * name, const Elf32_Half type,
const char * caller)
{
(void) printf("output image: %s\n", name);
}
void
ld_file(const char * name, const Elf_Kind kind, int flags,
Elf * elf)
{
if (flags & LD_SUP_EXTRACTED)
indent = 4;
else
indent = 2;
(void) printf("%*sfile: %s\n", indent, "", name);
}
void
ld_section(const char * name, Elf32_Shdr * shdr, Elf32_Word sndx,
Elf_Data * data, Elf * elf)
{
Elf32_Ehdr * ehdr = elf32_getehdr(elf);
if (ehdr->e_type == ET_REL)
(void) printf("%*s section [%ld]: %s\n", indent,
"", (long)sndx, name);
}
|
此支持库依赖于 libelf 来提供用于确定输入文件类型的 ELF 访问函数 elf32_getehdr(3ELF)。此支持库通过使用以下命令生成:
$ cc -o support.so.1 -G -K pic support.c -lelf -lc |
以下示例说明了从可重定位目标文件和本地归档库构造普通应用程序所产生的节诊断信息。使用 -S 选项不仅可以处理缺省调试信息,还可以调用支持库。
$ LD_OPTIONS="-S./support.so.1 -Slibldstab.so.1" \
cc -o prog main.c -L. -lfoo
output image: prog
file: /opt/COMPILER/crti.o
section [1]: .shstrtab
section [2]: .text
.......
file: /opt/COMPILER/crt1.o
section [1]: .shstrtab
section [2]: .text
.......
file: /opt/COMPILER/values-xt.o
section [1]: .shstrtab
section [2]: .text
.......
file: main.o
section [1]: .shstrtab
section [2]: .text
.......
file: ./libfoo.a
file: ./libfoo.a(foo.o)
section [1]: .shstrtab
section [2]: .text
.......
file: /lib/libc.so
file: /opt/COMPILER/crtn.o
section [1]: .shstrtab
section [2]: .text
.......
|
为了简化输出,已经减少了本示例中显示的节数。另外,编译器驱动程序所包含的文件也会有所不同。