blob: 43521787b609948455258cf23f207790ae34fb72 [file] [log] [blame]
Anders Waldenborgb932c662013-10-23 08:10:20 +00001/*===-- object.c - tool for testing libLLVM and llvm-c API ----------------===*\
2|* *|
3|* The LLVM Compiler Infrastructure *|
4|* *|
5|* This file is distributed under the University of Illinois Open Source *|
6|* License. See LICENSE.TXT for details. *|
7|* *|
8|*===----------------------------------------------------------------------===*|
9|* *|
10|* This file implements the --object-list-sections and --object-list-symbols *|
11|* commands in llvm-c-test. *|
12|* *|
13\*===----------------------------------------------------------------------===*/
14
15#include "llvm-c-test.h"
Eric Christophera6b96002015-12-18 01:46:52 +000016#include "llvm-c/Core.h"
Anders Waldenborgb932c662013-10-23 08:10:20 +000017#include "llvm-c/Object.h"
18#include <stdio.h>
19#include <stdlib.h>
20
21int object_list_sections(void) {
22 LLVMMemoryBufferRef MB;
23 LLVMObjectFileRef O;
NAKAMURA Takumic4914692013-10-23 17:56:37 +000024 LLVMSectionIteratorRef sect;
Anders Waldenborgb932c662013-10-23 08:10:20 +000025 char *msg = NULL;
26
27 if (LLVMCreateMemoryBufferWithSTDIN(&MB, &msg)) {
28 fprintf(stderr, "Error reading file: %s\n", msg);
29 exit(1);
30 }
31
32 O = LLVMCreateObjectFile(MB);
33 if (!O) {
34 fprintf(stderr, "Error reading object\n");
35 exit(1);
36 }
37
NAKAMURA Takumic4914692013-10-23 17:56:37 +000038 sect = LLVMGetSections(O);
Anders Waldenborgb932c662013-10-23 08:10:20 +000039 while (!LLVMIsSectionIteratorAtEnd(O, sect)) {
40 printf("'%s': @0x%08" PRIx64 " +%" PRIu64 "\n", LLVMGetSectionName(sect),
41 LLVMGetSectionAddress(sect), LLVMGetSectionSize(sect));
42
43 LLVMMoveToNextSection(sect);
44 }
45
46 LLVMDisposeSectionIterator(sect);
47
48 LLVMDisposeObjectFile(O);
49
50 return 0;
51}
52
53int object_list_symbols(void) {
54 LLVMMemoryBufferRef MB;
55 LLVMObjectFileRef O;
NAKAMURA Takumic4914692013-10-23 17:56:37 +000056 LLVMSectionIteratorRef sect;
57 LLVMSymbolIteratorRef sym;
Anders Waldenborgb932c662013-10-23 08:10:20 +000058 char *msg = NULL;
59
60 if (LLVMCreateMemoryBufferWithSTDIN(&MB, &msg)) {
61 fprintf(stderr, "Error reading file: %s\n", msg);
62 exit(1);
63 }
64
65 O = LLVMCreateObjectFile(MB);
66 if (!O) {
67 fprintf(stderr, "Error reading object\n");
68 exit(1);
69 }
70
NAKAMURA Takumic4914692013-10-23 17:56:37 +000071 sect = LLVMGetSections(O);
72 sym = LLVMGetSymbols(O);
Anders Waldenborgb932c662013-10-23 08:10:20 +000073 while (!LLVMIsSymbolIteratorAtEnd(O, sym)) {
74
75 LLVMMoveToContainingSection(sect, sym);
Rafael Espindola6956b1a2014-04-21 13:45:32 +000076 printf("%s @0x%08" PRIx64 " +%" PRIu64 " (%s)\n", LLVMGetSymbolName(sym),
77 LLVMGetSymbolAddress(sym), LLVMGetSymbolSize(sym),
Anders Waldenborgb932c662013-10-23 08:10:20 +000078 LLVMGetSectionName(sect));
79
80 LLVMMoveToNextSymbol(sym);
81 }
82
83 LLVMDisposeSymbolIterator(sym);
84
85 LLVMDisposeObjectFile(O);
86
87 return 0;
88}