2013-10-23 08:10:20 +00:00
|
|
|
/*===-- object.c - tool for testing libLLVM and llvm-c API ----------------===*\
|
|
|
|
|* *|
|
2019-01-19 08:50:56 +00: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 08:10:20 +00: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 13:31:14 +00:00
|
|
|
int llvm_object_list_sections(void) {
|
2013-10-23 08:10:20 +00:00
|
|
|
LLVMMemoryBufferRef MB;
|
2019-04-09 21:53:31 +00:00
|
|
|
LLVMBinaryRef O;
|
2013-10-23 17:56:37 +00:00
|
|
|
LLVMSectionIteratorRef sect;
|
2013-10-23 08:10:20 +00:00
|
|
|
|
2019-04-09 21:53:31 +00:00
|
|
|
char *outBufferErr = NULL;
|
|
|
|
if (LLVMCreateMemoryBufferWithSTDIN(&MB, &outBufferErr)) {
|
|
|
|
fprintf(stderr, "Error reading file: %s\n", outBufferErr);
|
|
|
|
free(outBufferErr);
|
2013-10-23 08:10:20 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2019-04-09 21:53:31 +00: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 08:10:20 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2019-04-09 21:53:31 +00:00
|
|
|
sect = LLVMObjectFileCopySectionIterator(O);
|
|
|
|
while (sect && !LLVMObjectFileIsSectionIteratorAtEnd(O, sect)) {
|
2013-10-23 08:10:20 +00:00
|
|
|
printf("'%s': @0x%08" PRIx64 " +%" PRIu64 "\n", LLVMGetSectionName(sect),
|
|
|
|
LLVMGetSectionAddress(sect), LLVMGetSectionSize(sect));
|
|
|
|
|
|
|
|
LLVMMoveToNextSection(sect);
|
|
|
|
}
|
|
|
|
|
|
|
|
LLVMDisposeSectionIterator(sect);
|
|
|
|
|
2019-04-09 21:53:31 +00:00
|
|
|
LLVMDisposeBinary(O);
|
|
|
|
|
|
|
|
LLVMDisposeMemoryBuffer(MB);
|
2013-10-23 08:10:20 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-02-05 13:31:14 +00:00
|
|
|
int llvm_object_list_symbols(void) {
|
2013-10-23 08:10:20 +00:00
|
|
|
LLVMMemoryBufferRef MB;
|
2019-04-09 21:53:31 +00:00
|
|
|
LLVMBinaryRef O;
|
2013-10-23 17:56:37 +00:00
|
|
|
LLVMSectionIteratorRef sect;
|
|
|
|
LLVMSymbolIteratorRef sym;
|
2013-10-23 08:10:20 +00:00
|
|
|
|
2019-04-09 21:53:31 +00:00
|
|
|
char *outBufferErr = NULL;
|
|
|
|
if (LLVMCreateMemoryBufferWithSTDIN(&MB, &outBufferErr)) {
|
|
|
|
fprintf(stderr, "Error reading file: %s\n", outBufferErr);
|
|
|
|
free(outBufferErr);
|
2013-10-23 08:10:20 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2019-04-09 21:53:31 +00: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 08:10:20 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2019-04-09 21:53:31 +00:00
|
|
|
sect = LLVMObjectFileCopySectionIterator(O);
|
|
|
|
sym = LLVMObjectFileCopySymbolIterator(O);
|
|
|
|
while (sect && sym && !LLVMObjectFileIsSymbolIteratorAtEnd(O, sym)) {
|
2013-10-23 08:10:20 +00:00
|
|
|
|
|
|
|
LLVMMoveToContainingSection(sect, sym);
|
2014-04-21 13:45:32 +00:00
|
|
|
printf("%s @0x%08" PRIx64 " +%" PRIu64 " (%s)\n", LLVMGetSymbolName(sym),
|
|
|
|
LLVMGetSymbolAddress(sym), LLVMGetSymbolSize(sym),
|
2013-10-23 08:10:20 +00:00
|
|
|
LLVMGetSectionName(sect));
|
|
|
|
|
|
|
|
LLVMMoveToNextSymbol(sym);
|
|
|
|
}
|
|
|
|
|
|
|
|
LLVMDisposeSymbolIterator(sym);
|
|
|
|
|
2019-04-09 21:53:31 +00:00
|
|
|
LLVMDisposeBinary(O);
|
|
|
|
|
|
|
|
LLVMDisposeMemoryBuffer(MB);
|
2013-10-23 08:10:20 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|