mirror of
https://github.com/rust-lang/rust.git
synced 2025-06-05 11:48:30 +00:00
rustc: Add a binding to LLVM's object file reader. Until it's sent upstream, add an explicit "support" library for our LLVM extensions.
This commit is contained in:
parent
a8fbb53d64
commit
d34e1ff89e
34
src/Makefile
34
src/Makefile
@ -34,6 +34,7 @@ DSYMUTIL := true
|
|||||||
|
|
||||||
ifeq ($(CFG_OSTYPE), FreeBSD)
|
ifeq ($(CFG_OSTYPE), FreeBSD)
|
||||||
CFG_RUNTIME := librustrt.so
|
CFG_RUNTIME := librustrt.so
|
||||||
|
CFG_SUPPORT := librustsupport.so
|
||||||
CFG_STDLIB := libstd.so
|
CFG_STDLIB := libstd.so
|
||||||
CFG_GCC_CFLAGS += -fPIC -march=i686 -I/usr/local/include
|
CFG_GCC_CFLAGS += -fPIC -march=i686 -I/usr/local/include
|
||||||
CFG_GCC_LINK_FLAGS += -shared -fPIC -lpthread -lrt
|
CFG_GCC_LINK_FLAGS += -shared -fPIC -lpthread -lrt
|
||||||
@ -47,6 +48,7 @@ endif
|
|||||||
|
|
||||||
ifeq ($(CFG_OSTYPE), Linux)
|
ifeq ($(CFG_OSTYPE), Linux)
|
||||||
CFG_RUNTIME := librustrt.so
|
CFG_RUNTIME := librustrt.so
|
||||||
|
CFG_SUPPORT := librustsupport.so
|
||||||
CFG_STDLIB := libstd.so
|
CFG_STDLIB := libstd.so
|
||||||
CFG_GCC_CFLAGS += -fPIC -march=i686
|
CFG_GCC_CFLAGS += -fPIC -march=i686
|
||||||
CFG_GCC_LINK_FLAGS += -shared -fPIC -ldl -lpthread -lrt
|
CFG_GCC_LINK_FLAGS += -shared -fPIC -ldl -lpthread -lrt
|
||||||
@ -60,6 +62,7 @@ endif
|
|||||||
|
|
||||||
ifeq ($(CFG_OSTYPE), Darwin)
|
ifeq ($(CFG_OSTYPE), Darwin)
|
||||||
CFG_RUNTIME := librustrt.dylib
|
CFG_RUNTIME := librustrt.dylib
|
||||||
|
CFG_SUPPORT := librustsupport.dylib
|
||||||
CFG_STDLIB := libstd.dylib
|
CFG_STDLIB := libstd.dylib
|
||||||
CFG_UNIXY := 1
|
CFG_UNIXY := 1
|
||||||
CFG_GCC_LINK_FLAGS += -dynamiclib -lpthread
|
CFG_GCC_LINK_FLAGS += -dynamiclib -lpthread
|
||||||
@ -85,6 +88,7 @@ ifdef CFG_WINDOWSY
|
|||||||
CFG_NATIVE := 1
|
CFG_NATIVE := 1
|
||||||
endif
|
endif
|
||||||
CFG_RUNTIME := rustrt.dll
|
CFG_RUNTIME := rustrt.dll
|
||||||
|
CFG_SUPPORT := rustsupport.dll
|
||||||
CFG_STDLIB := std.dll
|
CFG_STDLIB := std.dll
|
||||||
CFG_EXE_SUFFIX := .exe
|
CFG_EXE_SUFFIX := .exe
|
||||||
CFG_BOOT := ./rustboot.exe
|
CFG_BOOT := ./rustboot.exe
|
||||||
@ -107,6 +111,7 @@ ifdef CFG_UNIXY
|
|||||||
CFG_GCC_CROSS := i586-mingw32msvc-
|
CFG_GCC_CROSS := i586-mingw32msvc-
|
||||||
CFG_BOOT_FLAGS += -t win32-x86-pe
|
CFG_BOOT_FLAGS += -t win32-x86-pe
|
||||||
CFG_RUNTIME := rustrt.dll
|
CFG_RUNTIME := rustrt.dll
|
||||||
|
CFG_SUPPORT := rustsupport.dll
|
||||||
CFG_STDLIB := std.dll
|
CFG_STDLIB := std.dll
|
||||||
CFG_RUSTC := ./rustc.exe
|
CFG_RUSTC := ./rustc.exe
|
||||||
ifdef CFG_VALGRIND
|
ifdef CFG_VALGRIND
|
||||||
@ -179,7 +184,7 @@ endif
|
|||||||
ifneq ($(CFG_LLVM_CONFIG),)
|
ifneq ($(CFG_LLVM_CONFIG),)
|
||||||
CFG_LLVM_VERSION := $(shell $(CFG_LLVM_CONFIG) --version)
|
CFG_LLVM_VERSION := $(shell $(CFG_LLVM_CONFIG) --version)
|
||||||
$(info cfg: found llvm-config at $(CFG_LLVM_CONFIG))
|
$(info cfg: found llvm-config at $(CFG_LLVM_CONFIG))
|
||||||
CFG_LLVM_ALLOWED_VERSIONS := 2.8svn 2.8 2.9svn
|
CFG_LLVM_ALLOWED_VERSIONS := 2.8svn 2.8 2.9svn 3.0svn
|
||||||
ifneq ($(findstring $(CFG_LLVM_VERSION),$(CFG_LLVM_ALLOWED_VERSIONS)),)
|
ifneq ($(findstring $(CFG_LLVM_VERSION),$(CFG_LLVM_ALLOWED_VERSIONS)),)
|
||||||
$(info cfg: using LLVM version $(CFG_LLVM_VERSION))
|
$(info cfg: using LLVM version $(CFG_LLVM_VERSION))
|
||||||
else
|
else
|
||||||
@ -193,6 +198,10 @@ ifdef CFG_LLVM_CONFIG
|
|||||||
LLC := "$(shell $(CFG_LLVM_CONFIG) --bindir)/llc"
|
LLC := "$(shell $(CFG_LLVM_CONFIG) --bindir)/llc"
|
||||||
CFG_LLC_CFLAGS := -march=x86
|
CFG_LLC_CFLAGS := -march=x86
|
||||||
LLVM-DIS := "$(shell $(CFG_LLVM_CONFIG) --bindir)/llvm-dis"
|
LLVM-DIS := "$(shell $(CFG_LLVM_CONFIG) --bindir)/llvm-dis"
|
||||||
|
CFG_LLVM_INCDIR := $(shell $(CFG_LLVM_CONFIG) --includedir)
|
||||||
|
CFG_LLVM_CXXFLAGS := $(shell $(CFG_LLVM_CONFIG) --cxxflags)
|
||||||
|
CFG_LLVM_LDFLAGS := $(shell $(CFG_LLVM_CONFIG) --ldflags)
|
||||||
|
CFG_LLVM_LIBS := $(shell $(CFG_LLVM_CONFIG) --libs)
|
||||||
endif
|
endif
|
||||||
|
|
||||||
MKFILES := Makefile
|
MKFILES := Makefile
|
||||||
@ -301,6 +310,14 @@ RUNTIME_HDR := rt/globals.h \
|
|||||||
RUNTIME_INCS := -Irt/isaac -Irt/uthash
|
RUNTIME_INCS := -Irt/isaac -Irt/uthash
|
||||||
RUNTIME_OBJS := $(RUNTIME_CS:.cpp=.o)
|
RUNTIME_OBJS := $(RUNTIME_CS:.cpp=.o)
|
||||||
|
|
||||||
|
SUPPORT_CS := support/Object.cpp
|
||||||
|
|
||||||
|
SUPPORT_HDR := support/include/llvm-c/Object.h
|
||||||
|
|
||||||
|
SUPPORT_INCS := -iquote $(CFG_LLVM_INCDIR)
|
||||||
|
SUPPORT_OBJS := $(SUPPORT_CS:.cpp=.o)
|
||||||
|
SUPPORT_LIBS := $(CFG_LLVM_LDFLAGS) $(CFG_LLVM_LIBS)
|
||||||
|
|
||||||
STDLIB_CRATE := lib/std.rc
|
STDLIB_CRATE := lib/std.rc
|
||||||
STDLIB_INPUTS := $(wildcard lib/*.rc lib/*.rs lib/*/*.rs)
|
STDLIB_INPUTS := $(wildcard lib/*.rc lib/*.rs lib/*/*.rs)
|
||||||
COMPILER_CRATE := comp/rustc.rc
|
COMPILER_CRATE := comp/rustc.rc
|
||||||
@ -321,14 +338,24 @@ $(CFG_RUNTIME): $(RUNTIME_OBJS) $(MKFILES) $(RUNTIME_HDR)
|
|||||||
@$(call CFG_ECHO, compile: $<)
|
@$(call CFG_ECHO, compile: $<)
|
||||||
$(CFG_QUIET)$(call CFG_LINK_C, $@) $(RUNTIME_OBJS)
|
$(CFG_QUIET)$(call CFG_LINK_C, $@) $(RUNTIME_OBJS)
|
||||||
|
|
||||||
|
$(CFG_SUPPORT): $(SUPPORT_OBJS) $(MKFILES) $(SUPPORT_HDR)
|
||||||
|
@$(call CFG_ECHO, compile: $<)
|
||||||
|
$(CFG_QUIET)$(call CFG_LINK_C, $@ $(CFG_LLVM_LDFLAGS) $(CFG_LLVM_LIBS)) \
|
||||||
|
$(SUPPORT_OBJS)
|
||||||
|
|
||||||
$(CFG_STDLIB): $(STDLIB_CRATE) $(CFG_BOOT) $(MKFILES)
|
$(CFG_STDLIB): $(STDLIB_CRATE) $(CFG_BOOT) $(MKFILES)
|
||||||
@$(call CFG_ECHO, compile: $<)
|
@$(call CFG_ECHO, compile: $<)
|
||||||
$(BOOT) -shared -o $@ $(STDLIB_CRATE)
|
$(BOOT) -shared -o $@ $(STDLIB_CRATE)
|
||||||
|
|
||||||
%.o: %.cpp $(MKFILES)
|
rt/%.o: rt/%.cpp $(MKFILES)
|
||||||
@$(call CFG_ECHO, compile: $<)
|
@$(call CFG_ECHO, compile: $<)
|
||||||
$(CFG_QUIET)$(call CFG_COMPILE_C, $@, $(RUNTIME_INCS)) $<
|
$(CFG_QUIET)$(call CFG_COMPILE_C, $@, $(RUNTIME_INCS)) $<
|
||||||
|
|
||||||
|
support/%.o: support/%.cpp $(MKFILES)
|
||||||
|
@$(call CFG_ECHO, compile: $<)
|
||||||
|
$(CFG_QUIET)$(call CFG_COMPILE_C, $@, $(CFG_LLVM_CXXFLAGS) \
|
||||||
|
$(SUPPORT_INCS)) $<
|
||||||
|
|
||||||
ifdef CFG_NATIVE
|
ifdef CFG_NATIVE
|
||||||
$(CFG_BOOT): $(BOOT_CMXS) $(MKFILES)
|
$(CFG_BOOT): $(BOOT_CMXS) $(MKFILES)
|
||||||
@$(call CFG_ECHO, compile: $<)
|
@$(call CFG_ECHO, compile: $<)
|
||||||
@ -359,7 +386,8 @@ endif
|
|||||||
# Main compiler targets and rules
|
# Main compiler targets and rules
|
||||||
######################################################################
|
######################################################################
|
||||||
|
|
||||||
$(CFG_RUSTC): $(COMPILER_INPUTS) $(CFG_BOOT) $(CFG_RUNTIME) $(CFG_STDLIB)
|
$(CFG_RUSTC): $(COMPILER_INPUTS) $(CFG_BOOT) $(CFG_RUNTIME) $(CFG_STDLIB) \
|
||||||
|
$(CFG_SUPPORT)
|
||||||
@$(call CFG_ECHO, compile: $<)
|
@$(call CFG_ECHO, compile: $<)
|
||||||
$(BOOT) -minimal -o $@ $<
|
$(BOOT) -minimal -o $@ $<
|
||||||
$(CFG_QUIET)chmod 0755 $@
|
$(CFG_QUIET)chmod 0755 $@
|
||||||
|
55
src/support/Object.cpp
Normal file
55
src/support/Object.cpp
Normal file
@ -0,0 +1,55 @@
|
|||||||
|
//===- Object.cpp - C bindings to the object file library--------*- C++ -*-===//
|
||||||
|
//
|
||||||
|
// The LLVM Compiler Infrastructure
|
||||||
|
//
|
||||||
|
// This file is distributed under the University of Illinois Open Source
|
||||||
|
// License. See LICENSE.TXT for details.
|
||||||
|
//
|
||||||
|
//===----------------------------------------------------------------------===//
|
||||||
|
//
|
||||||
|
// This file defines the C bindings to the file-format-independent object
|
||||||
|
// library.
|
||||||
|
//
|
||||||
|
//===----------------------------------------------------------------------===//
|
||||||
|
|
||||||
|
#include "llvm/Object/ObjectFile.h"
|
||||||
|
#include "llvm-c/Object.h"
|
||||||
|
|
||||||
|
using namespace llvm;
|
||||||
|
using namespace object;
|
||||||
|
|
||||||
|
LLVMObjectFileRef LLVMCreateObjectFile(const char *ObjectPath) {
|
||||||
|
StringRef SR(ObjectPath);
|
||||||
|
return wrap(ObjectFile::createObjectFile(SR));
|
||||||
|
}
|
||||||
|
|
||||||
|
void LLVMDisposeObjectFile(LLVMObjectFileRef ObjectFile) {
|
||||||
|
delete unwrap(ObjectFile);
|
||||||
|
}
|
||||||
|
|
||||||
|
LLVMSectionIteratorRef LLVMGetSections(LLVMObjectFileRef ObjectFile) {
|
||||||
|
ObjectFile::section_iterator SI = unwrap(ObjectFile)->begin_sections();
|
||||||
|
return wrap(new ObjectFile::section_iterator(SI));
|
||||||
|
}
|
||||||
|
|
||||||
|
void LLVMDisposeSectionIterator(LLVMSectionIteratorRef SI) {
|
||||||
|
delete unwrap(SI);
|
||||||
|
}
|
||||||
|
|
||||||
|
void LLVMMoveToNextSection(LLVMSectionIteratorRef SI) {
|
||||||
|
ObjectFile::section_iterator UnwrappedSI = *unwrap(SI);
|
||||||
|
++UnwrappedSI;
|
||||||
|
}
|
||||||
|
|
||||||
|
const char *LLVMGetSectionName(LLVMSectionIteratorRef SI) {
|
||||||
|
return (*unwrap(SI))->getName().data();
|
||||||
|
}
|
||||||
|
|
||||||
|
uint64_t LLVMGetSectionSize(LLVMSectionIteratorRef SI) {
|
||||||
|
return (*unwrap(SI))->getSize();
|
||||||
|
}
|
||||||
|
|
||||||
|
const char *LLVMGetSectionContents(LLVMSectionIteratorRef SI) {
|
||||||
|
return (*unwrap(SI))->getContents().data();
|
||||||
|
}
|
||||||
|
|
3
src/support/README
Normal file
3
src/support/README
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
This directory currently contains some LLVM support code. This will generally
|
||||||
|
be sent upstream to LLVM in time; for now it lives here.
|
||||||
|
|
75
src/support/include/llvm-c/Object.h
Normal file
75
src/support/include/llvm-c/Object.h
Normal file
@ -0,0 +1,75 @@
|
|||||||
|
/*===-- llvm-c/Object.h - Object Lib C Iface --------------------*- C++ -*-===*/
|
||||||
|
/* */
|
||||||
|
/* The LLVM Compiler Infrastructure */
|
||||||
|
/* */
|
||||||
|
/* This file is distributed under the University of Illinois Open Source */
|
||||||
|
/* License. See LICENSE.TXT for details. */
|
||||||
|
/* */
|
||||||
|
/*===----------------------------------------------------------------------===*/
|
||||||
|
/* */
|
||||||
|
/* This header declares the C interface to libLLVMObject.a, which */
|
||||||
|
/* implements object file reading and writing. */
|
||||||
|
/* */
|
||||||
|
/* Many exotic languages can interoperate with C code but have a harder time */
|
||||||
|
/* with C++ due to name mangling. So in addition to C, this interface enables */
|
||||||
|
/* tools written in such languages. */
|
||||||
|
/* */
|
||||||
|
/*===----------------------------------------------------------------------===*/
|
||||||
|
|
||||||
|
#ifndef LLVM_C_OBJECT_H
|
||||||
|
#define LLVM_C_OBJECT_H
|
||||||
|
|
||||||
|
#include "llvm-c/Core.h"
|
||||||
|
#include "llvm/Config/llvm-config.h"
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
#include "llvm/Object/ObjectFile.h"
|
||||||
|
|
||||||
|
extern "C" {
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
typedef struct LLVMOpaqueObjectFile *LLVMObjectFileRef;
|
||||||
|
|
||||||
|
typedef struct LLVMOpaqueSectionIterator *LLVMSectionIteratorRef;
|
||||||
|
|
||||||
|
LLVMObjectFileRef LLVMCreateObjectFile(const char *ObjectPath);
|
||||||
|
void LLVMDisposeObjectFile(LLVMObjectFileRef ObjectFile);
|
||||||
|
|
||||||
|
LLVMSectionIteratorRef LLVMGetSections(LLVMObjectFileRef ObjectFile);
|
||||||
|
void LLVMDisposeSectionIterator(LLVMSectionIteratorRef SI);
|
||||||
|
void LLVMMoveToNextSection(LLVMSectionIteratorRef SI);
|
||||||
|
const char *LLVMGetSectionName(LLVMSectionIteratorRef SI);
|
||||||
|
uint64_t LLVMGetSectionSize(LLVMSectionIteratorRef SI);
|
||||||
|
const char *LLVMGetSectionContents(LLVMSectionIteratorRef SI);
|
||||||
|
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
}
|
||||||
|
|
||||||
|
namespace llvm {
|
||||||
|
namespace object {
|
||||||
|
inline ObjectFile *unwrap(LLVMObjectFileRef OF) {
|
||||||
|
return reinterpret_cast<ObjectFile*>(OF);
|
||||||
|
}
|
||||||
|
|
||||||
|
inline LLVMObjectFileRef wrap(const ObjectFile *OF) {
|
||||||
|
return reinterpret_cast<LLVMObjectFileRef>(const_cast<ObjectFile*>(OF));
|
||||||
|
}
|
||||||
|
|
||||||
|
inline ObjectFile::section_iterator *unwrap(LLVMSectionIteratorRef SI) {
|
||||||
|
return reinterpret_cast<ObjectFile::section_iterator*>(SI);
|
||||||
|
}
|
||||||
|
|
||||||
|
inline LLVMSectionIteratorRef
|
||||||
|
wrap(const ObjectFile::section_iterator *SI) {
|
||||||
|
return reinterpret_cast<LLVMSectionIteratorRef>
|
||||||
|
(const_cast<ObjectFile::section_iterator*>(SI));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif /* defined(__cplusplus) */
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
Loading…
Reference in New Issue
Block a user