blob: 809ad54f8721c82dc7df69f5e88099f59e892d77 [file] [log] [blame]
Anders Waldenborg2bef1a62013-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"
16#include "llvm-c/Object.h"
17#include <stdio.h>
18#include <stdlib.h>
19
Benjamin Kramer2e130532016-02-05 13:31:14 +000020int llvm_object_list_sections(void) {
Anders Waldenborg2bef1a62013-10-23 08:10:20 +000021 LLVMMemoryBufferRef MB;
22 LLVMObjectFileRef O;
NAKAMURA Takumi90fd79a2013-10-23 17:56:37 +000023 LLVMSectionIteratorRef sect;
Anders Waldenborg2bef1a62013-10-23 08:10:20 +000024 char *msg = NULL;
25
26 if (LLVMCreateMemoryBufferWithSTDIN(&MB, &msg)) {
27 fprintf(stderr, "Error reading file: %s\n", msg);
28 exit(1);
29 }
30
31 O = LLVMCreateObjectFile(MB);
32 if (!O) {
33 fprintf(stderr, "Error reading object\n");
34 exit(1);
35 }
36
NAKAMURA Takumi90fd79a2013-10-23 17:56:37 +000037 sect = LLVMGetSections(O);
Anders Waldenborg2bef1a62013-10-23 08:10:20 +000038 while (!LLVMIsSectionIteratorAtEnd(O, sect)) {
39 printf("'%s': @0x%08" PRIx64 " +%" PRIu64 "\n", LLVMGetSectionName(sect),
40 LLVMGetSectionAddress(sect), LLVMGetSectionSize(sect));
41
42 LLVMMoveToNextSection(sect);
43 }
44
45 LLVMDisposeSectionIterator(sect);
46
47 LLVMDisposeObjectFile(O);
48
49 return 0;
50}
51
Benjamin Kramer2e130532016-02-05 13:31:14 +000052int llvm_object_list_symbols(void) {
Anders Waldenborg2bef1a62013-10-23 08:10:20 +000053 LLVMMemoryBufferRef MB;
54 LLVMObjectFileRef O;
NAKAMURA Takumi90fd79a2013-10-23 17:56:37 +000055 LLVMSectionIteratorRef sect;
56 LLVMSymbolIteratorRef sym;
Anders Waldenborg2bef1a62013-10-23 08:10:20 +000057 char *msg = NULL;
58
59 if (LLVMCreateMemoryBufferWithSTDIN(&MB, &msg)) {
60 fprintf(stderr, "Error reading file: %s\n", msg);
61 exit(1);
62 }
63
64 O = LLVMCreateObjectFile(MB);
65 if (!O) {
66 fprintf(stderr, "Error reading object\n");
67 exit(1);
68 }
69
NAKAMURA Takumi90fd79a2013-10-23 17:56:37 +000070 sect = LLVMGetSections(O);
71 sym = LLVMGetSymbols(O);
Anders Waldenborg2bef1a62013-10-23 08:10:20 +000072 while (!LLVMIsSymbolIteratorAtEnd(O, sym)) {
73
74 LLVMMoveToContainingSection(sect, sym);
Rafael Espindola74267712014-04-21 13:45:32 +000075 printf("%s @0x%08" PRIx64 " +%" PRIu64 " (%s)\n", LLVMGetSymbolName(sym),
76 LLVMGetSymbolAddress(sym), LLVMGetSymbolSize(sym),
Anders Waldenborg2bef1a62013-10-23 08:10:20 +000077 LLVMGetSectionName(sect));
78
79 LLVMMoveToNextSymbol(sym);
80 }
81
82 LLVMDisposeSymbolIterator(sym);
83
84 LLVMDisposeObjectFile(O);
85
86 return 0;
87}