comparison tools/llvm-c-test/object.c @ 147:c2174574ed3a

LLVM 10
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Wed, 14 Aug 2019 16:55:33 +0900 (2019-08-14)
parents 1172e4bd9c6f
children
comparison
equal deleted inserted replaced
134:3a76565eade5 147:c2174574ed3a
1 /*===-- object.c - tool for testing libLLVM and llvm-c API ----------------===*\ 1 /*===-- object.c - tool for testing libLLVM and llvm-c API ----------------===*\
2 |* *| 2 |* *|
3 |* The LLVM Compiler Infrastructure *| 3 |* Part of the LLVM Project, under the Apache License v2.0 with LLVM *|
4 |* *| 4 |* Exceptions. *|
5 |* This file is distributed under the University of Illinois Open Source *| 5 |* See https://llvm.org/LICENSE.txt for license information. *|
6 |* License. See LICENSE.TXT for details. *| 6 |* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception *|
7 |* *| 7 |* *|
8 |*===----------------------------------------------------------------------===*| 8 |*===----------------------------------------------------------------------===*|
9 |* *| 9 |* *|
10 |* This file implements the --object-list-sections and --object-list-symbols *| 10 |* This file implements the --object-list-sections and --object-list-symbols *|
11 |* commands in llvm-c-test. *| 11 |* commands in llvm-c-test. *|
17 #include <stdio.h> 17 #include <stdio.h>
18 #include <stdlib.h> 18 #include <stdlib.h>
19 19
20 int llvm_object_list_sections(void) { 20 int llvm_object_list_sections(void) {
21 LLVMMemoryBufferRef MB; 21 LLVMMemoryBufferRef MB;
22 LLVMObjectFileRef O; 22 LLVMBinaryRef O;
23 LLVMSectionIteratorRef sect; 23 LLVMSectionIteratorRef sect;
24 char *msg = NULL;
25 24
26 if (LLVMCreateMemoryBufferWithSTDIN(&MB, &msg)) { 25 char *outBufferErr = NULL;
27 fprintf(stderr, "Error reading file: %s\n", msg); 26 if (LLVMCreateMemoryBufferWithSTDIN(&MB, &outBufferErr)) {
27 fprintf(stderr, "Error reading file: %s\n", outBufferErr);
28 free(outBufferErr);
28 exit(1); 29 exit(1);
29 } 30 }
30 31
31 O = LLVMCreateObjectFile(MB); 32 char *outBinaryErr = NULL;
32 if (!O) { 33 O = LLVMCreateBinary(MB, LLVMGetGlobalContext(), &outBinaryErr);
33 fprintf(stderr, "Error reading object\n"); 34 if (!O || outBinaryErr) {
35 fprintf(stderr, "Error reading object: %s\n", outBinaryErr);
36 free(outBinaryErr);
34 exit(1); 37 exit(1);
35 } 38 }
36 39
37 sect = LLVMGetSections(O); 40 sect = LLVMObjectFileCopySectionIterator(O);
38 while (!LLVMIsSectionIteratorAtEnd(O, sect)) { 41 while (sect && !LLVMObjectFileIsSectionIteratorAtEnd(O, sect)) {
39 printf("'%s': @0x%08" PRIx64 " +%" PRIu64 "\n", LLVMGetSectionName(sect), 42 printf("'%s': @0x%08" PRIx64 " +%" PRIu64 "\n", LLVMGetSectionName(sect),
40 LLVMGetSectionAddress(sect), LLVMGetSectionSize(sect)); 43 LLVMGetSectionAddress(sect), LLVMGetSectionSize(sect));
41 44
42 LLVMMoveToNextSection(sect); 45 LLVMMoveToNextSection(sect);
43 } 46 }
44 47
45 LLVMDisposeSectionIterator(sect); 48 LLVMDisposeSectionIterator(sect);
46 49
47 LLVMDisposeObjectFile(O); 50 LLVMDisposeBinary(O);
51
52 LLVMDisposeMemoryBuffer(MB);
48 53
49 return 0; 54 return 0;
50 } 55 }
51 56
52 int llvm_object_list_symbols(void) { 57 int llvm_object_list_symbols(void) {
53 LLVMMemoryBufferRef MB; 58 LLVMMemoryBufferRef MB;
54 LLVMObjectFileRef O; 59 LLVMBinaryRef O;
55 LLVMSectionIteratorRef sect; 60 LLVMSectionIteratorRef sect;
56 LLVMSymbolIteratorRef sym; 61 LLVMSymbolIteratorRef sym;
57 char *msg = NULL;
58 62
59 if (LLVMCreateMemoryBufferWithSTDIN(&MB, &msg)) { 63 char *outBufferErr = NULL;
60 fprintf(stderr, "Error reading file: %s\n", msg); 64 if (LLVMCreateMemoryBufferWithSTDIN(&MB, &outBufferErr)) {
65 fprintf(stderr, "Error reading file: %s\n", outBufferErr);
66 free(outBufferErr);
61 exit(1); 67 exit(1);
62 } 68 }
63 69
64 O = LLVMCreateObjectFile(MB); 70 char *outBinaryErr = NULL;
65 if (!O) { 71 O = LLVMCreateBinary(MB, LLVMGetGlobalContext(), &outBinaryErr);
66 fprintf(stderr, "Error reading object\n"); 72 if (!O || outBinaryErr) {
73 fprintf(stderr, "Error reading object: %s\n", outBinaryErr);
74 free(outBinaryErr);
67 exit(1); 75 exit(1);
68 } 76 }
69 77
70 sect = LLVMGetSections(O); 78 sect = LLVMObjectFileCopySectionIterator(O);
71 sym = LLVMGetSymbols(O); 79 sym = LLVMObjectFileCopySymbolIterator(O);
72 while (!LLVMIsSymbolIteratorAtEnd(O, sym)) { 80 while (sect && sym && !LLVMObjectFileIsSymbolIteratorAtEnd(O, sym)) {
73 81
74 LLVMMoveToContainingSection(sect, sym); 82 LLVMMoveToContainingSection(sect, sym);
75 printf("%s @0x%08" PRIx64 " +%" PRIu64 " (%s)\n", LLVMGetSymbolName(sym), 83 printf("%s @0x%08" PRIx64 " +%" PRIu64 " (%s)\n", LLVMGetSymbolName(sym),
76 LLVMGetSymbolAddress(sym), LLVMGetSymbolSize(sym), 84 LLVMGetSymbolAddress(sym), LLVMGetSymbolSize(sym),
77 LLVMGetSectionName(sect)); 85 LLVMGetSectionName(sect));
79 LLVMMoveToNextSymbol(sym); 87 LLVMMoveToNextSymbol(sym);
80 } 88 }
81 89
82 LLVMDisposeSymbolIterator(sym); 90 LLVMDisposeSymbolIterator(sym);
83 91
84 LLVMDisposeObjectFile(O); 92 LLVMDisposeBinary(O);
93
94 LLVMDisposeMemoryBuffer(MB);
85 95
86 return 0; 96 return 0;
87 } 97 }