Skip to content

Commit

Permalink
Initial commit. Code by Nick Kledzik. Cleanups and build system by me.
Browse files Browse the repository at this point in the history
llvm-svn: 146844
  • Loading branch information
Bigcheese committed Dec 18, 2011
1 parent 0b973d0 commit 773a8fb
Show file tree
Hide file tree
Showing 32 changed files with 2,537 additions and 0 deletions.
16 changes: 16 additions & 0 deletions lld/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#==============================================================================#
# This file specifies intentionally untracked files that git should ignore.
# See: http://www.kernel.org/pub/software/scm/git/docs/gitignore.html
#==============================================================================#

#==============================================================================#
# File extensions to be ignored anywhere in the tree.
#==============================================================================#
# Temp files created by most text editors.
*~
# Merge files created by git.
*.orig
# Byte compiled python modules.
*.pyc
# vim swap files
.*.swp
121 changes: 121 additions & 0 deletions lld/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
# If we are not building as a part of LLVM, build lld as a standalone project,
# using LLVM as an external library.

if (CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR)
project(lld)
cmake_minimum_required(VERSION 2.8)

set(LLD_PATH_TO_LLVM_SOURCE "" CACHE PATH
"Path to LLVM source code. Not necessary if using an installed LLVM.")
set(LLD_PATH_TO_LLVM_BUILD "" CACHE PATH
"Path to the directory where LLVM was built or installed.")

if (LLD_PATH_TO_LLVM_SOURCE)
if (NOT EXISTS "${LLD_PATH_TO_LLVM_SOURCE}/cmake/config-ix.cmake")
message(FATAL_ERROR "Please set LLD_PATH_TO_LLVM_SOURCE to the root "
"directory of LLVM source code.")
else()
get_filename_component(LLVM_MAIN_SRC_DIR ${LLD_PATH_TO_LLVM_SOURCE}
ABSOLUTE)
list(APPEND CMAKE_MODULE_PATH "${LLVM_MAIN_SRC_DIR}/cmake/modules")
endif()
endif()

list(APPEND CMAKE_MODULE_PATH "${LLD_PATH_TO_LLVM_BUILD}/share/llvm/cmake")

get_filename_component(PATH_TO_LLVM_BUILD ${LLD_PATH_TO_LLVM_BUILD}
ABSOLUTE)

include(AddLLVM)
include("${LLD_PATH_TO_LLVM_BUILD}/share/llvm/cmake/LLVMConfig.cmake")
include(HandleLLVMOptions)

set(PACKAGE_VERSION "${LLVM_PACKAGE_VERSION}")

set(LLVM_MAIN_INCLUDE_DIR "${LLVM_MAIN_SRC_DIR}/include")
set(LLVM_BINARY_DIR ${CMAKE_BINARY_DIR})

set(CMAKE_INCLUDE_CURRENT_DIR ON)
include_directories("${PATH_TO_LLVM_BUILD}/include"
"${LLVM_MAIN_INCLUDE_DIR}")
link_directories("${PATH_TO_LLVM_BUILD}/lib")

set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)

set(LLD_BUILT_STANDALONE 1)
endif()

set(LLD_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR})
set(LLD_BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR})

if (CMAKE_SOURCE_DIR STREQUAL CMAKE_BINARY_DIR)
message(FATAL_ERROR "In-source builds are not allowed. CMake would overwrite "
"the makefiles distributed with LLVM. Please create a directory and run cmake "
"from there, passing the path to this source directory as the last argument. "
"This process created the file `CMakeCache.txt' and the directory "
"`CMakeFiles'. Please delete them.")
endif()

