2013-10-23 10:10:20 +02:00
|
|
|
/*===-- object.c - tool for testing libLLVM and llvm-c API ----------------===*\
|
|
|
|
|* *|
|
2019-01-19 09:50:56 +01:00
|
|
|
|* Part of the LLVM Project, under the Apache License v2.0 with LLVM *|
|
|
|
|
|* Exceptions. *|
|
|
|
|
|* See https://llvm.org/LICENSE.txt for license information. *|
|
|
|
|
|* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception *|
|
2013-10-23 10:10:20 +02:00
|
|
|
|* *|
|
|
|
|
|*===----------------------------------------------------------------------===*|
|
|
|
|
|* *|
|
|
|
|
|* This file implements the --object-list-sections and --object-list-symbols *|
|
|
|
|
|* commands in llvm-c-test. *|
|
|
|
|
|* *|
|
|
|
|
\*===----------------------------------------------------------------------===*/
|
|
|
|
|
|
|
|
#include "llvm-c-test.h"
|
|
|
|
#include "llvm-c/Object.h"
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
2016-02-05 14:31:14 +01:00
|
|
|
int llvm_object_list_sections(void) {
|
2013-10-23 10:10:20 +02:00
|
|
|
LLVMMemoryBufferRef MB;
|
2019-04-09 23:53:31 +02:00
|
|
|
LLVMBinaryRef O;
|
2013-10-23 19:56:37 +02:00
|
|
|
LLVMSectionIteratorRef sect;
|
2013-10-23 10:10:20 +02:00
|
|
|
|
2019-04-09 23:53:31 +02:00
|
|
|
char *outBufferErr = NULL;
|
|
|
|
if (LLVMCreateMemoryBufferWithSTDIN(&MB, &outBufferErr)) {
|
|
|
|
fprintf(stderr, "Error reading file: %s\n", outBufferErr);
|
|
|
|
free(outBufferErr);
|
2013-10-23 10:10:20 +02:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2019-04-09 23:53:31 +02:00
|
|
|
char *outBinaryErr = NULL;
|
|
|
|
O = LLVMCreateBinary(MB, LLVMGetGlobalContext(), &outBinaryErr);
|
|
|
|
if (!O || outBinaryErr) {
|
|
|
|
fprintf(stderr, "Error reading object: %s\n", outBinaryErr);
|
|
|
|
free(outBinaryErr);
|
2013-10-23 10:10:20 +02:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2019-04-09 23:53:31 +02:00
|
|
|
sect = LLVMObjectFileCopySectionIterator(O);
|
|
|
|
while (sect && !LLVMObjectFileIsSectionIteratorAtEnd(O, sect)) {
|
2013-10-23 10:10:20 +02:00
|
|
|
printf("'%s': @0x%08" PRIx64 " +%" PRIu64 "\n", LLVMGetSectionName(sect),
|
|
|
|
LLVMGetSectionAddress(sect), LLVMGetSectionSize(sect));
|
|
|
|
|
|
|
|
LLVMMoveToNextSection(sect);
|
|
|
|
}
|
|
|
|
|
|
|
|
LLVMDisposeSectionIterator(sect);
|
|
|
|
|
2019-04-09 23:53:31 +02:00
|
|
|
LLVMDisposeBinary(O);
|
|
|
|
|
|
|
|
LLVMDisposeMemoryBuffer(MB);
|
2013-10-23 10:10:20 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-02-05 14:31:14 +01:00
|
|
|
int llvm_object_list_symbols(void) {
|
2013-10-23 10:10:20 +02:00
|
|
|
LLVMMemoryBufferRef MB;
|
2019-04-09 23:53:31 +02:00
|
|
|
LLVMBinaryRef O;
|
2013-10-23 19:56:37 +02:00
|
|
|
LLVMSectionIteratorRef sect;
|
|
|
|
LLVMSymbolIteratorRef sym;
|
2013-10-23 10:10:20 +02:00
|
|
|
|
2019-04-09 23:53:31 +02:00
|
|
|
char *outBufferErr = NULL;
|
|
|
|
if (LLVMCreateMemoryBufferWithSTDIN(&MB, &outBufferErr)) {
|
|
|
|
fprintf(stderr, "Error reading file: %s\n", outBufferErr);
|
|
|
|
free(outBufferErr);
|
2013-10-23 10:10:20 +02:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2019-04-09 23:53:31 +02:00
|
|
|
char *outBinaryErr = NULL;
|
|
|
|
O = LLVMCreateBinary(MB, LLVMGetGlobalContext(), &outBinaryErr);
|
|
|
|
if (!O || outBinaryErr) {
|
|
|
|
fprintf(stderr, "Error reading object: %s\n", outBinaryErr);
|
|
|
|
free(outBinaryErr);
|
2013-10-23 10:10:20 +02:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2019-04-09 23:53:31 +02:00
|
|
|
sect = LLVMObjectFileCopySectionIterator(O);
|
|
|
|
sym = LLVMObjectFileCopySymbolIterator(O);
|
|
|
|
while (sect && sym && !LLVMObjectFileIsSymbolIteratorAtEnd(O, sym)) {
|
2013-10-23 10:10:20 +02:00
|
|
|
|
|
|
|
LLVMMoveToContainingSection(sect, sym);
|
2014-04-21 15:45:32 +02:00
|
|
|
printf("%s @0x%08" PRIx64 " +%" PRIu64 " (%s)\n", LLVMGetSymbolName(sym),
|
|
|
|
LLVMGetSymbolAddress(sym), LLVMGetSymbolSize(sym),
|
2013-10-23 10:10:20 +02:00
|
|
|
LLVMGetSectionName(sect));
|
|
|
|
|
|
|
|
LLVMMoveToNextSymbol(sym);
|
|
|
|
}
|
|
|
|
|
|
|
|
LLVMDisposeSymbolIterator(sym);
|
|
|
|
|
2019-04-09 23:53:31 +02:00
|
|
|
LLVMDisposeBinary(O);
|
|
|
|
|
|
|
|
LLVMDisposeMemoryBuffer(MB);
|
2013-10-23 10:10:20 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|