macro(add_lld_library name)
llvm_process_sources(srcs ${ARGN})
if (MSVC_IDE OR XCODE)
string(REGEX MATCHALL "/[^/]+" split_path ${CMAKE_CURRENT_SOURCE_DIR})
list(GET split_path -1 dir)
file(GLOB_RECURSE headers
../../include/lld${dir}/*.h)
set(srcs ${srcs} ${headers})
endif()
if (MODULE)
set(libkind MODULE)
elseif (SHARED_LIBRARY)
set(libkind SHARED)
else()
set(libkind)
endif()
add_library(${name} ${libkind} ${srcs})
if (LLVM_COMMON_DEPENDS)
add_dependencies(${name} ${LLVM_COMMON_DEPENDS})
endif()

target_link_libraries(${name} ${LLVM_USED_LIBS})
llvm_config(${name} ${LLVM_LINK_COMPONENTS})
target_link_libraries(${name} ${LLVM_COMMON_LIBS})
link_system_libs(${name})

if(MSVC)
get_target_property(cflag ${name} COMPILE_FLAGS)
if(NOT cflag)
set(cflag "")
endif(NOT cflag)
set(cflag "${cflag} /Za")
set_target_properties(${name} PROPERTIES COMPILE_FLAGS ${cflag})
endif(MSVC)
install(TARGETS ${name}
LIBRARY DESTINATION lib${LLVM_LIBDIR_SUFFIX}
ARCHIVE DESTINATION lib${LLVM_LIBDIR_SUFFIX})
set_target_properties(${name} PROPERTIES FOLDER "lld libraries")
endmacro(add_lld_library)

macro(add_lld_executable name)
add_llvm_executable(${name} ${ARGN})
set_target_properties(${name} PROPERTIES FOLDER "lld executables")
endmacro(add_lld_executable)

include_directories(BEFORE
${CMAKE_CURRENT_BINARY_DIR}/include
${CMAKE_CURRENT_SOURCE_DIR}/include
)

install(DIRECTORY include/
DESTINATION include
FILES_MATCHING
PATTERN "*.h"
PATTERN ".svn" EXCLUDE
)

add_subdirectory(lib)
add_subdirectory(tools)

add_subdirectory(test)
56 changes: 56 additions & 0 deletions lld/include/lld/Core/AliasAtom.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
//===- Core/AliasAtom.h - Alias to another Atom ---------------------------===//
//
// The LLVM Linker
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//

#ifndef LLD_CORE_ALIAS_ATOM_H_
#define LLD_CORE_ALIAS_ATOM_H_

#include "lld/Core/Atom.h"

#include "llvm/ADT/StringRef.h"

namespace lld {

class AliasAtom : public Atom {
public:
AliasAtom(llvm::StringRef nm, const Atom &target, Atom::Scope scope)
: Atom( target.definition()
, Atom::combineNever
, scope
, target.contentType()
, target.sectionChoice()
, target.userVisibleName()
, target.deadStrip()
, target.isThumb()
, true
, target.alignment()
)
, _name(nm)
, _aliasOf(target) {}

// overrides of Atom
virtual const File *file() const {
return _aliasOf.file();
}

virtual bool translationUnitSource(llvm::StringRef &path) const {
return _aliasOf.translationUnitSource(path);
}

virtual llvm::StringRef name() const {
return _name;
}

private:
const llvm::StringRef _name;
const Atom &_aliasOf;
};

} // namespace lld

#endif // LLD_CORE_ALIAS_ATOM_H_
197 changes: 197 additions & 0 deletions lld/include/lld/Core/Atom.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,197 @@
//===- Core/Atom.h - The Fundimental Unit of Linking ----------------------===//
//
// The LLVM Linker
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//

#ifndef LLD_CORE_ATOM_H_
#define LLD_CORE_ATOM_H_

#include "lld/Core/Reference.h"

namespace llvm {
template <typename T>
class ArrayRef;

class StringRef;
}

namespace lld {

class File;

/// An atom is the fundamental unit of linking. A C function or global variable
/// is an atom. An atom has content and attributes. The content of a function
/// atom is the instructions that implement the function. The content of a
/// global variable atom is its initial bytes.
class Atom {
public:
enum Scope {
scopeTranslationUnit, // static, private to translation unit
scopeLinkageUnit, // hidden, accessible to just atoms being linked
scopeGlobal // default
};

enum Definition {
definitionRegular, // usual C/C++ function or global variable
definitionTentative, // C-only pre-ANSI support aka common
definitionAbsolute, // asm-only (foo = 10) not tied to any content
definitionUndefined, // Only in .o files to model reference to undef
definitionSharedLibrary // Only in shared libraries to model export
};

enum Combine {
combineNever, // most symbols
combineByName, // weak-definition symbol
combineByTypeContent, // simple constant that can be coalesced
combineByTypeContentDeep // complex coalescable constants
};

enum ContentType {
typeUnknown, // for use with definitionUndefined
typeCode, // executable code
typeResolver, // function which returns address of target
typeBranchIsland, // linker created for large binaries
typeBranchShim, // linker created to switch thumb mode
typeStub, // linker created for calling external function
typeStubHelper, // linker created for initial stub binding
typeConstant, // a read-only constant
typeCString, // a zero terminated UTF8 C string
typeUTF16String, // a zero terminated UTF16 string
typeCFI, // a FDE or CIE from dwarf unwind info
typeLSDA, // extra unwinding info
typeLiteral4, // a four-btye read-only constant
typeLiteral8, // an eight-btye read-only constant
typeLiteral16, // a sixteen-btye read-only constant
typeData, // read-write data
typeZeroFill, // zero-fill data
typeObjC1Class, // ObjC1 class [Darwin]
typeLazyPointer, // pointer through which a stub jumps
typeLazyDylibPointer, // pointer through which a stub jumps [Darwin]
typeCFString, // NS/CFString object [Darwin]
typeGOT, // pointer to external symbol
typeInitializerPtr, // pointer to initializer function
typeTerminatorPtr, // pointer to terminator function
typeCStringPtr, // pointer to UTF8 C string [Darwin]
typeObjCClassPtr, // pointer to ObjC class [Darwin]
typeObjC2CategoryList, // pointers to ObjC category [Darwin]
typeDTraceDOF, // runtime data for Dtrace [Darwin]
typeTempLTO, // temporary atom for bitcode reader
typeCompactUnwindInfo, // runtime data for unwinder [Darwin]
typeThunkTLV, // thunk used to access a TLV [Darwin]
typeTLVInitialData, // initial data for a TLV [Darwin]
typeTLVInitialZeroFill, // TLV initial zero fill data [Darwin]
typeTLVInitializerPtr, // pointer to thread local initializer [Darwin]
typeFirstInSection, // label for boundary of section [Darwin]
typeLastInSection, // label for boundary of section [Darwin]
};

enum ContentPermissions {
perm___ = 0, // mapped as unacessible
permR__ = 8, // mapped read-only
permR_X = 8 + 2, // mapped readable and executable
permRW_ = 8 + 4, // mapped readable and writable
permRW_L = 8 + 4 + 1, // initially mapped r/w, then made read-only
// loader writable
};

enum SectionChoice {
sectionBasedOnContent, // linker infers final section based on content
sectionCustomPreferred, // linker may place in specific section
sectionCustomRequired // linker must place in specific section
};

struct Alignment {
Alignment(int p2, int m = 0)
: powerOf2(p2)
, modulus(m) {}

uint16_t powerOf2;
uint16_t modulus;
};

// MacOSX specific compact unwind info
struct UnwindInfo {
uint32_t startOffset;
uint32_t unwindInfo;

typedef UnwindInfo *iterator;
};

// link-once (throw away if not used)??
// dll import/export

Scope scope() const { return _scope; }
Definition definition() const { return _definition; }
Combine combine() const { return _combine; }
ContentType contentType() const { return _contentType; }
Alignment alignment() const;
SectionChoice sectionChoice() const { return _sectionChoice; }
bool deadStrip() const { return _DeadStrip; }
bool isThumb() const { return _thumb; }
bool isAlias() const { return _alias; }
bool userVisibleName() const { return _userVisibleName; }
bool autoHide() const;
void setLive(bool l) { _live = l; }
bool live() const { return _live; }
void setOverridesDylibsWeakDef();

virtual const class File *file() const = 0;
virtual bool translationUnitSource(llvm::StringRef &path) const;
virtual llvm::StringRef name() const;
virtual uint64_t objectAddress() const = 0;
virtual llvm::StringRef customSectionName() const;
virtual uint64_t size() const = 0;
virtual ContentPermissions permissions() const { return perm___; }
virtual void copyRawContent(uint8_t buffer[]) const = 0;
virtual llvm::ArrayRef<uint8_t> rawContent() const;
virtual Reference::iterator referencesBegin() const;
virtual Reference::iterator referencesEnd() const;
virtual UnwindInfo::iterator beginUnwind() const;
virtual UnwindInfo::iterator endUnwind() const;

Atom( Definition d
, Combine c
, Scope s
, ContentType ct
, SectionChoice sc
, bool UserVisibleName
, bool DeadStrip
, bool IsThumb
, bool IsAlias
, Alignment a)
: _alignmentModulus(a.modulus)
, _alignmentPowerOf2(a.powerOf2)
, _definition(d)
, _combine(c)
, _userVisibleName(UserVisibleName)
, _DeadStrip(DeadStrip)
, _thumb(IsThumb)
, _alias(IsAlias)
, _contentType(ct)
, _scope(s)
, _sectionChoice(sc) {}

virtual ~Atom();

protected:
uint16_t _alignmentModulus;
uint8_t _alignmentPowerOf2;
Definition _definition : 3;
Combine _combine : 2;
bool _userVisibleName : 1;
bool _DeadStrip : 1;
bool _thumb : 1;
bool _alias : 1;
bool _live : 1;
ContentType _contentType : 8;
Scope _scope : 2;
SectionChoice _sectionChoice: 2;
};

} // namespace lld

#endif // LLD_CORE_ATOM_H_
Loading

0 comments on commit 773a8fb

Please sign in to comment.