diff --git a/rocclr/compiler/lib/Doxyfile b/rocclr/compiler/lib/Doxyfile new file mode 100644 index 0000000000..1c59052d73 --- /dev/null +++ b/rocclr/compiler/lib/Doxyfile @@ -0,0 +1,1606 @@ +# Doxyfile 1.6.3 + +# This file describes the settings to be used by the documentation system +# doxygen (www.doxygen.org) for a project +# +# All text after a hash (#) is considered a comment and will be ignored +# The format is: +# TAG = value [value, ...] +# For lists items can also be appended using: +# TAG += value [value, ...] +# Values that contain spaces should be placed between quotes (" ") + +#--------------------------------------------------------------------------- +# Project related configuration options +#--------------------------------------------------------------------------- + +# This tag specifies the encoding used for all characters in the config file +# that follow. The default is UTF-8 which is also the encoding used for all +# text before the first occurrence of this tag. Doxygen uses libiconv (or the +# iconv built into libc) for the transcoding. See +# http://www.gnu.org/software/libiconv for the list of possible encodings. + +DOXYFILE_ENCODING = UTF-8 + +# The PROJECT_NAME tag is a single word (or a sequence of words surrounded +# by quotes) that should identify the project. + +PROJECT_NAME = CompilerLibrary + +# The PROJECT_NUMBER tag can be used to enter a project or revision number. +# This could be handy for archiving the generated documentation or +# if some version control system is used. + +PROJECT_NUMBER = 0.9 + +# The OUTPUT_DIRECTORY tag is used to specify the (relative or absolute) +# base path where the generated documentation will be put. +# If a relative path is entered, it will be relative to the location +# where doxygen was started. If left blank the current directory will be used. + +OUTPUT_DIRECTORY = D:/hq/stg/opencl/drivers/opencl/compiler/lib/docs + +# If the CREATE_SUBDIRS tag is set to YES, then doxygen will create +# 4096 sub-directories (in 2 levels) under the output directory of each output +# format and will distribute the generated files over these directories. +# Enabling this option can be useful when feeding doxygen a huge amount of +# source files, where putting all generated files in the same directory would +# otherwise cause performance problems for the file system. + +CREATE_SUBDIRS = NO + +# The OUTPUT_LANGUAGE tag is used to specify the language in which all +# documentation generated by doxygen is written. Doxygen will use this +# information to generate all constant output in the proper language. +# The default language is English, other supported languages are: +# Afrikaans, Arabic, Brazilian, Catalan, Chinese, Chinese-Traditional, +# Croatian, Czech, Danish, Dutch, Esperanto, Farsi, Finnish, French, German, +# Greek, Hungarian, Italian, Japanese, Japanese-en (Japanese with English +# messages), Korean, Korean-en, Lithuanian, Norwegian, Macedonian, Persian, +# Polish, Portuguese, Romanian, Russian, Serbian, Serbian-Cyrilic, Slovak, +# Slovene, Spanish, Swedish, Ukrainian, and Vietnamese. + +OUTPUT_LANGUAGE = English + +# If the BRIEF_MEMBER_DESC tag is set to YES (the default) Doxygen will +# include brief member descriptions after the members that are listed in +# the file and class documentation (similar to JavaDoc). +# Set to NO to disable this. + +BRIEF_MEMBER_DESC = YES + +# If the REPEAT_BRIEF tag is set to YES (the default) Doxygen will prepend +# the brief description of a member or function before the detailed description. +# Note: if both HIDE_UNDOC_MEMBERS and BRIEF_MEMBER_DESC are set to NO, the +# brief descriptions will be completely suppressed. + +REPEAT_BRIEF = YES + +# This tag implements a quasi-intelligent brief description abbreviator +# that is used to form the text in various listings. Each string +# in this list, if found as the leading text of the brief description, will be +# stripped from the text and the result after processing the whole list, is +# used as the annotated text. Otherwise, the brief description is used as-is. +# If left blank, the following values are used ("$name" is automatically +# replaced with the name of the entity): "The $name class" "The $name widget" +# "The $name file" "is" "provides" "specifies" "contains" +# "represents" "a" "an" "the" + +ABBREVIATE_BRIEF = "The $name class" \ + "The $name widget" \ + "The $name file" \ + is \ + provides \ + specifies \ + contains \ + represents \ + a \ + an \ + the + +# If the ALWAYS_DETAILED_SEC and REPEAT_BRIEF tags are both set to YES then +# Doxygen will generate a detailed section even if there is only a brief +# description. + +ALWAYS_DETAILED_SEC = NO + +# If the INLINE_INHERITED_MEMB tag is set to YES, doxygen will show all +# inherited members of a class in the documentation of that class as if those +# members were ordinary class members. Constructors, destructors and assignment +# operators of the base classes will not be shown. + +INLINE_INHERITED_MEMB = NO + +# If the FULL_PATH_NAMES tag is set to YES then Doxygen will prepend the full +# path before files name in the file list and in the header files. If set +# to NO the shortest path that makes the file name unique will be used. + +FULL_PATH_NAMES = NO + +# If the FULL_PATH_NAMES tag is set to YES then the STRIP_FROM_PATH tag +# can be used to strip a user-defined part of the path. Stripping is +# only done if one of the specified strings matches the left-hand part of +# the path. The tag can be used to show relative paths in the file list. +# If left blank the directory from which doxygen is run is used as the +# path to strip. + +STRIP_FROM_PATH = + +# The STRIP_FROM_INC_PATH tag can be used to strip a user-defined part of +# the path mentioned in the documentation of a class, which tells +# the reader which header file to include in order to use a class. +# If left blank only the name of the header file containing the class +# definition is used. Otherwise one should specify the include paths that +# are normally passed to the compiler using the -I flag. + +STRIP_FROM_INC_PATH = + +# If the SHORT_NAMES tag is set to YES, doxygen will generate much shorter +# (but less readable) file names. This can be useful is your file systems +# doesn't support long names like on DOS, Mac, or CD-ROM. + +SHORT_NAMES = NO + +# If the JAVADOC_AUTOBRIEF tag is set to YES then Doxygen +# will interpret the first line (until the first dot) of a JavaDoc-style +# comment as the brief description. If set to NO, the JavaDoc +# comments will behave just like regular Qt-style comments +# (thus requiring an explicit @brief command for a brief description.) + +JAVADOC_AUTOBRIEF = NO + +# If the QT_AUTOBRIEF tag is set to YES then Doxygen will +# interpret the first line (until the first dot) of a Qt-style +# comment as the brief description. If set to NO, the comments +# will behave just like regular Qt-style comments (thus requiring +# an explicit \brief command for a brief description.) + +QT_AUTOBRIEF = NO + +# The MULTILINE_CPP_IS_BRIEF tag can be set to YES to make Doxygen +# treat a multi-line C++ special comment block (i.e. a block of //! or /// +# comments) as a brief description. This used to be the default behaviour. +# The new default is to treat a multi-line C++ comment block as a detailed +# description. Set this tag to YES if you prefer the old behaviour instead. + +MULTILINE_CPP_IS_BRIEF = NO + +# If the INHERIT_DOCS tag is set to YES (the default) then an undocumented +# member inherits the documentation from any documented member that it +# re-implements. + +INHERIT_DOCS = YES + +# If the SEPARATE_MEMBER_PAGES tag is set to YES, then doxygen will produce +# a new page for each member. If set to NO, the documentation of a member will +# be part of the file/class/namespace that contains it. + +SEPARATE_MEMBER_PAGES = NO + +# The TAB_SIZE tag can be used to set the number of spaces in a tab. +# Doxygen uses this value to replace tabs by spaces in code fragments. + +TAB_SIZE = 8 + +# This tag can be used to specify a number of aliases that acts +# as commands in the documentation. An alias has the form "name=value". +# For example adding "sideeffect=\par Side Effects:\n" will allow you to +# put the command \sideeffect (or @sideeffect) in the documentation, which +# will result in a user-defined paragraph with heading "Side Effects:". +# You can put \n's in the value part of an alias to insert newlines. + +ALIASES = + +# Set the OPTIMIZE_OUTPUT_FOR_C tag to YES if your project consists of C +# sources only. Doxygen will then generate output that is more tailored for C. +# For instance, some of the names that are used will be different. The list +# of all members will be omitted, etc. + +OPTIMIZE_OUTPUT_FOR_C = YES + +# Set the OPTIMIZE_OUTPUT_JAVA tag to YES if your project consists of Java +# sources only. Doxygen will then generate output that is more tailored for +# Java. For instance, namespaces will be presented as packages, qualified +# scopes will look different, etc. + +OPTIMIZE_OUTPUT_JAVA = NO + +# Set the OPTIMIZE_FOR_FORTRAN tag to YES if your project consists of Fortran +# sources only. Doxygen will then generate output that is more tailored for +# Fortran. + +OPTIMIZE_FOR_FORTRAN = NO + +# Set the OPTIMIZE_OUTPUT_VHDL tag to YES if your project consists of VHDL +# sources. Doxygen will then generate output that is tailored for +# VHDL. + +OPTIMIZE_OUTPUT_VHDL = NO + +# Doxygen selects the parser to use depending on the extension of the files it parses. +# With this tag you can assign which parser to use for a given extension. +# Doxygen has a built-in mapping, but you can override or extend it using this tag. +# The format is ext=language, where ext is a file extension, and language is one of +# the parsers supported by doxygen: IDL, Java, Javascript, C#, C, C++, D, PHP, +# Objective-C, Python, Fortran, VHDL, C, C++. For instance to make doxygen treat +# .inc files as Fortran files (default is PHP), and .f files as C (default is Fortran), +# use: inc=Fortran f=C. Note that for custom extensions you also need to set +# FILE_PATTERNS otherwise the files are not read by doxygen. + +EXTENSION_MAPPING = + +# If you use STL classes (i.e. std::string, std::vector, etc.) but do not want +# to include (a tag file for) the STL sources as input, then you should +# set this tag to YES in order to let doxygen match functions declarations and +# definitions whose arguments contain STL classes (e.g. func(std::string); v.s. +# func(std::string) {}). This also make the inheritance and collaboration +# diagrams that involve STL classes more complete and accurate. + +BUILTIN_STL_SUPPORT = YES + +# If you use Microsoft's C++/CLI language, you should set this option to YES to +# enable parsing support. + +CPP_CLI_SUPPORT = NO + +# Set the SIP_SUPPORT tag to YES if your project consists of sip sources only. +# Doxygen will parse them like normal C++ but will assume all classes use public +# instead of private inheritance when no explicit protection keyword is present. + +SIP_SUPPORT = NO + +# For Microsoft's IDL there are propget and propput attributes to indicate getter +# and setter methods for a property. Setting this option to YES (the default) +# will make doxygen to replace the get and set methods by a property in the +# documentation. This will only work if the methods are indeed getting or +# setting a simple type. If this is not the case, or you want to show the +# methods anyway, you should set this option to NO. + +IDL_PROPERTY_SUPPORT = YES + +# If member grouping is used in the documentation and the DISTRIBUTE_GROUP_DOC +# tag is set to YES, then doxygen will reuse the documentation of the first +# member in the group (if any) for the other members of the group. By default +# all members of a group must be documented explicitly. + +DISTRIBUTE_GROUP_DOC = YES + +# Set the SUBGROUPING tag to YES (the default) to allow class member groups of +# the same type (for instance a group of public functions) to be put as a +# subgroup of that type (e.g. under the Public Functions section). Set it to +# NO to prevent subgrouping. Alternatively, this can be done per class using +# the \nosubgrouping command. + +SUBGROUPING = YES + +# When TYPEDEF_HIDES_STRUCT is enabled, a typedef of a struct, union, or enum +# is documented as struct, union, or enum with the name of the typedef. So +# typedef struct TypeS {} TypeT, will appear in the documentation as a struct +# with name TypeT. When disabled the typedef will appear as a member of a file, +# namespace, or class. And the struct will be named TypeS. This can typically +# be useful for C code in case the coding convention dictates that all compound +# types are typedef'ed and only the typedef is referenced, never the tag name. + +TYPEDEF_HIDES_STRUCT = YES + +# The SYMBOL_CACHE_SIZE determines the size of the internal cache use to +# determine which symbols to keep in memory and which to flush to disk. +# When the cache is full, less often used symbols will be written to disk. +# For small to medium size projects (<1000 input files) the default value is +# probably good enough. For larger projects a too small cache size can cause +# doxygen to be busy swapping symbols to and from disk most of the time +# causing a significant performance penality. +# If the system has enough physical memory increasing the cache will improve the +# performance by keeping more symbols in memory. Note that the value works on +# a logarithmic scale so increasing the size by one will rougly double the +# memory usage. The cache size is given by this formula: +# 2^(16+SYMBOL_CACHE_SIZE). The valid range is 0..9, the default is 0, +# corresponding to a cache size of 2^16 = 65536 symbols + +SYMBOL_CACHE_SIZE = 0 + +#--------------------------------------------------------------------------- +# Build related configuration options +#--------------------------------------------------------------------------- + +# If the EXTRACT_ALL tag is set to YES doxygen will assume all entities in +# documentation are documented, even if no documentation was available. +# Private class members and static file members will be hidden unless +# the EXTRACT_PRIVATE and EXTRACT_STATIC tags are set to YES + +EXTRACT_ALL = YES + +# If the EXTRACT_PRIVATE tag is set to YES all private members of a class +# will be included in the documentation. + +EXTRACT_PRIVATE = YES + +# If the EXTRACT_STATIC tag is set to YES all static members of a file +# will be included in the documentation. + +EXTRACT_STATIC = YES + +# If the EXTRACT_LOCAL_CLASSES tag is set to YES classes (and structs) +# defined locally in source files will be included in the documentation. +# If set to NO only classes defined in header files are included. + +EXTRACT_LOCAL_CLASSES = YES + +# This flag is only useful for Objective-C code. When set to YES local +# methods, which are defined in the implementation section but not in +# the interface are included in the documentation. +# If set to NO (the default) only methods in the interface are included. + +EXTRACT_LOCAL_METHODS = YES + +# If this flag is set to YES, the members of anonymous namespaces will be +# extracted and appear in the documentation as a namespace called +# 'anonymous_namespace{file}', where file will be replaced with the base +# name of the file that contains the anonymous namespace. By default +# anonymous namespace are hidden. + +EXTRACT_ANON_NSPACES = YES + +# If the HIDE_UNDOC_MEMBERS tag is set to YES, Doxygen will hide all +# undocumented members of documented classes, files or namespaces. +# If set to NO (the default) these members will be included in the +# various overviews, but no documentation section is generated. +# This option has no effect if EXTRACT_ALL is enabled. + +HIDE_UNDOC_MEMBERS = NO + +# If the HIDE_UNDOC_CLASSES tag is set to YES, Doxygen will hide all +# undocumented classes that are normally visible in the class hierarchy. +# If set to NO (the default) these classes will be included in the various +# overviews. This option has no effect if EXTRACT_ALL is enabled. + +HIDE_UNDOC_CLASSES = NO + +# If the HIDE_FRIEND_COMPOUNDS tag is set to YES, Doxygen will hide all +# friend (class|struct|union) declarations. +# If set to NO (the default) these declarations will be included in the +# documentation. + +HIDE_FRIEND_COMPOUNDS = NO + +# If the HIDE_IN_BODY_DOCS tag is set to YES, Doxygen will hide any +# documentation blocks found inside the body of a function. +# If set to NO (the default) these blocks will be appended to the +# function's detailed documentation block. + +HIDE_IN_BODY_DOCS = NO + +# The INTERNAL_DOCS tag determines if documentation +# that is typed after a \internal command is included. If the tag is set +# to NO (the default) then the documentation will be excluded. +# Set it to YES to include the internal documentation. + +INTERNAL_DOCS = NO + +# If the CASE_SENSE_NAMES tag is set to NO then Doxygen will only generate +# file names in lower-case letters. If set to YES upper-case letters are also +# allowed. This is useful if you have classes or files whose names only differ +# in case and if your file system supports case sensitive file names. Windows +# and Mac users are advised to set this option to NO. + +CASE_SENSE_NAMES = NO + +# If the HIDE_SCOPE_NAMES tag is set to NO (the default) then Doxygen +# will show members with their full class and namespace scopes in the +# documentation. If set to YES the scope will be hidden. + +HIDE_SCOPE_NAMES = NO + +# If the SHOW_INCLUDE_FILES tag is set to YES (the default) then Doxygen +# will put a list of the files that are included by a file in the documentation +# of that file. + +SHOW_INCLUDE_FILES = YES + +# If the FORCE_LOCAL_INCLUDES tag is set to YES then Doxygen +# will list include files with double quotes in the documentation +# rather than with sharp brackets. + +FORCE_LOCAL_INCLUDES = NO + +# If the INLINE_INFO tag is set to YES (the default) then a tag [inline] +# is inserted in the documentation for inline members. + +INLINE_INFO = YES + +# If the SORT_MEMBER_DOCS tag is set to YES (the default) then doxygen +# will sort the (detailed) documentation of file and class members +# alphabetically by member name. If set to NO the members will appear in +# declaration order. + +SORT_MEMBER_DOCS = YES + +# If the SORT_BRIEF_DOCS tag is set to YES then doxygen will sort the +# brief documentation of file, namespace and class members alphabetically +# by member name. If set to NO (the default) the members will appear in +# declaration order. + +SORT_BRIEF_DOCS = NO + +# If the SORT_MEMBERS_CTORS_1ST tag is set to YES then doxygen +# will sort the (brief and detailed) documentation of class members so that +# constructors and destructors are listed first. If set to NO (the default) +# the constructors will appear in the respective orders defined by +# SORT_MEMBER_DOCS and SORT_BRIEF_DOCS. +# This tag will be ignored for brief docs if SORT_BRIEF_DOCS is set to NO +# and ignored for detailed docs if SORT_MEMBER_DOCS is set to NO. + +SORT_MEMBERS_CTORS_1ST = NO + +# If the SORT_GROUP_NAMES tag is set to YES then doxygen will sort the +# hierarchy of group names into alphabetical order. If set to NO (the default) +# the group names will appear in their defined order. + +SORT_GROUP_NAMES = NO + +# If the SORT_BY_SCOPE_NAME tag is set to YES, the class list will be +# sorted by fully-qualified names, including namespaces. If set to +# NO (the default), the class list will be sorted only by class name, +# not including the namespace part. +# Note: This option is not very useful if HIDE_SCOPE_NAMES is set to YES. +# Note: This option applies only to the class list, not to the +# alphabetical list. + +SORT_BY_SCOPE_NAME = NO + +# The GENERATE_TODOLIST tag can be used to enable (YES) or +# disable (NO) the todo list. This list is created by putting \todo +# commands in the documentation. + +GENERATE_TODOLIST = YES + +# The GENERATE_TESTLIST tag can be used to enable (YES) or +# disable (NO) the test list. This list is created by putting \test +# commands in the documentation. + +GENERATE_TESTLIST = YES + +# The GENERATE_BUGLIST tag can be used to enable (YES) or +# disable (NO) the bug list. This list is created by putting \bug +# commands in the documentation. + +GENERATE_BUGLIST = YES + +# The GENERATE_DEPRECATEDLIST tag can be used to enable (YES) or +# disable (NO) the deprecated list. This list is created by putting +# \deprecated commands in the documentation. + +GENERATE_DEPRECATEDLIST= YES + +# The ENABLED_SECTIONS tag can be used to enable conditional +# documentation sections, marked by \if sectionname ... \endif. + +ENABLED_SECTIONS = + +# The MAX_INITIALIZER_LINES tag determines the maximum number of lines +# the initial value of a variable or define consists of for it to appear in +# the documentation. If the initializer consists of more lines than specified +# here it will be hidden. Use a value of 0 to hide initializers completely. +# The appearance of the initializer of individual variables and defines in the +# documentation can be controlled using \showinitializer or \hideinitializer +# command in the documentation regardless of this setting. + +MAX_INITIALIZER_LINES = 30 + +# Set the SHOW_USED_FILES tag to NO to disable the list of files generated +# at the bottom of the documentation of classes and structs. If set to YES the +# list will mention the files that were used to generate the documentation. + +SHOW_USED_FILES = YES + +# If the sources in your project are distributed over multiple directories +# then setting the SHOW_DIRECTORIES tag to YES will show the directory hierarchy +# in the documentation. The default is NO. + +SHOW_DIRECTORIES = NO + +# Set the SHOW_FILES tag to NO to disable the generation of the Files page. +# This will remove the Files entry from the Quick Index and from the +# Folder Tree View (if specified). The default is YES. + +SHOW_FILES = YES + +# Set the SHOW_NAMESPACES tag to NO to disable the generation of the +# Namespaces page. This will remove the Namespaces entry from the Quick Index +# and from the Folder Tree View (if specified). The default is YES. + +SHOW_NAMESPACES = YES + +# The FILE_VERSION_FILTER tag can be used to specify a program or script that +# doxygen should invoke to get the current version for each file (typically from +# the version control system). Doxygen will invoke the program by executing (via +# popen()) the command , where is the value of +# the FILE_VERSION_FILTER tag, and is the name of an input file +# provided by doxygen. Whatever the program writes to standard output +# is used as the file version. See the manual for examples. + +FILE_VERSION_FILTER = + +# The LAYOUT_FILE tag can be used to specify a layout file which will be parsed by +# doxygen. The layout file controls the global structure of the generated output files +# in an output format independent way. The create the layout file that represents +# doxygen's defaults, run doxygen with the -l option. You can optionally specify a +# file name after the option, if omitted DoxygenLayout.xml will be used as the name +# of the layout file. + +LAYOUT_FILE = + +#--------------------------------------------------------------------------- +# configuration options related to warning and progress messages +#--------------------------------------------------------------------------- + +# The QUIET tag can be used to turn on/off the messages that are generated +# by doxygen. Possible values are YES and NO. If left blank NO is used. + +QUIET = NO + +# The WARNINGS tag can be used to turn on/off the warning messages that are +# generated by doxygen. Possible values are YES and NO. If left blank +# NO is used. + +WARNINGS = YES + +# If WARN_IF_UNDOCUMENTED is set to YES, then doxygen will generate warnings +# for undocumented members. If EXTRACT_ALL is set to YES then this flag will +# automatically be disabled. + +WARN_IF_UNDOCUMENTED = YES + +# If WARN_IF_DOC_ERROR is set to YES, doxygen will generate warnings for +# potential errors in the documentation, such as not documenting some +# parameters in a documented function, or documenting parameters that +# don't exist or using markup commands wrongly. + +WARN_IF_DOC_ERROR = YES + +# This WARN_NO_PARAMDOC option can be abled to get warnings for +# functions that are documented, but have no documentation for their parameters +# or return value. If set to NO (the default) doxygen will only warn about +# wrong or incomplete parameter documentation, but not about the absence of +# documentation. + +WARN_NO_PARAMDOC = YES + +# The WARN_FORMAT tag determines the format of the warning messages that +# doxygen can produce. The string should contain the $file, $line, and $text +# tags, which will be replaced by the file and line number from which the +# warning originated and the warning text. Optionally the format may contain +# $version, which will be replaced by the version of the file (if it could +# be obtained via FILE_VERSION_FILTER) + +WARN_FORMAT = "$file:$line: $text" + +# The WARN_LOGFILE tag can be used to specify a file to which warning +# and error messages should be written. If left blank the output is written +# to stderr. + +WARN_LOGFILE = + +#--------------------------------------------------------------------------- +# configuration options related to the input files +#--------------------------------------------------------------------------- + +# The INPUT tag can be used to specify the files and/or directories that contain +# documented source files. You may enter file names like "myfile.cpp" or +# directories like "/usr/src/myproject". Separate the files or directories +# with spaces. + +INPUT = D:/hq/stg/opencl/drivers/opencl/compiler/lib + +# This tag can be used to specify the character encoding of the source files +# that doxygen parses. Internally doxygen uses the UTF-8 encoding, which is +# also the default input encoding. Doxygen uses libiconv (or the iconv built +# into libc) for the transcoding. See http://www.gnu.org/software/libiconv for +# the list of possible encodings. + +INPUT_ENCODING = UTF-8 + +# If the value of the INPUT tag contains directories, you can use the +# FILE_PATTERNS tag to specify one or more wildcard pattern (like *.cpp +# and *.h) to filter out the source-files in the directories. If left +# blank the following patterns are tested: +# *.c *.cc *.cxx *.cpp *.c++ *.java *.ii *.ixx *.ipp *.i++ *.inl *.h *.hh *.hxx +# *.hpp *.h++ *.idl *.odl *.cs *.php *.php3 *.inc *.m *.mm *.py *.f90 + +FILE_PATTERNS = *.c \ + *.cc \ + *.cxx \ + *.cpp \ + *.c++ \ + *.d \ + *.java \ + *.ii \ + *.ixx \ + *.ipp \ + *.i++ \ + *.inl \ + *.h \ + *.hh \ + *.hxx \ + *.hpp \ + *.h++ \ + *.idl \ + *.odl \ + *.cs \ + *.php \ + *.php3 \ + *.inc \ + *.m \ + *.mm \ + *.dox \ + *.py \ + *.f90 \ + *.f \ + *.vhd \ + *.vhdl + +# The RECURSIVE tag can be used to turn specify whether or not subdirectories +# should be searched for input files as well. Possible values are YES and NO. +# If left blank NO is used. + +RECURSIVE = YES + +# The EXCLUDE tag can be used to specify files and/or directories that should +# excluded from the INPUT source files. This way you can easily exclude a +# subdirectory from a directory tree whose root is specified with the INPUT tag. + +EXCLUDE = D:/hq/stg/opencl/drivers/opencl/compiler/lib/backends/gpu/sc \ + D:/hq/stg/opencl/drivers/opencl/compiler/lib/loaders/elf \ + D:/hq/stg/opencl/drivers/opencl/compiler/lib/promotions \ + D:/hq/stg/opencl/drivers/opencl/compiler/lib/loaders/bif/amuabi \ + D:/hq/stg/opencl/drivers/opencl/compiler/lib/include/v0_7 \ + D:/hq/stg/opencl/drivers/opencl/compiler/lib/api/v0_7 \ + D:/hq/stg/opencl/drivers/opencl/compiler/lib/loaders/plugins + +# The EXCLUDE_SYMLINKS tag can be used select whether or not files or +# directories that are symbolic links (a Unix filesystem feature) are excluded +# from the input. + +EXCLUDE_SYMLINKS = YES + +# If the value of the INPUT tag contains directories, you can use the +# EXCLUDE_PATTERNS tag to specify one or more wildcard patterns to exclude +# certain files from those directories. Note that the wildcards are matched +# against the file with absolute path, so to exclude all test directories +# for example use the pattern */test/* + +EXCLUDE_PATTERNS = *.d + +# The EXCLUDE_SYMBOLS tag can be used to specify one or more symbol names +# (namespaces, classes, functions, etc.) that should be excluded from the +# output. The symbol name can be a fully qualified name, a word, or if the +# wildcard * is used, a substring. Examples: ANamespace, AClass, +# AClass::ANamespace, ANamespace::*Test + +EXCLUDE_SYMBOLS = + +# The EXAMPLE_PATH tag can be used to specify one or more files or +# directories that contain example code fragments that are included (see +# the \include command). + +EXAMPLE_PATH = + +# If the value of the EXAMPLE_PATH tag contains directories, you can use the +# EXAMPLE_PATTERNS tag to specify one or more wildcard pattern (like *.cpp +# and *.h) to filter out the source-files in the directories. If left +# blank all files are included. + +EXAMPLE_PATTERNS = * + +# If the EXAMPLE_RECURSIVE tag is set to YES then subdirectories will be +# searched for input files to be used with the \include or \dontinclude +# commands irrespective of the value of the RECURSIVE tag. +# Possible values are YES and NO. If left blank NO is used. + +EXAMPLE_RECURSIVE = NO + +# The IMAGE_PATH tag can be used to specify one or more files or +# directories that contain image that are included in the documentation (see +# the \image command). + +IMAGE_PATH = + +# The INPUT_FILTER tag can be used to specify a program that doxygen should +# invoke to filter for each input file. Doxygen will invoke the filter program +# by executing (via popen()) the command , where +# is the value of the INPUT_FILTER tag, and is the name of an +# input file. Doxygen will then use the output that the filter program writes +# to standard output. If FILTER_PATTERNS is specified, this tag will be +# ignored. + +INPUT_FILTER = + +# The FILTER_PATTERNS tag can be used to specify filters on a per file pattern +# basis. Doxygen will compare the file name with each pattern and apply the +# filter if there is a match. The filters are a list of the form: +# pattern=filter (like *.cpp=my_cpp_filter). See INPUT_FILTER for further +# info on how filters are used. If FILTER_PATTERNS is empty, INPUT_FILTER +# is applied to all files. + +FILTER_PATTERNS = + +# If the FILTER_SOURCE_FILES tag is set to YES, the input filter (if set using +# INPUT_FILTER) will be used to filter the input files when producing source +# files to browse (i.e. when SOURCE_BROWSER is set to YES). + +FILTER_SOURCE_FILES = NO + +#--------------------------------------------------------------------------- +# configuration options related to source browsing +#--------------------------------------------------------------------------- + +# If the SOURCE_BROWSER tag is set to YES then a list of source files will +# be generated. Documented entities will be cross-referenced with these sources. +# Note: To get rid of all source code in the generated output, make sure also +# VERBATIM_HEADERS is set to NO. + +SOURCE_BROWSER = YES + +# Setting the INLINE_SOURCES tag to YES will include the body +# of functions and classes directly in the documentation. + +INLINE_SOURCES = YES + +# Setting the STRIP_CODE_COMMENTS tag to YES (the default) will instruct +# doxygen to hide any special comment blocks from generated source code +# fragments. Normal C and C++ comments will always remain visible. + +STRIP_CODE_COMMENTS = YES + +# If the REFERENCED_BY_RELATION tag is set to YES +# then for each documented function all documented +# functions referencing it will be listed. + +REFERENCED_BY_RELATION = YES + +# If the REFERENCES_RELATION tag is set to YES +# then for each documented function all documented entities +# called/used by that function will be listed. + +REFERENCES_RELATION = YES + +# If the REFERENCES_LINK_SOURCE tag is set to YES (the default) +# and SOURCE_BROWSER tag is set to YES, then the hyperlinks from +# functions in REFERENCES_RELATION and REFERENCED_BY_RELATION lists will +# link to the source code. Otherwise they will link to the documentation. + +REFERENCES_LINK_SOURCE = YES + +# If the USE_HTAGS tag is set to YES then the references to source code +# will point to the HTML generated by the htags(1) tool instead of doxygen +# built-in source browser. The htags tool is part of GNU's global source +# tagging system (see http://www.gnu.org/software/global/global.html). You +# will need version 4.8.6 or higher. + +USE_HTAGS = NO + +# If the VERBATIM_HEADERS tag is set to YES (the default) then Doxygen +# will generate a verbatim copy of the header file for each class for +# which an include is specified. Set to NO to disable this. + +VERBATIM_HEADERS = YES + +#--------------------------------------------------------------------------- +# configuration options related to the alphabetical class index +#--------------------------------------------------------------------------- + +# If the ALPHABETICAL_INDEX tag is set to YES, an alphabetical index +# of all compounds will be generated. Enable this if the project +# contains a lot of classes, structs, unions or interfaces. + +ALPHABETICAL_INDEX = NO + +# If the alphabetical index is enabled (see ALPHABETICAL_INDEX) then +# the COLS_IN_ALPHA_INDEX tag can be used to specify the number of columns +# in which this list will be split (can be a number in the range [1..20]) + +COLS_IN_ALPHA_INDEX = 5 + +# In case all classes in a project start with a common prefix, all +# classes will be put under the same header in the alphabetical index. +# The IGNORE_PREFIX tag can be used to specify one or more prefixes that +# should be ignored while generating the index headers. + +IGNORE_PREFIX = + +#--------------------------------------------------------------------------- +# configuration options related to the HTML output +#--------------------------------------------------------------------------- + +# If the GENERATE_HTML tag is set to YES (the default) Doxygen will +# generate HTML output. + +GENERATE_HTML = YES + +# The HTML_OUTPUT tag is used to specify where the HTML docs will be put. +# If a relative path is entered the value of OUTPUT_DIRECTORY will be +# put in front of it. If left blank `html' will be used as the default path. + +HTML_OUTPUT = html + +# The HTML_FILE_EXTENSION tag can be used to specify the file extension for +# each generated HTML page (for example: .htm,.php,.asp). If it is left blank +# doxygen will generate files with .html extension. + +HTML_FILE_EXTENSION = .html + +# The HTML_HEADER tag can be used to specify a personal HTML header for +# each generated HTML page. If it is left blank doxygen will generate a +# standard header. + +HTML_HEADER = + +# The HTML_FOOTER tag can be used to specify a personal HTML footer for +# each generated HTML page. If it is left blank doxygen will generate a +# standard footer. + +HTML_FOOTER = + +# The HTML_STYLESHEET tag can be used to specify a user-defined cascading +# style sheet that is used by each HTML page. It can be used to +# fine-tune the look of the HTML output. If the tag is left blank doxygen +# will generate a default style sheet. Note that doxygen will try to copy +# the style sheet file to the HTML output directory, so don't put your own +# stylesheet in the HTML output directory as well, or it will be erased! + +HTML_STYLESHEET = + +# If the HTML_TIMESTAMP tag is set to YES then the footer of each generated HTML +# page will contain the date and time when the page was generated. Setting +# this to NO can help when comparing the output of multiple runs. + +HTML_TIMESTAMP = YES + +# If the HTML_ALIGN_MEMBERS tag is set to YES, the members of classes, +# files or namespaces will be aligned in HTML using tables. If set to +# NO a bullet list will be used. + +HTML_ALIGN_MEMBERS = YES + +# If the HTML_DYNAMIC_SECTIONS tag is set to YES then the generated HTML +# documentation will contain sections that can be hidden and shown after the +# page has loaded. For this to work a browser that supports +# JavaScript and DHTML is required (for instance Mozilla 1.0+, Firefox +# Netscape 6.0+, Internet explorer 5.0+, Konqueror, or Safari). + +HTML_DYNAMIC_SECTIONS = NO + +# If the GENERATE_DOCSET tag is set to YES, additional index files +# will be generated that can be used as input for Apple's Xcode 3 +# integrated development environment, introduced with OSX 10.5 (Leopard). +# To create a documentation set, doxygen will generate a Makefile in the +# HTML output directory. Running make will produce the docset in that +# directory and running "make install" will install the docset in +# ~/Library/Developer/Shared/Documentation/DocSets so that Xcode will find +# it at startup. +# See http://developer.apple.com/tools/creatingdocsetswithdoxygen.html for more information. + +GENERATE_DOCSET = NO + +# When GENERATE_DOCSET tag is set to YES, this tag determines the name of the +# feed. A documentation feed provides an umbrella under which multiple +# documentation sets from a single provider (such as a company or product suite) +# can be grouped. + +DOCSET_FEEDNAME = "Doxygen generated docs" + +# When GENERATE_DOCSET tag is set to YES, this tag specifies a string that +# should uniquely identify the documentation set bundle. This should be a +# reverse domain-name style string, e.g. com.mycompany.MyDocSet. Doxygen +# will append .docset to the name. + +DOCSET_BUNDLE_ID = org.doxygen.Project + +# If the GENERATE_HTMLHELP tag is set to YES, additional index files +# will be generated that can be used as input for tools like the +# Microsoft HTML help workshop to generate a compiled HTML help file (.chm) +# of the generated HTML documentation. + +GENERATE_HTMLHELP = NO + +# If the GENERATE_HTMLHELP tag is set to YES, the CHM_FILE tag can +# be used to specify the file name of the resulting .chm file. You +# can add a path in front of the file if the result should not be +# written to the html output directory. + +CHM_FILE = + +# If the GENERATE_HTMLHELP tag is set to YES, the HHC_LOCATION tag can +# be used to specify the location (absolute path including file name) of +# the HTML help compiler (hhc.exe). If non-empty doxygen will try to run +# the HTML help compiler on the generated index.hhp. + +HHC_LOCATION = + +# If the GENERATE_HTMLHELP tag is set to YES, the GENERATE_CHI flag +# controls if a separate .chi index file is generated (YES) or that +# it should be included in the master .chm file (NO). + +GENERATE_CHI = NO + +# If the GENERATE_HTMLHELP tag is set to YES, the CHM_INDEX_ENCODING +# is used to encode HtmlHelp index (hhk), content (hhc) and project file +# content. + +CHM_INDEX_ENCODING = + +# If the GENERATE_HTMLHELP tag is set to YES, the BINARY_TOC flag +# controls whether a binary table of contents is generated (YES) or a +# normal table of contents (NO) in the .chm file. + +BINARY_TOC = NO + +# The TOC_EXPAND flag can be set to YES to add extra items for group members +# to the contents of the HTML help documentation and to the tree view. + +TOC_EXPAND = NO + +# If the GENERATE_QHP tag is set to YES and both QHP_NAMESPACE and QHP_VIRTUAL_FOLDER +# are set, an additional index file will be generated that can be used as input for +# Qt's qhelpgenerator to generate a Qt Compressed Help (.qch) of the generated +# HTML documentation. + +GENERATE_QHP = NO + +# If the QHG_LOCATION tag is specified, the QCH_FILE tag can +# be used to specify the file name of the resulting .qch file. +# The path specified is relative to the HTML output folder. + +QCH_FILE = + +# The QHP_NAMESPACE tag specifies the namespace to use when generating +# Qt Help Project output. For more information please see +# http://doc.trolltech.com/qthelpproject.html#namespace + +QHP_NAMESPACE = org.doxygen.Project + +# The QHP_VIRTUAL_FOLDER tag specifies the namespace to use when generating +# Qt Help Project output. For more information please see +# http://doc.trolltech.com/qthelpproject.html#virtual-folders + +QHP_VIRTUAL_FOLDER = doc + +# If QHP_CUST_FILTER_NAME is set, it specifies the name of a custom filter to add. +# For more information please see +# http://doc.trolltech.com/qthelpproject.html#custom-filters + +QHP_CUST_FILTER_NAME = + +# The QHP_CUST_FILT_ATTRS tag specifies the list of the attributes of the custom filter to add.For more information please see +# Qt Help Project / Custom Filters. + +QHP_CUST_FILTER_ATTRS = + +# The QHP_SECT_FILTER_ATTRS tag specifies the list of the attributes this project's +# filter section matches. +# Qt Help Project / Filter Attributes. + +QHP_SECT_FILTER_ATTRS = + +# If the GENERATE_QHP tag is set to YES, the QHG_LOCATION tag can +# be used to specify the location of Qt's qhelpgenerator. +# If non-empty doxygen will try to run qhelpgenerator on the generated +# .qhp file. + +QHG_LOCATION = + +# If the GENERATE_ECLIPSEHELP tag is set to YES, additional index files +# will be generated, which together with the HTML files, form an Eclipse help +# plugin. To install this plugin and make it available under the help contents +# menu in Eclipse, the contents of the directory containing the HTML and XML +# files needs to be copied into the plugins directory of eclipse. The name of +# the directory within the plugins directory should be the same as +# the ECLIPSE_DOC_ID value. After copying Eclipse needs to be restarted before +# the help appears. + +GENERATE_ECLIPSEHELP = NO + +# A unique identifier for the eclipse help plugin. When installing the plugin +# the directory name containing the HTML and XML files should also have +# this name. + +ECLIPSE_DOC_ID = org.doxygen.Project + +# The DISABLE_INDEX tag can be used to turn on/off the condensed index at +# top of each HTML page. The value NO (the default) enables the index and +# the value YES disables it. + +DISABLE_INDEX = NO + +# This tag can be used to set the number of enum values (range [1..20]) +# that doxygen will group on one line in the generated HTML documentation. + +ENUM_VALUES_PER_LINE = 4 + +# The GENERATE_TREEVIEW tag is used to specify whether a tree-like index +# structure should be generated to display hierarchical information. +# If the tag value is set to YES, a side panel will be generated +# containing a tree-like index structure (just like the one that +# is generated for HTML Help). For this to work a browser that supports +# JavaScript, DHTML, CSS and frames is required (i.e. any modern browser). +# Windows users are probably better off using the HTML help feature. + +GENERATE_TREEVIEW = YES + +# By enabling USE_INLINE_TREES, doxygen will generate the Groups, Directories, +# and Class Hierarchy pages using a tree view instead of an ordered list. + +USE_INLINE_TREES = NO + +# If the treeview is enabled (see GENERATE_TREEVIEW) then this tag can be +# used to set the initial width (in pixels) of the frame in which the tree +# is shown. + +TREEVIEW_WIDTH = 250 + +# Use this tag to change the font size of Latex formulas included +# as images in the HTML documentation. The default is 10. Note that +# when you change the font size after a successful doxygen run you need +# to manually remove any form_*.png images from the HTML output directory +# to force them to be regenerated. + +FORMULA_FONTSIZE = 10 + +# When the SEARCHENGINE tag is enabled doxygen will generate a search box +# for the HTML output. The underlying search engine uses javascript +# and DHTML and should work on any modern browser. Note that when using +# HTML help (GENERATE_HTMLHELP), Qt help (GENERATE_QHP), or docsets +# (GENERATE_DOCSET) there is already a search function so this one should +# typically be disabled. For large projects the javascript based search engine +# can be slow, then enabling SERVER_BASED_SEARCH may provide a better solution. + +SEARCHENGINE = NO + +# When the SERVER_BASED_SEARCH tag is enabled the search engine will be +# implemented using a PHP enabled web server instead of at the web client +# using Javascript. Doxygen will generate the search PHP script and index +# file to put on the web server. The advantage of the server +# based approach is that it scales better to large projects and allows +# full text search. The disadvances is that it is more difficult to setup +# and does not have live searching capabilities. + +SERVER_BASED_SEARCH = NO + +#--------------------------------------------------------------------------- +# configuration options related to the LaTeX output +#--------------------------------------------------------------------------- + +# If the GENERATE_LATEX tag is set to YES (the default) Doxygen will +# generate Latex output. + +GENERATE_LATEX = NO + +# The LATEX_OUTPUT tag is used to specify where the LaTeX docs will be put. +# If a relative path is entered the value of OUTPUT_DIRECTORY will be +# put in front of it. If left blank `latex' will be used as the default path. + +LATEX_OUTPUT = latex + +# The LATEX_CMD_NAME tag can be used to specify the LaTeX command name to be +# invoked. If left blank `latex' will be used as the default command name. +# Note that when enabling USE_PDFLATEX this option is only used for +# generating bitmaps for formulas in the HTML output, but not in the +# Makefile that is written to the output directory. + +LATEX_CMD_NAME = latex + +# The MAKEINDEX_CMD_NAME tag can be used to specify the command name to +# generate index for LaTeX. If left blank `makeindex' will be used as the +# default command name. + +MAKEINDEX_CMD_NAME = makeindex + +# If the COMPACT_LATEX tag is set to YES Doxygen generates more compact +# LaTeX documents. This may be useful for small projects and may help to +# save some trees in general. + +COMPACT_LATEX = NO + +# The PAPER_TYPE tag can be used to set the paper type that is used +# by the printer. Possible values are: a4, a4wide, letter, legal and +# executive. If left blank a4wide will be used. + +PAPER_TYPE = a4wide + +# The EXTRA_PACKAGES tag can be to specify one or more names of LaTeX +# packages that should be included in the LaTeX output. + +EXTRA_PACKAGES = + +# The LATEX_HEADER tag can be used to specify a personal LaTeX header for +# the generated latex document. The header should contain everything until +# the first chapter. If it is left blank doxygen will generate a +# standard header. Notice: only use this tag if you know what you are doing! + +LATEX_HEADER = + +# If the PDF_HYPERLINKS tag is set to YES, the LaTeX that is generated +# is prepared for conversion to pdf (using ps2pdf). The pdf file will +# contain links (just like the HTML output) instead of page references +# This makes the output suitable for online browsing using a pdf viewer. + +PDF_HYPERLINKS = YES + +# If the USE_PDFLATEX tag is set to YES, pdflatex will be used instead of +# plain latex in the generated Makefile. Set this option to YES to get a +# higher quality PDF documentation. + +USE_PDFLATEX = YES + +# If the LATEX_BATCHMODE tag is set to YES, doxygen will add the \\batchmode. +# command to the generated LaTeX files. This will instruct LaTeX to keep +# running if errors occur, instead of asking the user for help. +# This option is also used when generating formulas in HTML. + +LATEX_BATCHMODE = NO + +# If LATEX_HIDE_INDICES is set to YES then doxygen will not +# include the index chapters (such as File Index, Compound Index, etc.) +# in the output. + +LATEX_HIDE_INDICES = NO + +# If LATEX_SOURCE_CODE is set to YES then doxygen will include +# source code with syntax highlighting in the LaTeX output. +# Note that which sources are shown also depends on other settings +# such as SOURCE_BROWSER. + +LATEX_SOURCE_CODE = NO + +#--------------------------------------------------------------------------- +# configuration options related to the RTF output +#--------------------------------------------------------------------------- + +# If the GENERATE_RTF tag is set to YES Doxygen will generate RTF output +# The RTF output is optimized for Word 97 and may not look very pretty with +# other RTF readers or editors. + +GENERATE_RTF = NO + +# The RTF_OUTPUT tag is used to specify where the RTF docs will be put. +# If a relative path is entered the value of OUTPUT_DIRECTORY will be +# put in front of it. If left blank `rtf' will be used as the default path. + +RTF_OUTPUT = rtf + +# If the COMPACT_RTF tag is set to YES Doxygen generates more compact +# RTF documents. This may be useful for small projects and may help to +# save some trees in general. + +COMPACT_RTF = NO + +# If the RTF_HYPERLINKS tag is set to YES, the RTF that is generated +# will contain hyperlink fields. The RTF file will +# contain links (just like the HTML output) instead of page references. +# This makes the output suitable for online browsing using WORD or other +# programs which support those fields. +# Note: wordpad (write) and others do not support links. + +RTF_HYPERLINKS = NO + +# Load stylesheet definitions from file. Syntax is similar to doxygen's +# config file, i.e. a series of assignments. You only have to provide +# replacements, missing definitions are set to their default value. + +RTF_STYLESHEET_FILE = + +# Set optional variables used in the generation of an rtf document. +# Syntax is similar to doxygen's config file. + +RTF_EXTENSIONS_FILE = + +#--------------------------------------------------------------------------- +# configuration options related to the man page output +#--------------------------------------------------------------------------- + +# If the GENERATE_MAN tag is set to YES (the default) Doxygen will +# generate man pages + +GENERATE_MAN = NO + +# The MAN_OUTPUT tag is used to specify where the man pages will be put. +# If a relative path is entered the value of OUTPUT_DIRECTORY will be +# put in front of it. If left blank `man' will be used as the default path. + +MAN_OUTPUT = man + +# The MAN_EXTENSION tag determines the extension that is added to +# the generated man pages (default is the subroutine's section .3) + +MAN_EXTENSION = .3 + +# If the MAN_LINKS tag is set to YES and Doxygen generates man output, +# then it will generate one additional man file for each entity +# documented in the real man page(s). These additional files +# only source the real man page, but without them the man command +# would be unable to find the correct page. The default is NO. + +MAN_LINKS = NO + +#--------------------------------------------------------------------------- +# configuration options related to the XML output +#--------------------------------------------------------------------------- + +# If the GENERATE_XML tag is set to YES Doxygen will +# generate an XML file that captures the structure of +# the code including all documentation. + +GENERATE_XML = NO + +# The XML_OUTPUT tag is used to specify where the XML pages will be put. +# If a relative path is entered the value of OUTPUT_DIRECTORY will be +# put in front of it. If left blank `xml' will be used as the default path. + +XML_OUTPUT = xml + +# The XML_SCHEMA tag can be used to specify an XML schema, +# which can be used by a validating XML parser to check the +# syntax of the XML files. + +XML_SCHEMA = + +# The XML_DTD tag can be used to specify an XML DTD, +# which can be used by a validating XML parser to check the +# syntax of the XML files. + +XML_DTD = + +# If the XML_PROGRAMLISTING tag is set to YES Doxygen will +# dump the program listings (including syntax highlighting +# and cross-referencing information) to the XML output. Note that +# enabling this will significantly increase the size of the XML output. + +XML_PROGRAMLISTING = YES + +#--------------------------------------------------------------------------- +# configuration options for the AutoGen Definitions output +#--------------------------------------------------------------------------- + +# If the GENERATE_AUTOGEN_DEF tag is set to YES Doxygen will +# generate an AutoGen Definitions (see autogen.sf.net) file +# that captures the structure of the code including all +# documentation. Note that this feature is still experimental +# and incomplete at the moment. + +GENERATE_AUTOGEN_DEF = NO + +#--------------------------------------------------------------------------- +# configuration options related to the Perl module output +#--------------------------------------------------------------------------- + +# If the GENERATE_PERLMOD tag is set to YES Doxygen will +# generate a Perl module file that captures the structure of +# the code including all documentation. Note that this +# feature is still experimental and incomplete at the +# moment. + +GENERATE_PERLMOD = NO + +# If the PERLMOD_LATEX tag is set to YES Doxygen will generate +# the necessary Makefile rules, Perl scripts and LaTeX code to be able +# to generate PDF and DVI output from the Perl module output. + +PERLMOD_LATEX = NO + +# If the PERLMOD_PRETTY tag is set to YES the Perl module output will be +# nicely formatted so it can be parsed by a human reader. This is useful +# if you want to understand what is going on. On the other hand, if this +# tag is set to NO the size of the Perl module output will be much smaller +# and Perl will parse it just the same. + +PERLMOD_PRETTY = YES + +# The names of the make variables in the generated doxyrules.make file +# are prefixed with the string contained in PERLMOD_MAKEVAR_PREFIX. +# This is useful so different doxyrules.make files included by the same +# Makefile don't overwrite each other's variables. + +PERLMOD_MAKEVAR_PREFIX = + +#--------------------------------------------------------------------------- +# Configuration options related to the preprocessor +#--------------------------------------------------------------------------- + +# If the ENABLE_PREPROCESSING tag is set to YES (the default) Doxygen will +# evaluate all C-preprocessor directives found in the sources and include +# files. + +ENABLE_PREPROCESSING = YES + +# If the MACRO_EXPANSION tag is set to YES Doxygen will expand all macro +# names in the source code. If set to NO (the default) only conditional +# compilation will be performed. Macro expansion can be done in a controlled +# way by setting EXPAND_ONLY_PREDEF to YES. + +MACRO_EXPANSION = NO + +# If the EXPAND_ONLY_PREDEF and MACRO_EXPANSION tags are both set to YES +# then the macro expansion is limited to the macros specified with the +# PREDEFINED and EXPAND_AS_DEFINED tags. + +EXPAND_ONLY_PREDEF = NO + +# If the SEARCH_INCLUDES tag is set to YES (the default) the includes files +# in the INCLUDE_PATH (see below) will be search if a #include is found. + +SEARCH_INCLUDES = YES + +# The INCLUDE_PATH tag can be used to specify one or more directories that +# contain include files that are not input files but should be processed by +# the preprocessor. + +INCLUDE_PATH = + +# You can use the INCLUDE_FILE_PATTERNS tag to specify one or more wildcard +# patterns (like *.h and *.hpp) to filter out the header-files in the +# directories. If left blank, the patterns specified with FILE_PATTERNS will +# be used. + +INCLUDE_FILE_PATTERNS = + +# The PREDEFINED tag can be used to specify one or more macro names that +# are defined before the preprocessor is started (similar to the -D option of +# gcc). The argument of the tag is a list of macros of the form: name +# or name=definition (no spaces). If the definition and the = are +# omitted =1 is assumed. To prevent a macro definition from being +# undefined via #undef or recursively expanded use the := operator +# instead of the = operator. + +PREDEFINED = + +# If the MACRO_EXPANSION and EXPAND_ONLY_PREDEF tags are set to YES then +# this tag can be used to specify a list of macro names that should be expanded. +# The macro definition that is found in the sources will be used. +# Use the PREDEFINED tag if you want to use a different macro definition. + +EXPAND_AS_DEFINED = + +# If the SKIP_FUNCTION_MACROS tag is set to YES (the default) then +# doxygen's preprocessor will remove all function-like macros that are alone +# on a line, have an all uppercase name, and do not end with a semicolon. Such +# function macros are typically used for boiler-plate code, and will confuse +# the parser if not removed. + +SKIP_FUNCTION_MACROS = YES + +#--------------------------------------------------------------------------- +# Configuration::additions related to external references +#--------------------------------------------------------------------------- + +# The TAGFILES option can be used to specify one or more tagfiles. +# Optionally an initial location of the external documentation +# can be added for each tagfile. The format of a tag file without +# this location is as follows: +# TAGFILES = file1 file2 ... +# Adding location for the tag files is done as follows: +# TAGFILES = file1=loc1 "file2 = loc2" ... +# where "loc1" and "loc2" can be relative or absolute paths or +# URLs. If a location is present for each tag, the installdox tool +# does not have to be run to correct the links. +# Note that each tag file must have a unique name +# (where the name does NOT include the path) +# If a tag file is not located in the directory in which doxygen +# is run, you must also specify the path to the tagfile here. + +TAGFILES = + +# When a file name is specified after GENERATE_TAGFILE, doxygen will create +# a tag file that is based on the input files it reads. + +GENERATE_TAGFILE = + +# If the ALLEXTERNALS tag is set to YES all external classes will be listed +# in the class index. If set to NO only the inherited external classes +# will be listed. + +ALLEXTERNALS = NO + +# If the EXTERNAL_GROUPS tag is set to YES all external groups will be listed +# in the modules index. If set to NO, only the current project's groups will +# be listed. + +EXTERNAL_GROUPS = YES + +# The PERL_PATH should be the absolute path and name of the perl script +# interpreter (i.e. the result of `which perl'). + +PERL_PATH = /usr/bin/perl + +#--------------------------------------------------------------------------- +# Configuration options related to the dot tool +#--------------------------------------------------------------------------- + +# If the CLASS_DIAGRAMS tag is set to YES (the default) Doxygen will +# generate a inheritance diagram (in HTML, RTF and LaTeX) for classes with base +# or super classes. Setting the tag to NO turns the diagrams off. Note that +# this option is superseded by the HAVE_DOT option below. This is only a +# fallback. It is recommended to install and use dot, since it yields more +# powerful graphs. + +CLASS_DIAGRAMS = YES + +# You can define message sequence charts within doxygen comments using the \msc +# command. Doxygen will then run the mscgen tool (see +# http://www.mcternan.me.uk/mscgen/) to produce the chart and insert it in the +# documentation. The MSCGEN_PATH tag allows you to specify the directory where +# the mscgen tool resides. If left empty the tool is assumed to be found in the +# default search path. + +MSCGEN_PATH = + +# If set to YES, the inheritance and collaboration graphs will hide +# inheritance and usage relations if the target is undocumented +# or is not a class. + +HIDE_UNDOC_RELATIONS = YES + +# If you set the HAVE_DOT tag to YES then doxygen will assume the dot tool is +# available from the path. This tool is part of Graphviz, a graph visualization +# toolkit from AT&T and Lucent Bell Labs. The other options in this section +# have no effect if this option is set to NO (the default) + +HAVE_DOT = NO + +# By default doxygen will write a font called FreeSans.ttf to the output +# directory and reference it in all dot files that doxygen generates. This +# font does not include all possible unicode characters however, so when you need +# these (or just want a differently looking font) you can specify the font name +# using DOT_FONTNAME. You need need to make sure dot is able to find the font, +# which can be done by putting it in a standard location or by setting the +# DOTFONTPATH environment variable or by setting DOT_FONTPATH to the directory +# containing the font. + +DOT_FONTNAME = FreeSans + +# The DOT_FONTSIZE tag can be used to set the size of the font of dot graphs. +# The default size is 10pt. + +DOT_FONTSIZE = 10 + +# By default doxygen will tell dot to use the output directory to look for the +# FreeSans.ttf font (which doxygen will put there itself). If you specify a +# different font using DOT_FONTNAME you can set the path where dot +# can find it using this tag. + +DOT_FONTPATH = + +# If the CLASS_GRAPH and HAVE_DOT tags are set to YES then doxygen +# will generate a graph for each documented class showing the direct and +# indirect inheritance relations. Setting this tag to YES will force the +# the CLASS_DIAGRAMS tag to NO. + +CLASS_GRAPH = YES + +# If the COLLABORATION_GRAPH and HAVE_DOT tags are set to YES then doxygen +# will generate a graph for each documented class showing the direct and +# indirect implementation dependencies (inheritance, containment, and +# class references variables) of the class with other documented classes. + +COLLABORATION_GRAPH = YES + +# If the GROUP_GRAPHS and HAVE_DOT tags are set to YES then doxygen +# will generate a graph for groups, showing the direct groups dependencies + +GROUP_GRAPHS = YES + +# If the UML_LOOK tag is set to YES doxygen will generate inheritance and +# collaboration diagrams in a style similar to the OMG's Unified Modeling +# Language. + +UML_LOOK = NO + +# If set to YES, the inheritance and collaboration graphs will show the +# relations between templates and their instances. + +TEMPLATE_RELATIONS = NO + +# If the ENABLE_PREPROCESSING, SEARCH_INCLUDES, INCLUDE_GRAPH, and HAVE_DOT +# tags are set to YES then doxygen will generate a graph for each documented +# file showing the direct and indirect include dependencies of the file with +# other documented files. + +INCLUDE_GRAPH = YES + +# If the ENABLE_PREPROCESSING, SEARCH_INCLUDES, INCLUDED_BY_GRAPH, and +# HAVE_DOT tags are set to YES then doxygen will generate a graph for each +# documented header file showing the documented files that directly or +# indirectly include this file. + +INCLUDED_BY_GRAPH = YES + +# If the CALL_GRAPH and HAVE_DOT options are set to YES then +# doxygen will generate a call dependency graph for every global function +# or class method. Note that enabling this option will significantly increase +# the time of a run. So in most cases it will be better to enable call graphs +# for selected functions only using the \callgraph command. + +CALL_GRAPH = NO + +# If the CALLER_GRAPH and HAVE_DOT tags are set to YES then +# doxygen will generate a caller dependency graph for every global function +# or class method. Note that enabling this option will significantly increase +# the time of a run. So in most cases it will be better to enable caller +# graphs for selected functions only using the \callergraph command. + +CALLER_GRAPH = NO + +# If the GRAPHICAL_HIERARCHY and HAVE_DOT tags are set to YES then doxygen +# will graphical hierarchy of all classes instead of a textual one. + +GRAPHICAL_HIERARCHY = YES + +# If the DIRECTORY_GRAPH, SHOW_DIRECTORIES and HAVE_DOT tags are set to YES +# then doxygen will show the dependencies a directory has on other directories +# in a graphical way. The dependency relations are determined by the #include +# relations between the files in the directories. + +DIRECTORY_GRAPH = YES + +# The DOT_IMAGE_FORMAT tag can be used to set the image format of the images +# generated by dot. Possible values are png, jpg, or gif +# If left blank png will be used. + +DOT_IMAGE_FORMAT = png + +# The tag DOT_PATH can be used to specify the path where the dot tool can be +# found. If left blank, it is assumed the dot tool can be found in the path. + +DOT_PATH = + +# The DOTFILE_DIRS tag can be used to specify one or more directories that +# contain dot files that are included in the documentation (see the +# \dotfile command). + +DOTFILE_DIRS = + +# The DOT_GRAPH_MAX_NODES tag can be used to set the maximum number of +# nodes that will be shown in the graph. If the number of nodes in a graph +# becomes larger than this value, doxygen will truncate the graph, which is +# visualized by representing a node as a red box. Note that doxygen if the +# number of direct children of the root node in a graph is already larger than +# DOT_GRAPH_MAX_NODES then the graph will not be shown at all. Also note +# that the size of a graph can be further restricted by MAX_DOT_GRAPH_DEPTH. + +DOT_GRAPH_MAX_NODES = 50 + +# The MAX_DOT_GRAPH_DEPTH tag can be used to set the maximum depth of the +# graphs generated by dot. A depth value of 3 means that only nodes reachable +# from the root by following a path via at most 3 edges will be shown. Nodes +# that lay further from the root node will be omitted. Note that setting this +# option to 1 or 2 may greatly reduce the computation time needed for large +# code bases. Also note that the size of a graph can be further restricted by +# DOT_GRAPH_MAX_NODES. Using a depth of 0 means no depth restriction. + +MAX_DOT_GRAPH_DEPTH = 0 + +# Set the DOT_TRANSPARENT tag to YES to generate images with a transparent +# background. This is disabled by default, because dot on Windows does not +# seem to support this out of the box. Warning: Depending on the platform used, +# enabling this option may lead to badly anti-aliased labels on the edges of +# a graph (i.e. they become hard to read). + +DOT_TRANSPARENT = NO + +# Set the DOT_MULTI_TARGETS tag to YES allow dot to generate multiple output +# files in one run (i.e. multiple -o and -T options on the command line). This +# makes dot run faster, but since only newer versions of dot (>1.8.10) +# support this, this feature is disabled by default. + +DOT_MULTI_TARGETS = NO + +# If the GENERATE_LEGEND tag is set to YES (the default) Doxygen will +# generate a legend page explaining the meaning of the various boxes and +# arrows in the dot generated graphs. + +GENERATE_LEGEND = YES + +# If the DOT_CLEANUP tag is set to YES (the default) Doxygen will +# remove the intermediate dot files that are used to generate +# the various graphs. + +DOT_CLEANUP = YES diff --git a/rocclr/compiler/lib/amdoclcl.def.in b/rocclr/compiler/lib/amdoclcl.def.in new file mode 100644 index 0000000000..0d8a7009e2 --- /dev/null +++ b/rocclr/compiler/lib/amdoclcl.def.in @@ -0,0 +1,99 @@ +EXPORTS +aclLoader_OnLoad +aclLoader_OnUnload +aclCompilerInit +aclCompilerFini +aclCompilerVersion +aclVersionSize +aclGetErrorString +aclGetArchInfo +aclGetDeviceInfo +aclGetTargetInfo +aclGetArchitecture +aclGetFamily +aclGetChip +aclBinaryInit +aclBinaryFini +aclReadFromFile +aclReadFromMem +aclWriteToFile +aclWriteToMem +aclCreateFromBinary +aclBinaryVersion +aclInsertSection +aclRemoveSection +aclExtractSection +aclInsertSymbol +aclRemoveSymbol +aclExtractSymbol +aclDbgAddArgument +aclDbgRemoveArgument +aclQueryInfo +aclCompile +aclLink +aclGetCompilerLog +aclRetrieveType +aclSetType +aclConvertType +aclDisassemble +aclGetDeviceBinary +oclCompilerInit +oclCompilerFini +oclCompilerVersion +oclVersionSize +oclCompileSource +oclCompileSourceToIR +oclCompileIRToIL +oclCompileILToISA +oclCompileBinary +oclGetCompilerLog +oclGetArchInfo +oclGetFamilyInfo +oclGetDeviceInfo +oclGetAsicInfo +oclGetTargetInfo +oclGetArchitecture +oclGetFamily +oclGetChip +oclGetAsic +elfInsertSection +elfExtractSection +elfInsertSymbol +elfExtractSymbol +oclirSetType +oclirRetrieveType +oclirLink +oclhsaCompileSource +oclhsaCompileBinary +oclhsaSetType +oclhsaRetrieveType +oclhsaConvertType +oclilCompileSource +oclilCompileBinary +oclilSetType +oclilRetrieveType +oclilConvertType +oclisaDisassemble +oclisaGetBinaryBlob +constructOclElf +destructOclElf +readOclElfFromFile +readOclElfFromMem +writeOclElfToFile +writeOclElfToMem +createElfFromElf +getBIFVersion +rtGetInfo +aclDumpBinary +aclJITObjectImageCreate +aclJITObjectImageCopy +aclJITObjectImageDestroy +aclJITObjectImageFinalize +aclJITObjectImageSize +aclJITObjectImageData +aclJITObjectImageDisassembleKernel +aclJITObjectImageIterateSymbols +aclJITObjectImageGetGlobalsSize +#if defined(WITH_TARGET_HSAIL) +aclHsaLoader +#endif diff --git a/rocclr/compiler/lib/amdoclcl.map.in b/rocclr/compiler/lib/amdoclcl.map.in new file mode 100644 index 0000000000..f2ee10dedc --- /dev/null +++ b/rocclr/compiler/lib/amdoclcl.map.in @@ -0,0 +1,104 @@ +ACL_API_0.8 { +global: + oclCompilerInit; + oclCompilerFini; + oclCompilerVersion; + oclVersionSize; + oclCompileSource; + oclCompileSourceToIR; + oclCompileIRToIL; + oclCompileILToISA; + oclCompileBinary; + oclGetCompilerLog; + oclGetArchInfo; + oclGetDeviceInfo; + oclGetTargetInfo; + oclGetArchitecture; + oclGetFamily; + oclGetChip; + readOclElfFromFile; + readOclElfFromMem; + writeOclElfToFile; + writeOclElfToMem; + createElfFromElf; + getBIFVersion; + constructOclElf; + destructOclElf; + elfInsertSection; + elfExtractSection; + elfInsertSymbol; + elfExtractSymbol; + oclirSetType; + oclirRetrieveType; + oclirLink; + oclhsaCompileSource; + oclhsaCompileBinary; + oclhsaSetType; + oclhsaRetrieveType; + oclhsaConvertType; + oclilCompileSource; + oclilCompileBinary; + oclilSetType; + oclilRetrieveType; + oclilConvertType; + oclisaDisassemble; + oclisaGetBinaryBlob; + rtGetInfo; + aclCompilerInit; + aclCompilerFini; + aclCompilerVersion; + aclVersionSize; + aclGetErrorString; + aclGetArchInfo; + aclGetFamilyInfo; + aclGetDeviceInfo; + aclGetAsicInfo; + aclGetTargetInfo; + aclGetArchitecture; + aclGetFamily; + aclGetChip; + aclGetAsic; + aclBinaryInit; + aclBinaryFini; + aclReadFromFile; + aclReadFromMem; + aclWriteToFile; + aclWriteToMem; + aclCreateFromBinary; + aclBinaryVersion; + aclInsertSection; + aclRemoveSection; + aclExtractSection; + aclInsertSymbol; + aclRemoveSymbol; + aclExtractSymbol; + aclQueryInfo; + aclDbgAddArgument; + aclDbgRemoveArgument; + aclCompile; + aclLink; + aclGetCompilerLog; + aclRetrieveType; + aclSetType; + aclConvertType; + aclDisassemble; + aclGetDeviceBinary; + aclLoader_OnLoad; + aclLoader_OnUnload; + aclJITObjectImageCreate; + aclJITObjectImageCopy; + aclJITObjectImageDestroy; + aclJITObjectImageFinalize; + aclJITObjectImageSize; + aclJITObjectImageData; + aclJITObjectImageDisassembleKernel; + aclJITObjectImageIterateSymbols; + aclJITObjectImageGetGlobalsSize; +#if defined(WITH_TARGET_HSAIL) + aclHsaLoader; +#endif +#if defined(OPENCL_MAINLINE) +local: + *; +#endif +}; diff --git a/rocclr/compiler/lib/amdoclcl.rc b/rocclr/compiler/lib/amdoclcl.rc new file mode 100644 index 0000000000..c0b14d40ed --- /dev/null +++ b/rocclr/compiler/lib/amdoclcl.rc @@ -0,0 +1,75 @@ +#define STR(__macro__) #__macro__ +#define XSTR(__macro__) STR(__macro__) + +#if defined(_DEBUG) +#define DEBUG_ONLY(x) x +#else +#define DEBUG_ONLY(x) +#endif + +#define VERSION_PREFIX_MAJOR 1 +#define VERSION_PREFIX_MINOR 1 + + +#define APSTUDIO_READONLY_SYMBOLS +///////////////////////////////////////////////////////////////////////////// +// +// Generated from the TEXTINCLUDE 2 resource. +// +#include "winresrc.h" +#include "utils/versions.hpp" + +///////////////////////////////////////////////////////////////////////////// +#undef APSTUDIO_READONLY_SYMBOLS + +///////////////////////////////////////////////////////////////////////////// +// English (U.S.) resources + +#if !defined(AFX_RESOURCE_DLL) || defined(AFX_TARG_ENU) +#ifdef _WIN32 +LANGUAGE LANG_ENGLISH, SUBLANG_ENGLISH_US +#pragma code_page(1252) +#endif //_WIN32 + + +///////////////////////////////////////////////////////////////////////////// +// +// Version +// + +VS_VERSION_INFO VERSIONINFO + FILEVERSION AMD_COMPILER_MAJOR_VERSION,AMD_COMPILER_MINOR_VERSION,AMD_COMPILER_BUILD_NUMBER,AMD_COMPILER_REVISION_NUMBER + PRODUCTVERSION AMD_COMPILER_MAJOR_VERSION,AMD_COMPILER_MINOR_VERSION,AMD_COMPILER_BUILD_NUMBER,AMD_COMPILER_REVISION_NUMBER + FILEFLAGSMASK 0x3fL +#ifdef _DEBUG + FILEFLAGS 0x1L +#else + FILEFLAGS 0x0L +#endif + FILEOS 0x40004L + FILETYPE 0x2L + FILESUBTYPE 0x0L +BEGIN + BLOCK "StringFileInfo" + BEGIN + BLOCK "040904b0" + BEGIN + VALUE "Comments", " \0" + VALUE "CompanyName", "Advanced Micro Devices Inc.\0" + VALUE "FileDescription", AMD_COMPILER_NAME " OpenCL " XSTR(VERSION_PREFIX_MAJOR) "." XSTR(VERSION_PREFIX_MINOR) " Compiler\0" + VALUE "FileVersion", XSTR(AMD_COMPILER_MAJOR_VERSION) ", " XSTR(AMD_COMPILER_MINOR_VERSION) ", " XSTR(AMD_COMPILER_BUILD_NUMBER) ", " XSTR(AMD_COMPILER_REVISION_NUMBER) ", " XSTR(AMD_SC_COMPILER_REVISION_NUMBER) + VALUE "InternalName", "AOC" + VALUE "LegalCopyright", "Copyright (C) 2011 Advanced Micro Devices Inc.\0" + VALUE "OriginalFilename", "amdoclcl.dll" + VALUE "ProductName", "AMD Compiler Library " XSTR(VERSION_PREFIX_MAJOR) "." XSTR(VERSION_PREFIX_MINOR) " " AMD_COMPILER_INFO "\0" + VALUE "ProductVersion", XSTR(AMD_COMPILER_MAJOR_VERSION) ", " XSTR(AMD_COMPILER_MINOR_VERSION) ", " XSTR(AMD_COMPILER_BUILD_NUMBER) ", " XSTR(AMD_COMPILER_REVISION_NUMBER) ", " XSTR(AMD_SC_COMPILER_REVISION_NUMBER) + END + END + BLOCK "VarFileInfo" + BEGIN + VALUE "Translation", 0x409, 1200 + END +END + +#endif // English (U.S.) resources +///////////////////////////////////////////////////////////////////////////// diff --git a/rocclr/compiler/lib/backends/common/backend.hpp b/rocclr/compiler/lib/backends/common/backend.hpp new file mode 100644 index 0000000000..e7e9cdd3e7 --- /dev/null +++ b/rocclr/compiler/lib/backends/common/backend.hpp @@ -0,0 +1,33 @@ +// +// Copyright (c) 2008 Advanced Micro Devices, Inc. All rights reserved. +// +#ifndef _BE_BACKEND_HPP_ +#define _BE_BACKEND_HPP_ +#include "compiler_stage.hpp" + +namespace amdcl +{ + /*! \addtogroup Compiler Library + * + * \copydoc amdcl::Backend + * + * @{ + */ + class Backend : public CompilerStage { + Backend(Backend&); // DO NOT IMPLEMENT. + Backend(); // DO NOT IMPLEMENT. + public: + Backend(aclCompiler *cl, aclBinary *elf, aclLogFunction log) + : CompilerStage(cl, elf, log) {} + + virtual ~Backend() {} + + /*! Function that takes in a string that is a source file + * and generates the backend binary that is then + * inserted into the elf file at the correct location. + */ + virtual int jit(const std::string &source) = 0; + }; // class Backend + /*@}*/ +}; // amdcl namespace +#endif // _BE_BACKEND_HPP diff --git a/rocclr/compiler/lib/backends/common/codegen.cpp b/rocclr/compiler/lib/backends/common/codegen.cpp new file mode 100644 index 0000000000..57a639c65a --- /dev/null +++ b/rocclr/compiler/lib/backends/common/codegen.cpp @@ -0,0 +1,367 @@ +// +// Copyright (c) 2008 Advanced Micro Devices, Inc. All rights reserved. +// +#include "top.hpp" +#include "codegen.hpp" +#include "utils/libUtils.h" +#include "os/os.hpp" +#include "jit/src/jit.hpp" +#include "utils/target_mappings.h" +#include "llvm/Support/FormattedStream.h" +#include "llvm/Support/Host.h" +#include "llvm/Support/raw_ostream.h" +#include "llvm/Support/TargetSelect.h" +#include "llvm/DataLayout.h" +#include "llvm/Target/TargetMachine.h" +#include "llvm/Target/TargetOptions.h" +#include "llvm/Support/TargetRegistry.h" +#include "llvm/Support/CommandLine.h" +#include +#include +#include +#include + +using namespace amdcl; +using namespace llvm; + +#ifdef WITH_TARGET_HSAIL +// Variable FileType are checked by HSAILTargetMachine, but only +// created in llc.exe. Create it here for online compilation path. +llvm::cl::opt +FileType("filetype", cl::init(TargetMachine::CGFT_ObjectFile), + cl::values( + clEnumValN(TargetMachine::CGFT_AssemblyFile, "asm", ""), + clEnumValN(TargetMachine::CGFT_ObjectFile, "obj", ""), + clEnumValN(TargetMachine::CGFT_Null, "null", ""), + clEnumValEnd)); +#endif + +static std::string aclGetCodegenName(const aclTargetInfo &tgtInfo) +{ + assert(tgtInfo.arch_id <= aclLast && "Unknown device id!"); + const FamilyMapping *family = familySet + tgtInfo.arch_id; + if (!family) return ""; + + assert((tgtInfo.chip_id) < family->children_size && "Unknown family id!"); + const TargetMapping *target = &family->target[tgtInfo.chip_id]; + return (target) ? target->codegen_name : ""; +} + + + +/*! Function that modifies the code gen level based on the + * function size threshhold. + */ +static CodeGenOpt::Level +AdjustCGOptLevel(Module& M, CodeGenOpt::Level OrigOLvl) +{ + const unsigned int FuncSizeThreshold = 10000; + if (OrigOLvl == CodeGenOpt::None) + return OrigOLvl; + for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) { + Function *F = (Function *)I; + if (F->size() > FuncSizeThreshold) { + return CodeGenOpt::None; + } + } + return OrigOLvl; +} + +int +llvmCodeGen( + Module* Composite, + amd::option::Options *OptionsObj, + std::string& output, + aclBinary* binary) +{ + const FamilyMapping &familyMap = familySet[binary->target.arch_id]; + const bool optimize = (OptionsObj ? (OptionsObj->oVariables->OptLevel > 0) : true); + const TargetMapping* targetMap = familyMap.target; + unsigned famID = binary->target.chip_id; + if (!targetMap || !targetMap[famID].supported) { + LogError("Device is not supported by code generator!"); + return 1; + } + +#if 1 || LLVM_TRUNK_INTEGRATION_CL >= 1463 +#else + // a dirty way to guarantee "push bp" inserted by CodeGen in prologue + llvm::NoFramePointerElim = !optimize; +#endif + // Load the module to be compiled... + Module &mod = *Composite; + + // FIXME: The triple given in this map is wrong and isn't really + // useful. Only need the architecture. + const std::string TargetTriple = std::string(familyMap.triple); + Triple TheTriple(TargetTriple); + if (TheTriple.getTriple().empty()) { + TheTriple.setTriple(sys::getDefaultTargetTriple()); + } + + Triple::ArchType arch = TheTriple.getArch(); + + bool isGPU = (arch == Triple::amdil || arch == Triple::amdil64 || + arch == Triple::hsail || arch == Triple::hsail_64); + + if (isGPU) { + TheTriple.setOS(Triple::UnknownOS); + } else { // CPUs + // FIXME: This should come from somewhere else. +#ifdef __linux__ + TheTriple.setOS(Triple::Linux); +#else + TheTriple.setOS(Triple::MinGW32); +#endif + } + + TheTriple.setEnvironment(Triple::AMDOpenCL); + // FIXME: need to make AMDOpenCL be the same as ELF + if (OptionsObj->oVariables->UseJIT) + TheTriple.setEnvironment(Triple::ELF); + mod.setTargetTriple(TheTriple.getTriple()); + + // Allocate target machine. First, check whether the user has explicitly + // specified an architecture to compile for. If so we have to look it up by + // name, because it might be a backend that has no mapping to a target triple. + const Target *TheTarget = 0; + assert(binary->target.arch_id != aclError && "Cannot have the error device!"); + + std::string MArch = familyMap.architecture; + +#ifdef WITH_TARGET_HSAIL + if (MArch == "hsail" && OptionsObj->oVariables->GPU64BitIsa) { + MArch = std::string("hsail-64"); + } +#endif + + for (TargetRegistry::iterator it = TargetRegistry::begin(), + ie = TargetRegistry::end(); it != ie; ++it) { + if (MArch == it->getName()) { + TheTarget = &*it; + break; + } + } + + if (!TheTarget) { + errs() << ": ERROR: invalid target '" << MArch << "'.\n"; + return 1; + } + + CodeGenOpt::Level OLvl = CodeGenOpt::None; + switch (OptionsObj->oVariables->OptLevel) { + case 0: // -O0 + OLvl = CodeGenOpt::None; + break; + case 1: // -O1 + OLvl = CodeGenOpt::Less; + break; + default: + assert(!"Error with optimization level"); + case 2: // -O2 + case 5: // -O5(-Os) + OLvl = CodeGenOpt::Default; + break; + case 3: // -O3 + case 4: // -O4 + OLvl = CodeGenOpt::Aggressive; + break; + }; + + // If there is a very big function, lower the optimization level. + OLvl = AdjustCGOptLevel(mod, OLvl); + + // Adjust the triple to match (if known), otherwise stick with the + // module/host triple. + Triple::ArchType Type = Triple::getArchTypeForLLVMName(MArch); + if (Type != Triple::UnknownArch) + TheTriple.setArch(Type); + + // Package up features to be passed to target/subtarget + std::string FeatureStr; + if ((Type == Triple::amdil || Type == Triple::amdil64) && + targetMap[famID].chip_options) { + uint64_t y = targetMap[famID].chip_options; + for (uint64_t x = 0; y != 0; y >>= 1, ++x) { + if (!(y & 0x1) && (x >= 11 && x < 16)) { + continue; + } + + if ((1 << x) == F_NO_ALIAS) { + FeatureStr += (!OptionsObj->oVariables->AssumeAlias ? '+' : '-'); + } else if ((1 << x) == F_STACK_UAV) { + FeatureStr += (OptionsObj->oVariables->UseStackUAV ? '+' : '-'); + } else if ((1 << x) == F_MACRO_CALL) { + FeatureStr += (OptionsObj->oVariables->UseMacroForCall ? '+' : '-'); + } else if ((1 << x) == F_64BIT_PTR) { + FeatureStr += (binary->target.arch_id == aclAMDIL64) ? '+' : '-'; + } else { + FeatureStr += ((y & 0x1) ? '+' : '-'); + } + + FeatureStr += GPUCodeGenFlagTable[x]; + if (y != 0x1) { + FeatureStr += ','; + } + } + } + + if (Type == Triple::amdil64) { + if (OptionsObj->oVariables->SmallGlobalObjects) + FeatureStr += ",+small-global-objects"; + } + +#if 1 || LLVM_TRUNK_INTEGRATION_CL >= 1463 + llvm::TargetOptions targetOptions; + targetOptions.NoFramePointerElim = false; + targetOptions.StackAlignmentOverride = + OptionsObj->oVariables->CPUStackAlignment; + // jgolds + //targetOptions.EnableEBB = (optimize && OptionsObj->oVariables->CGEBB); + //targetOptions.EnableBFO = OptionsObj->oVariables->CGBFO; + //targetOptions.NoExcessFPPrecision = !OptionsObj->oVariables->EnableFMA; + + // Don't allow unsafe optimizations for CPU because the library + // contains code that is not safe. See bug 9567. + if (isGPU) + targetOptions.UnsafeFPMath = OptionsObj->oVariables->UnsafeMathOpt; + targetOptions.LessPreciseFPMADOption = OptionsObj->oVariables->MadEnable || + OptionsObj->oVariables->EnableMAD; + targetOptions.NoInfsFPMath = OptionsObj->oVariables->FiniteMathOnly; + // Need to add a support for OptionsObj->oVariables->NoSignedZeros, + targetOptions.NoNaNsFPMath = OptionsObj->oVariables->FastRelaxedMath; + + std::auto_ptr + target(TheTarget->createTargetMachine(TheTriple.getTriple(), + aclGetCodegenName(binary->target), FeatureStr, targetOptions, + WINDOWS_SWITCH(Reloc::DynamicNoPIC, Reloc::PIC_), + CodeModel::Default, OLvl)); +#else + std::auto_ptr + target(TheTarget->createTargetMachine(TheTriple.getTriple(), + aclGetCodegenName(binary->target), FeatureStr, + WINDOWS_SWITCH(Reloc::DynamicNoPIC, Reloc::PIC_), + CodeModel::Default)); + assert(target.get() && "Could not allocate target machine!"); +#endif + + // MCJIT(Jan) + if(!isGPU && OptionsObj->oVariables->UseJIT) { + TargetMachine* jittarget(TheTarget->createTargetMachine(TheTriple.getTriple(), + aclGetCodegenName(binary->target), FeatureStr, targetOptions, + WINDOWS_SWITCH(Reloc::DynamicNoPIC, Reloc::PIC_), + CodeModel::Default, OLvl)); + + std::string ErrStr = jitCodeGen(Composite, jittarget, OLvl, output); + + if (!ErrStr.empty()) { + LogError("MCJIT failed to generate code"); + LogError(ErrStr.c_str()); + return 1; + } + return 0; + } + + + TargetMachine &Target = *target; + + // Figure out where we are going to send the output... + raw_string_ostream *RSOut = new raw_string_ostream(output); + formatted_raw_ostream *Out = new formatted_raw_ostream(*RSOut, formatted_raw_ostream::DELETE_STREAM); + if (Out == 0) { + LogError("llvmCodeGen couldn't create an output stream"); + return 1; + } + + // Build up all of the passes that we want to do to the module or function or + // Basic Block. + PassManager Passes; + + // Add the target data from the target machine, if it exists, or the module. + if (const DataLayout *TD = Target.getDataLayout()) + Passes.add(new DataLayout(*TD)); + else + Passes.add(new DataLayout(&mod)); + + // Override default to generate verbose assembly, if the device is not the GPU. + // The GPU sets this in AMDILTargetMachine.cpp. + if (familyMap.target == (const TargetMapping*)&X86TargetMapping || +#if WITH_VERSION_0_9 + familyMap.target == (const TargetMapping*)&A32TargetMapping || + familyMap.target == (const TargetMapping*)&A32TargetMapping || +#elif WITH_VERSION_0_8 +#else +#error "The current version implementation was not implemented here." +#endif + familyMap.target == (const TargetMapping*)&X64TargetMapping + ) { + Target.setAsmVerbosityDefault(true); + } + +#ifdef WITH_TARGET_HSAIL + if (isHSAILTarget(binary->target)) { + if (Target.addPassesToEmitFile(Passes, *Out, TargetMachine::CGFT_ObjectFile, true)) { + delete Out; + return 1; + } + } else +#endif + { +#ifndef NDEBUG +#if 1 || LLVM_TRUNK_INTEGRATION_CL >= 1144 + if (Target.addPassesToEmitFile(Passes, *Out, TargetMachine::CGFT_AssemblyFile, false)) +#else + if (Target.addPassesToEmitFile(Passes, *Out, TargetMachine::CGFT_AssemblyFile, OLvl, false)) +#endif +#else +#if 1 || LLVM_TRUNK_INTEGRATION_CL >= 1144 + if (Target.addPassesToEmitFile(Passes, *Out, TargetMachine::CGFT_AssemblyFile, true)) +#else + if (Target.addPassesToEmitFile(Passes, *Out, TargetMachine::CGFT_AssemblyFile, OLvl, true)) +#endif +#endif + { + delete Out; + return 1; + } + } + + Passes.run(mod); + + delete Out; + return 0; +} + + int +CLCodeGen::codegen(llvm::Module *input) +{ + uint64_t time_cg = 0ULL; + if (Options()->oVariables->EnableBuildTiming) { + time_cg = amd::Os::timeNanos(); + } + llvmbinary_ = input; + amdcl::CompilerStage *cs = reinterpret_cast(this); + if (!isHSAILTarget(cs->Elf()->target)) { + setWholeProgram(true); + } + + int ret = llvmCodeGen(LLVMBinary(), Options(), Source(), Elf()); + + if (Options()->oVariables->EnableBuildTiming) { + time_cg = amd::Os::timeNanos() - time_cg; + std::stringstream tmp_ss; + tmp_ss << " LLVM CodeGen time: " + << time_cg/1000ULL + << "us\n"; + appendLogToCL(CL(), tmp_ss.str()); + } + if (!Source().empty() && Options()->isDumpFlagSet(amd::option::DUMP_CGIL)) { + std::string ilFileName = Options()->getDumpFileName(".il"); + std::fstream f; + f.open(ilFileName.c_str(), (std::fstream::out | std::fstream::binary)); + f.write(Source().data(), Source().length()); + f.close(); + } + + return ret; +} diff --git a/rocclr/compiler/lib/backends/common/codegen.hpp b/rocclr/compiler/lib/backends/common/codegen.hpp new file mode 100644 index 0000000000..e8581e19ce --- /dev/null +++ b/rocclr/compiler/lib/backends/common/codegen.hpp @@ -0,0 +1,87 @@ +// +// Copyright (c) 2008 Advanced Micro Devices, Inc. All rights reserved. +// +#ifndef _BE_CODEGEN_HPP_ +#define _BE_CODEGEN_HPP_ +#include "compiler_stage.hpp" + +namespace amdcl +{ + /*! \addtogroup Compiler Library + * + * \copydoc amdcl::CodeGen + * + * @{ + */ + class CodeGen : public LLVMCompilerStage { + CodeGen(CodeGen&); // DO NOT IMPLEMENT. + CodeGen(); // DO NOT IMPLEMENT. + public: + CodeGen(aclCompiler *cl, aclBinary *elf, aclLogFunction log) + : LLVMCompilerStage(cl, elf, log) {} + + virtual ~CodeGen() {} + + /*! Function that takes in an LLVM module as input + * and generates code for it based on the target + * device. + * Returns 0 on success and non-zero on failure. + */ + virtual int codegen(llvm::Module *input) = 0; + + }; // class CodeGen + /*@}*/ + + /*! \addtogroup Compiler Library + * + * \copydoc amdcl::CLCodeGen + * + * @{ + */ + class CLCodeGen : public CodeGen { + CLCodeGen(CLCodeGen&); // DO NOT IMPLEMENT. + CLCodeGen(); // DO NOT IMPLEMENT. + public: + CLCodeGen(aclCompiler *cl, aclBinary *elf, aclLogFunction log) + : CodeGen(cl, elf, log) {} + + virtual ~CLCodeGen() {} + + /*! Function that takes in an LLVM module as input + * and generates code for it based on the target + * device. + * Returns 0 on success and non-zero on failure. + */ + virtual int codegen(llvm::Module *input); + + }; // class CLCodeGen + /*@}*/ + +#if 0 + /*! \addtogroup Compiler Library + * + * \copydoc amdcl::AMDILCodeGen + * + * @{ + */ + class AMDILCodeGen : public CodeGen { + AMDILCodeGen(AMDILCodeGen&); // DO NOT IMPLEMENT. + AMDILCodeGen(); // DO NOT IMPLEMENT. + public: + AMDILCodeGen(aclCompiler *cl, aclBinary *elf, llvm::LLVMContext *ctx) + : CLCodeGen(cl, elf, ctx) {} + + virtual ~AMDILCodeGen() {} + + /*! Function that takes in an LLVM module as input + * and generates code for it based on the target + * device. + * Returns 0 on success and non-zero on failure. + */ + int codegen(llvm::Module *input) = 0; + + }; // class AMDILCodeGen + /*@}*/ +#endif +} // amdcl namespace +#endif // _BE_CODEGEN_HPP_ diff --git a/rocclr/compiler/lib/backends/common/compiler_stage.cpp b/rocclr/compiler/lib/backends/common/compiler_stage.cpp new file mode 100644 index 0000000000..36a855a7cb --- /dev/null +++ b/rocclr/compiler/lib/backends/common/compiler_stage.cpp @@ -0,0 +1,99 @@ +// +// Copyright (c) 2012 Advanced Micro Devices, Inc. All rights reserved. +// +#include "compiler_stage.hpp" +#include "utils/libUtils.h" + +using namespace amdcl; + +CompilerStage::CompilerStage(aclCompiler* cl, aclBinary* elf, + aclLogFunction callback) +: cl_(cl), elf_(elf), binary_(NULL), + source_(""), log_(""), callback_(callback) +{ + opts_ = (amd::option::Options*)Elf()->options; +} + +CompilerStage::~CompilerStage() +{ } + +LLVMCompilerStage::LLVMCompilerStage(aclCompiler *cl, aclBinary *elf, + aclLogFunction callback) +: CompilerStage(cl, elf, callback), + llvmbinary_(NULL), + context_(NULL) +{ + if (!Options()->oVariables->DisableAllWarnings) { + hookup_.LLVMBuildLog = &log_; + } + // Expose some options to LLVM. + llvm::AMDOptions *amdopts = &hookup_.amdoptions; + amdopts->OptLiveness = Options()->oVariables->OptLiveness; + if (isHSAILTarget(Elf()->target)) { + if ((amdopts->NumAvailGPRs == ~0u) || (Options()->NumAvailGPRs != -1)) + amdopts->NumAvailGPRs = Options()->NumAvailGPRs; + } else { + amdopts->OptPrintLiveness = Options()->oVariables->OptPrintLiveness; + amdopts->OptMem2reg = Options()->oVariables->OptMem2reg; + amdopts->UseJIT = Options()->oVariables->UseJIT; + amdopts->APThreshold = Options()->oVariables->APThreshold; + amdopts->AAForBarrier = Options()->oVariables->AAForBarrier; + amdopts->UnrollScratchThreshold = 500; + amdopts->AmdilUseDefaultResId = Options()->oVariables->DefaultResourceId; + } + amdopts->OptSimplifyLibCall = Options()->oVariables->OptSimplifyLibCall; + amdopts->EnableFDiv2FMul = Options()->oVariables->EnableFDiv2FMul; + amdopts->SRThreshold = Options()->oVariables->SRThreshold; + amdopts->OptMemCombineMaxVecGen = Options()->oVariables->OptMemCombineMaxVecGen; + amdopts->OptLICM = Options()->oVariables->OptLICM; + + // math-related options + amdopts->UnsafeMathOpt = Options()->oVariables->UnsafeMathOpt; + amdopts->NoSignedZeros = Options()->oVariables->NoSignedZeros; + amdopts->FiniteMathOnly = Options()->oVariables->FiniteMathOnly; + amdopts->FastRelaxedMath = Options()->oVariables->FastRelaxedMath; + + amdopts->LUThreshold = Options()->oVariables->LUThreshold; + amdopts->LUCount = Options()->oVariables->LUCount; + amdopts->LUAllowPartial = Options()->oVariables->LUAllowPartial; + amdopts->GPUArch = (uint32_t)getLibraryType(&elf->target); +} + + void +LLVMCompilerStage::setContext(aclContext *ctx) +{ + context_ = reinterpret_cast(ctx); + if (ctx) { + Context().setAMDLLVMContextHook(&hookup_); + } +} + +LLVMCompilerStage::~LLVMCompilerStage() +{ + if (context_) { + Context().setAMDLLVMContextHook(NULL); + } +} + + llvm::Module* +LLVMCompilerStage::loadBitcode(std::string& llvmBinary) +{ + if (!llvm::isBitcode(reinterpret_cast(llvmBinary.data()), + reinterpret_cast(llvmBinary.data() + + llvmBinary.length()))) { + llvm::SMDiagnostic diags; + return ParseAssemblyString(llvmBinary.c_str(), llvmbinary_, diags, Context()); + + } + // Use getMemBuffer() ? + if (llvm::MemoryBuffer *Buffer = + llvm::MemoryBuffer::getMemBufferCopy( + llvm::StringRef(llvmBinary), "input.bc")) { + std::string ErrorMessage; + llvm::Module* M = + llvm::ParseBitcodeFile(Buffer, Context(), &ErrorMessage); + delete Buffer; + return M; + } + return NULL; +} diff --git a/rocclr/compiler/lib/backends/common/compiler_stage.hpp b/rocclr/compiler/lib/backends/common/compiler_stage.hpp new file mode 100644 index 0000000000..2d14bdde9a --- /dev/null +++ b/rocclr/compiler/lib/backends/common/compiler_stage.hpp @@ -0,0 +1,96 @@ +// +// Copyright (c) 2012 Advanced Micro Devices, Inc. All rights reserved. +// +#ifndef _BE_COMPILER_STAGE_HPP_ +#define _BE_COMPILER_STAGE_HPP_ +#include "aclTypes.h" +#include "utils/options.hpp" +#include "llvm/AMDLLVMContextHook.h" +#include "llvm/LLVMContext.h" +#include "llvm/Module.h" +#include "llvm/PassManager.h" +#include "llvm/Pass.h" +#include "llvm/Support/MemoryBuffer.h" +#include "llvm/Support/SourceMgr.h" +#include "llvm/Bitcode/ReaderWriter.h" +#include "llvm/Assembly/Parser.h" + +#include +#include + +namespace amdcl +{ + /*! \addtogroup CompilerLibrary + * + * \copydoc amdcl::CompilerStage + * + * @{ + */ + class CompilerStage { + private: + CompilerStage(); // DO NOT IMPLEMENT. + CompilerStage(CompilerStage&); // DO NOT IMPLEMENT. + public: + CompilerStage(aclCompiler* cl, aclBinary* elf, aclLogFunction callback); + + virtual ~CompilerStage(); + + /*! Returns the Compiler */ + aclCompiler* CL() const { return cl_; } + + /*! Returns the elf binary */ + aclBinary* Elf() const { return elf_; } + + /*! Returns the callback */ + aclLogFunction Callback() const { return callback_; } + + /*! Returns the options */ + amd::option::Options* Options() const { + assert(opts_ && "Options should not be null"); + return opts_; + } + + + /*! Returns the source file */ + std::string& Source() { return source_; } + + /*! Returns the build log */ + std::string& BuildLog() { return log_; } + + protected: + aclCompiler *cl_; + aclBinary *elf_; + void *binary_; + amd::option::Options* opts_; + std::string source_; + std::string log_; + aclLogFunction callback_; + }; // class CompilerStage + + class LLVMCompilerStage : public CompilerStage { + public: + LLVMCompilerStage(aclCompiler *cl, aclBinary *elf, + aclLogFunction callback); + virtual ~LLVMCompilerStage(); + void setContext(aclContext *ctx); + + /*! Returns the local context */ + llvm::LLVMContext& Context() { return (*context_); } + + /*! Loads bitcode in either text or binary format and return + * and LLVM module. */ + virtual llvm::Module* loadBitcode(std::string& llvmBinary); + void setGPU(bool isForGPU) { hookup_.amdoptions.IsGPU = isForGPU; } + void setWholeProgram(bool Val) { hookup_.amdoptions.WholeProgram = Val; } + + /*! Returns the llvm binary */ + llvm::Module* LLVMBinary() const { return llvmbinary_; } + aclModule* Module() const { return reinterpret_cast(llvmbinary_);} + protected: + llvm::Module *llvmbinary_; + llvm::LLVMContext *context_; + llvm::AMDLLVMContextHook hookup_; + }; // class CompilerStage + /*@}*/ +} +#endif // _BE_COMPILER_STAGE_HPP_ diff --git a/rocclr/compiler/lib/backends/common/frontend.cpp b/rocclr/compiler/lib/backends/common/frontend.cpp new file mode 100644 index 0000000000..a89e7802a1 --- /dev/null +++ b/rocclr/compiler/lib/backends/common/frontend.cpp @@ -0,0 +1,295 @@ +// +// Copyright (c) 2011 Advanced Micro Devices, Inc. All rights reserved. +// +#include "top.hpp" +#include "frontend.hpp" +#include "bif/bifbase.hpp" +#include "utils/libUtils.h" +#include "utils/target_mappings.h" +#include "utils/options.hpp" +#include "os/os.hpp" +#include "llvm/ADT/StringRef.h" +#include +#include +#include +#include +#include +#include + +bool Is64bitMachine() {return sizeof(void*) == 8;} + +void +amdcl::OCLFrontend::appendCLVersionFlag(std::stringstream &ss, + const amd::option::Options *opts) +{ + llvm::StringRef clStd(opts->oVariables->CLStd); + + if (clStd == "CL1.0") { + ss << "--opencl=1.0 "; + } else if (clStd == "CL1.1") { + ss << "--opencl=1.1 "; + } else if (clStd == "CL1.2") { + ss << "--opencl=1.2 "; + } else { + if (clStd != "CL2.0") { + appendLogToCL(CL(), "Warning: invalid value for -cl-std, defaulting to CL1.2"); + ss << "--opencl=1.2 "; + return; + } + + ss << "--opencl=2.0 "; + } +} + +/// +/// @brief Function that converts elf + src combo into the correct +/// sequence of commands to call the CLC frontend. +/// +/// FIXME: This needs to be modified so writing to a file is +/// not necessary! +std::string +amdcl::OCLFrontend::getFrontendCommand(aclBinary *elf, + const std::string &src, + std::string &logFile, + std::string &clFile, + bool preprocessOnly) +{ + std::stringstream systemPath; + std::fstream f; + amd::option::Options* Opts = (amd::option::Options*)elf->options; + + f.open(clFile.c_str(), (std::fstream::out | std::fstream::binary)); + f.write(src.data(), src.length()); + f.close(); + + bool enableSpir = false; +#ifdef DEBUG + enableSpir = getenv("AMD_OCL_ENABLE_SPIR"); +#endif + + if (enableSpir) + systemPath << "clc --spir --emit=spirbc "; + else + systemPath << "clc --emit=llvmbc "; + + appendCLVersionFlag(systemPath, Opts); + + if (enableSpir) + systemPath << "--amd-options-begin " << Opts->origOptionStr << " --amd-options-end "; + +#if WITH_VERSION_0_8 + if (checkFlag(aclutGetCaps(elf), capImageSupport)) { + systemPath << "-D__IMAGE_SUPPORT__=1 "; + } + + if (checkFlag(aclutGetCaps(elf), capFMA)) { + systemPath << "-DFP_FAST_FMAF=1 "; + systemPath << "-DFP_FAST_FMA=1 "; + } +#elif WITH_VERSION_0_9 +#else +#error "The current version was not handled correctly here." +#endif + + // F_IMAGES + if (Options()->oVariables->ImageSupport) { + systemPath << "-D__IMAGE_SUPPORT__=1 "; + } + + if (Options()->oVariables->FastFMA) { + systemPath << "-DFP_FAST_FMA=1 "; + } + + if (Options()->oVariables->FastFMAF) { + systemPath << "-DFP_FAST_FMAF=1 "; + } + + systemPath << "-D__AMD__=1 "; + uint32_t chipName = elf->target.chip_id; + assert(chipName < familySet[elf->target.arch_id].children_size && "Cannot index past end of array!"); + switch(elf->target.arch_id) { + default: + assert(!"Unknown target device ID!"); + case aclX64: + systemPath << "--march=x86-64 -D__X86_64__=1 -D__" << X64TargetMapping[chipName].chip_name << "__=1 "; + break; + case aclX86: + systemPath << "--march=x86 -D__X86__=1 -D__" << X86TargetMapping[chipName].chip_name << "__=1 "; + break; + case aclAMDIL: + systemPath << "-D__AMDIL__ -D__" << AMDILTargetMapping[chipName].chip_name << "__=1 "; + break; + case aclAMDIL64: + systemPath << "--march=gpu-64 -D__AMDIL_64__ -D__" << AMDIL64TargetMapping[chipName].chip_name << "__=1 "; + break; +#if WITH_VERSION_0_9 + case aclA64: + systemPath << "--march=arm64 -D__ARM_64__=1 -D__" << A64TargetMapping[chipName].chip_name << "__=1 "; + break; + case aclARM: + systemPath << "--march=arm -D__ARM__=1 -D__" << A32TargetMapping[chipName].chip_name << "__=1 "; + break; +#elif WITH_VERSION_0_8 +#else +#error "The current version was not handled correctly here." +#endif + case aclHSAIL: + systemPath << "--march=hsail -D__HSAIL__ -D__" << HSAILTargetMapping[chipName].chip_name << "__=1 "; + break; + case aclHSAIL64: + systemPath << "--march=hsail-64 -D__HSAIL__ -D__" << HSAIL64TargetMapping[chipName].chip_name << "__=1 "; + break; + }; + // AMDIL and non CPU HSAIL targets get the GPU define, everything + // else gets CPU define. + if (!isCpuTarget(elf->target)) { + systemPath << "-D__GPU__=1 "; + } else { + systemPath << "-D__CPU__=1 "; + } + + if (elf->target.arch_id == aclAMDIL + && AMDILTargetMapping[chipName].family_enum == FAMILY_RV7XX) { + systemPath << "-Dcl_amd_vec3=1 -Dcl_amd_printf=1 --opencl=1.0"; + } + + if (Opts) { + systemPath << Opts->clcOptions; + } + +#ifdef WITH_TARGET_HSAIL + if ((Is64bitMachine() && isHSAILTarget(elf->target)) || + (Opts->oVariables->GPU64BitIsa && (elf->target.arch_id == aclHSAIL))) + systemPath << " --march=hsail-64 "; +#endif + +#ifdef DEBUG + const char* env = getenv("AMD_EDG_OPTIONS"); + if (env) + systemPath << env << " "; +#endif + +#ifdef DEBUG + if (!getenv("AMD_OCL_SHOW_COMPILER_OUTPUT")) +#endif + systemPath << " --error_output \"" << logFile << "\" "; + if(preprocessOnly) { + std::string clppFileName = Opts->getDumpFileName(".i"); + systemPath << " -E -o \"" << clppFileName << "\""; + } + systemPath << " \"" << clFile << "\" "; + + LogPrintfDebug("Invoking CL to LLVM binary compilation:\n %s", + systemPath.str().c_str()); + +#ifdef DEBUG + if(getenv("AMD_OCL_SHOW_CMD_LINE")) + std::cout << "command line: " << systemPath.str() << std::endl; +#endif + + if (Opts && Opts->isDumpFlagSet(amd::option::DUMP_CL) && !preprocessOnly) { + std::string clFileName = Opts->getDumpFileName(".cl"); + f.open(clFileName.c_str(), (std::fstream::out | std::fstream::binary)); + f.write(src.data(), src.length()); + f.close(); + } + std::string clcCmd = systemPath.str(); + return clcCmd; +} +// CLC_IN_PROCESS_CHANGE +extern int openclFrontEnd(const char* cmdline, std::string*, std::string* typeInfo = NULL); + +static std::string +loadFileToStr(std::string file) +{ + std::string str = ""; + std::ifstream log(file.c_str(), std::ios::in|std::ios::ate); + if (log.is_open()) { + size_t size = (size_t)log.tellg(); + log.seekg(0, std::ios::beg); + + std::vector buffer(size+1); + log.read(&buffer[0],size); + log.close(); + //for safety + buffer[size] = '\0'; + str += &buffer[0]; + } + return str; +} + +int +amdcl::OCLFrontend::compileCommand(const std::string& singleSrc) +{ + + std::string tempFileName = amd::Os::getTempFileName(); + std::string logFile = tempFileName + ".log"; + std::string clFile = tempFileName + ".cl"; + std::string frontendCmd = getFrontendCommand(Elf(), singleSrc, logFile, + clFile, false); + std::string logStr; + uint64_t start_time = 0, stop_time = 0; + amd::option::Options* Opts = (amd::option::Options*)Elf()->options; + + if (Options()->oVariables->EnableBuildTiming) { + start_time = amd::Os::timeNanos(); + } + if ( +#if WITH_VERSION_0_8 + !checkFlag(aclutGetCaps(Elf()), capSaveSOURCE) +#elif WITH_VERSION_0_9 + !Options()->oVariables->BinSOURCE +#else +#error "The current version was not handled correctly here." +#endif + ) { + CL()->clAPI.remSec(CL(), Elf(), aclSOURCE); + } + int ret = openclFrontEnd(frontendCmd.c_str(), &Source(), NULL); + + // We dump the preprocessed code by invoking clc a second time after the + // original call, just in case somthing really bad happens in the original + // call. + if (Opts && Opts->isDumpFlagSet(amd::option::DUMP_I)) { + std::string pplogFile = tempFileName + "preprocess.log"; + std::string ppFrontendCmd = + getFrontendCommand(Elf(), singleSrc, pplogFile, clFile, true); + (void) openclFrontEnd(ppFrontendCmd.c_str(), &Source(), NULL); + amd::Os::unlink(pplogFile.c_str()); + } + if (Options()->oVariables->EnableBuildTiming) { + stop_time = amd::Os::timeNanos(); + std::stringstream tmp_ss; + tmp_ss << " OpenCL FE time: " + << (stop_time - start_time)/1000ULL + << "us\n"; + appendLogToCL(CL(), tmp_ss.str()); + } + llvmbinary_ = loadBitcode(Source()); + if (!llvmbinary_) { + ret |= 1; + } + if (!ret && +#if WITH_VERSION_0_8 + checkFlag(aclutGetCaps(Elf()), capSaveLLVMIR) +#elif WITH_VERSION_0_9 + Options()->oVariables->BinLLVMIR +#else +#error "The current version was not handled correctly here." +#endif + ) { + CL()->clAPI.insSec(CL(), Elf(), Source().data(), + Source().size(), aclLLVMIR); + } else { + CL()->clAPI.remSec(CL(), Elf(), aclLLVMIR); + } + log_ += loadFileToStr(logFile); + amd::Os::unlink(logFile.c_str()); + if (isCpuTarget(Elf()->target) + && Options()->oVariables->EnableDebug) { + Options()->sourceFileName_ = clFile; + } else { + amd::Os::unlink(clFile.c_str()); + } + return ret; +} diff --git a/rocclr/compiler/lib/backends/common/frontend.hpp b/rocclr/compiler/lib/backends/common/frontend.hpp new file mode 100644 index 0000000000..0c9145f84d --- /dev/null +++ b/rocclr/compiler/lib/backends/common/frontend.hpp @@ -0,0 +1,99 @@ +// +// Copyright (c) 2008 Advanced Micro Devices, Inc. All rights reserved. +// +#ifndef _BE_FRONTEND_HPP_ +#define _BE_FRONTEND_HPP_ +#include +#include "aclTypes.h" +#include "compiler_stage.hpp" + +namespace amdcl +{ + /*! \addtogroup CompilerLibrary + * + * \copydoc amdcl::Frontend + * + * @{ + * \brief Interface parent class for the frontend child classes. + * This class should never be instantiated directly. + */ + class Frontend : public LLVMCompilerStage { + Frontend(Frontend&); // DO NOT IMPLEMENT. + Frontend(); // DO NOT IMPLEMENT. + public: + Frontend(aclCompiler* cl, aclBinary* elf, aclLogFunction log) + : LLVMCompilerStage(cl, elf, log) {} + //! Virtual destructer that makes sure everything is cleaned up. + virtual ~Frontend() {} + + //! Function that converts from OpenCL singleSrc into + // OpenCL formatted LLVM-IR stored as a std::string. + // This function generates a command string for clc to execute. + virtual int compileCommand(const std::string& singleSrc) = 0; + + }; // class Frontend + /*@}*/ + + /*! \addtogroup CompilerLibrary + * + * \copydoc amdcl::OCLFrontend + * + * @{ + * \brief Implementation of the Frontend interface to compile + * from OpenCL C to LLVM-IR. + */ + class OCLFrontend : public Frontend { + OCLFrontend(OCLFrontend&); // DO NOT IMPLEMENT. + OCLFrontend(); // DO NOT IMPLEMENT. + + void appendCLVersionFlag( + std::stringstream &ss, + const amd::option::Options *opts); + + std::string getFrontendCommand( + aclBinary *elf, + const std::string &src, + std::string &logFile, + std::string &clFile, + bool preprocessOnly); + + public: + OCLFrontend(aclCompiler* cl, aclBinary* elf, aclLogFunction log) + : Frontend(cl, elf, log) {} + + virtual ~OCLFrontend() {} + + //! Function that converts from OpenCL singleSrc into + // OpenCL formatted LLVM-IR stored as a std::string. + // This function generates a command string for clc to execute. + virtual int compileCommand(const std::string& singleSrc); + + }; // class OCLFrontend + /*@}*/ + + + /*! \addtogroup CompilerLibrary + * + * \copydoc amdcl::Frontend + * + * @{ + * \brief This is the class which calls the clang front-end. + * This class will be used if user asks for it (By default EDG will be + * called). + */ + class ClangOCLFrontend : public Frontend { + //! Options to be passed to the ClangOCLFE library. + + public: + ClangOCLFrontend(aclCompiler* cl, aclBinary* elf, aclLogFunction log); + + //! Virtual destructer that makes sure everything is cleaned up. + virtual ~ClangOCLFrontend() {} + + //! This function generates a command string for ClangOCLFE to execute. + virtual int compileCommand(const std::string& singleSrc); + + }; // class Frontend + /*@}*/ +} // namespac amdcl +#endif // _BE_FRONTEND_HPP_ diff --git a/rocclr/compiler/lib/backends/common/frontend_clang.cpp b/rocclr/compiler/lib/backends/common/frontend_clang.cpp new file mode 100644 index 0000000000..e7ae76e70e --- /dev/null +++ b/rocclr/compiler/lib/backends/common/frontend_clang.cpp @@ -0,0 +1,202 @@ +// +// Copyright (c) 2008 Advanced Micro Devices, Inc. All rights reserved. +// + +#include "OpenCLFE.h" + +#include "bif/bifbase.hpp" +#include "frontend.hpp" +#include "os/os.hpp" +#include "top.hpp" +#include "utils/libUtils.h" +#include "utils/options.hpp" +#include "utils/target_mappings.h" + +#include "llvm/Support/ErrorHandling.h" +#include "llvm/Support/MemoryBuffer.h" +#include "llvm/ADT/StringRef.h" + +#include +#include +#include + +amdcl::ClangOCLFrontend::ClangOCLFrontend(aclCompiler* cl, aclBinary* elf, + aclLogFunction log) + : Frontend(cl, elf, log){} + +/// @brief This function generates the required command-line options to +/// call the ClangOCLFE library. +int amdcl::ClangOCLFrontend::compileCommand(const std::string& src) { + + std::vector argsToClang; + std::string tempFileName = amd::Os::getTempFileName(); + std::string logFileName = tempFileName + ".log"; + std::string inpCLFileName = tempFileName + ".cl"; + std::string logFromClang; + int ret = 0; + + aclBinary *elf = Elf(); + amd::option::Options* amdOpts = (amd::option::Options*)elf->options; + + // Following are the options passed to the ClangOCLFE library + // and then to Clang itself. + + // Passing the compiler FE options to clang. + if (amdOpts) { + for (std::vector::const_iterator it = amdOpts->clangOptions.begin(); + it != amdOpts->clangOptions.end(); ++it) { + argsToClang.push_back((*it).c_str()); + } + } + + // Other options are passed using OptionsInfo structure. + clc2::OptionsInfo ClangOptions; + + ClangOptions.InFilename = inpCLFileName; + + // Generate target triple. + // TODO: Refine the triple as necessary. + uint32_t chipName = elf->target.chip_id; + assert(chipName < familySet[elf->target.arch_id].children_size && + "Cannot index past end of array!"); + switch (elf->target.arch_id) { + default: + log_ += "\nerror: Unknown target device ID!\n"; + ret |= 1; + return ret; + break; + case aclX86: + case aclAMDIL: + case aclHSAIL: + // See bug: http://ocltc.amd.com/bugs/show_bug.cgi?id=9631 + if (sizeof(void*) != 4) { + log_ += "\nerror: 32-bit kernels not supported on a 64-bit executable\n"; + ret |= 1; + return ret; + } + ClangOptions.TargetArch = llvm::Triple::spir; + break; + case aclX64: + case aclAMDIL64: + case aclHSAIL64: + // See bug: http://ocltc.amd.com/bugs/show_bug.cgi?id=9631 + if (sizeof(void*) != 8) { + log_ += "\nerror: 64-bit kernels not supported on a 32-bit executable\n"; + ret |= 1; + return ret; + } + ClangOptions.TargetArch = llvm::Triple::spir64; + break; + }; + + // Copy the source to a buffer. Note that the input + // file itself is not passed to the ClangOCLFE library. It is a passed + // as a string for compilation. + + llvm::MemoryBuffer *srcBuffer = + llvm::MemoryBuffer::getMemBuffer(src, inpCLFileName.c_str(), + true); + llvm::OwningPtr srcBufferPtr(srcBuffer); + ClangOptions.Src.swap(srcBufferPtr); + assert(ClangOptions.Src.get() && "ClangOCLFE: Memory Buffer" + " initialization error\n"); + + // Set Pre-processor output if user asks for it. + if (amdOpts && amdOpts->isDumpFlagSet(amd::option::DUMP_I)) { + ClangOptions.PreProcOut = amdOpts->getDumpFileName(".i"); + } + + // Set the LLVMContext for the front-end compilation. + ClangOptions.CompilerContext = &Context(); + + if (amdOpts && amdOpts->isDumpFlagSet(amd::option::DUMP_CL)) { + std::string inpCLFileName = amdOpts->getDumpFileName(".cl"); + std::fstream f; + f.open(inpCLFileName.c_str(), (std::fstream::out | std::fstream::binary)); + f.write(src.data(), src.length()); + f.close(); + } + + //Start the compilation + uint64_t start_time = 0, stop_time = 0; + + if (Options()->oVariables->EnableBuildTiming) { + start_time = amd::Os::timeNanos(); + } + + if ( +#if WITH_VERSION_0_8 + !checkFlag(aclutGetCaps(Elf()), capSaveSOURCE) +#elif WITH_VERSION_0_9 + !Options()->oVariables->BinSOURCE +#else +#error "The current version was not handled correctly here." +#endif + ) { + CL()->clAPI.remSec(CL(), Elf(), aclSOURCE); + } + + // Pass OpenCL version option to Clang + llvm::StringRef OCLVer(amdOpts->oVariables->CLStd); + if (OCLVer.equals("CL1.2")) { + ClangOptions.OCLVer = clc2::OCL_12; + } else if (OCLVer.equals("CL2.0")) { + ClangOptions.OCLVer = clc2::OCL_20; + } else { + llvm_unreachable("Unknown OpenCL version"); + } + + // Call the Clang Front-end to generate serialized llvm::Module + // from the OpenCL source. +#ifdef ANDROID + // We will not exercise Clang for RenderScript. + log_ += "\nerror: Clang front-end compilation unsupported on Android!\n"; + ret |= 1; + return ret; +#else + if (!parseOCLSource(ClangOptions, argsToClang, &Source(), &logFromClang)) { + log_ += logFromClang; + log_ += "\nerror: Clang front-end compilation failed!\n"; + ret |= 1; + return ret; + } +#endif + + if (Options()->oVariables->EnableBuildTiming) { + stop_time = amd::Os::timeNanos(); + std::stringstream tmp_ss; + tmp_ss << " OpenCL FE time: " + << (stop_time - start_time)/1000ULL + << "us\n"; + appendLogToCL(CL(), tmp_ss.str()); + } + + llvmbinary_ = loadBitcode(Source()); + + if (!llvmbinary_) { + ret |= 1; + } + + if (!ret && +#if WITH_VERSION_0_8 + checkFlag(aclutGetCaps(Elf()), capSaveLLVMIR) +#elif WITH_VERSION_0_9 + Options()->oVariables->BinLLVMIR +#else +#error "The current version was not handled correctly here." +#endif + ) { + CL()->clAPI.insSec(CL(), Elf(), Source().data(), + Source().size(), aclLLVMIR); + } else { + CL()->clAPI.remSec(CL(), Elf(), aclLLVMIR); + } + log_ += logFromClang; + if (isCpuTarget(Elf()->target) + && Options()->oVariables->EnableDebug) { + Options()->sourceFileName_ = inpCLFileName; + } else { + amd::Os::unlink(inpCLFileName.c_str()); + } + return ret; +} diff --git a/rocclr/compiler/lib/backends/common/if_acl.h b/rocclr/compiler/lib/backends/common/if_acl.h new file mode 100644 index 0000000000..e688dc880b --- /dev/null +++ b/rocclr/compiler/lib/backends/common/if_acl.h @@ -0,0 +1,15 @@ +// +// Copyright (c) 2012 Advanced Micro Devices, Inc. All rights reserved. +// +#ifndef _IF_ACL_H_ +#define _IF_ACL_H_ +#include "aclTypes.h" +#if WITH_VERSION_0_8 +#include "v0_8/if_acl.h" +#elif WITH_VERSION_0_9 +#include "v0_9/if_acl.h" +#else +#error "The compiler library version was not defined." +#include "v0_8/if_acl.h" +#endif +#endif // _IF_ACL_H_ diff --git a/rocclr/compiler/lib/backends/common/library.cpp b/rocclr/compiler/lib/backends/common/library.cpp new file mode 100644 index 0000000000..471bdbf3f7 --- /dev/null +++ b/rocclr/compiler/lib/backends/common/library.cpp @@ -0,0 +1,290 @@ + +// Copyright (c) 2008 Advanced Micro Devices, Inc. All rights reserved. +// + +#include +#include + +#include "top.hpp" +#include "aclTypes.h" +#include "library.hpp" +#include "utils/options.hpp" + +namespace amd { + +/* + Integrated bitcode libraries + */ + +// GPU libraries +#if defined(WITH_TARGET_AMDIL) +#include "builtins-gpugen-comm.inc" +#include "builtins-gpugen-diff.gpu.inc" +#include "builtins-gpugen-diff.gpu-64.inc" +#include "builtins-gpucommon-comm.inc" +#include "builtins-gpucommon-diff.gpu.inc" +#include "builtins-gpucommon-diff.gpu-64.inc" +#include "builtins-SI-comm.inc" +#include "builtins-SI-diff.gpu.inc" +#include "builtins-SI-diff.gpu-64.inc" +#include "builtins-CI-comm.inc" +#include "builtins-CI-diff.gpu.inc" +#include "builtins-CI-diff.gpu-64.inc" +#endif // WITH_TARGET_AMDIL + +// CPU libraries +#if defined(WITH_TARGET_X86) +#include "builtins-cpugen.x86.inc" +#include "builtins-cpucommon.x86.inc" +#include "builtins-avx.x86.inc" +#include "builtins-fma4.x86.inc" +#include "builtins-cpugen.x86-64.inc" +#include "builtins-cpucommon.x86-64.inc" +#include "builtins-avx.x86-64.inc" +#include "builtins-fma4.x86-64.inc" +#endif // WITH_TARGET_X86 + +#if defined(WITH_TARGET_ARM) +#include "builtins-cpugen.arm.inc" +#include "builtins-cpucommon.arm.inc" +#endif // WITH_TARGET_ARM + +#ifdef WITH_TARGET_HSAIL +// HSAIL libraries +#include "builtins-hsail.inc" +#include "builtins-hsail-amd-ci.inc" +#include "builtins-gcn.inc" +#endif + +#include +// getLibsDesc() : returns a list of libraries that need to be linked with the +// application. The max number of libraries is defined by +// enum MAX_NUM_LIBRARY_DESCS in class LibraryDescriptor. +// +// Return 0: successful +// : error happened +int +getLibDescs ( + LibrarySelector LibType, // input + LibraryDescriptor* LibDesc, // output + int& LibDescSize // output -- LibDesc[0:LibDescSize-1] +) +{ + switch (LibType) { +#if defined(WITH_TARGET_AMDIL) + case GPU_Library_Evergreen: + // Library order is important! + LibDesc[0].start = reinterpret_cast + (builtins_gpucommon_comm); + LibDesc[0].size = builtins_gpucommon_comm_size; + LibDesc[1].start = reinterpret_cast + (builtins_gpucommon_diff_gpu); + LibDesc[1].size = builtins_gpucommon_diff_gpu_size; + LibDesc[2].start = reinterpret_cast + (builtins_gpugen_comm); + LibDesc[2].size = builtins_gpugen_comm_size; + LibDesc[3].start = reinterpret_cast + (builtins_gpugen_diff_gpu); + LibDesc[3].size = builtins_gpugen_diff_gpu_size; + LibDescSize = 4; + break; + + case GPU_Library_SI: + // Library order is important! + LibDesc[0].start = reinterpret_cast + (builtins_SI_comm); + LibDesc[0].size = builtins_SI_comm_size; + LibDesc[1].start = reinterpret_cast + (builtins_SI_diff_gpu); + LibDesc[1].size = builtins_SI_diff_gpu_size; + LibDesc[2].start = reinterpret_cast + (builtins_gpucommon_comm); + LibDesc[2].size = builtins_gpucommon_comm_size; + LibDesc[3].start = reinterpret_cast + (builtins_gpucommon_diff_gpu); + LibDesc[3].size = builtins_gpucommon_diff_gpu_size; + LibDesc[4].start = reinterpret_cast + (builtins_gpugen_comm); + LibDesc[4].size = builtins_gpugen_comm_size; + LibDesc[5].start = reinterpret_cast + (builtins_gpugen_diff_gpu); + LibDesc[5].size = builtins_gpugen_diff_gpu_size; + LibDescSize = 6; + break; + + case GPU64_Library_SI: + // Library order is important! + LibDesc[0].start = reinterpret_cast + (builtins_SI_comm); + LibDesc[0].size = builtins_SI_comm_size; + LibDesc[1].start = reinterpret_cast + (builtins_SI_diff_gpu_64); + LibDesc[1].size = builtins_SI_diff_gpu_64_size; + LibDesc[2].start = reinterpret_cast + (builtins_gpucommon_comm); + LibDesc[2].size = builtins_gpucommon_comm_size; + LibDesc[3].start = reinterpret_cast + (builtins_gpucommon_diff_gpu_64); + LibDesc[3].size = builtins_gpucommon_diff_gpu_64_size; + LibDesc[4].start = reinterpret_cast + (builtins_gpugen_comm); + LibDesc[4].size = builtins_gpugen_comm_size; + LibDesc[5].start = reinterpret_cast + (builtins_gpugen_diff_gpu_64); + LibDesc[5].size = builtins_gpugen_diff_gpu_64_size; + LibDescSize = 6; + break; + + case GPU_Library_CI: + // Library order is important! + LibDesc[0].start = reinterpret_cast + (builtins_CI_comm); + LibDesc[0].size = builtins_CI_comm_size; + LibDesc[1].start = reinterpret_cast + (builtins_CI_diff_gpu); + LibDesc[1].size = builtins_CI_diff_gpu_size; + LibDesc[2].start = reinterpret_cast + (builtins_SI_comm); + LibDesc[2].size = builtins_SI_comm_size; + LibDesc[3].start = reinterpret_cast + (builtins_SI_diff_gpu); + LibDesc[3].size = builtins_SI_diff_gpu_size; + LibDesc[4].start = reinterpret_cast + (builtins_gpucommon_comm); + LibDesc[4].size = builtins_gpucommon_comm_size; + LibDesc[5].start = reinterpret_cast + (builtins_gpucommon_diff_gpu); + LibDesc[5].size = builtins_gpucommon_diff_gpu_size; + LibDesc[6].start = reinterpret_cast + (builtins_gpugen_comm); + LibDesc[6].size = builtins_gpugen_comm_size; + LibDesc[7].start = reinterpret_cast + (builtins_gpugen_diff_gpu); + LibDesc[7].size = builtins_gpugen_diff_gpu_size; + LibDescSize = 8; + break; + + case GPU64_Library_CI: + // Library order is important! + LibDesc[0].start = reinterpret_cast + (builtins_CI_comm); + LibDesc[0].size = builtins_CI_comm_size; + LibDesc[1].start = reinterpret_cast + (builtins_CI_diff_gpu_64); + LibDesc[1].size = builtins_CI_diff_gpu_64_size; + LibDesc[2].start = reinterpret_cast + (builtins_SI_comm); + LibDesc[2].size = builtins_SI_comm_size; + LibDesc[3].start = reinterpret_cast + (builtins_SI_diff_gpu_64); + LibDesc[3].size = builtins_SI_diff_gpu_64_size; + LibDesc[4].start = reinterpret_cast + (builtins_gpucommon_comm); + LibDesc[4].size = builtins_gpucommon_comm_size; + LibDesc[5].start = reinterpret_cast + (builtins_gpucommon_diff_gpu_64); + LibDesc[5].size = builtins_gpucommon_diff_gpu_64_size; + LibDesc[6].start = reinterpret_cast + (builtins_gpugen_comm); + LibDesc[6].size = builtins_gpugen_comm_size; + LibDesc[7].start = reinterpret_cast + (builtins_gpugen_diff_gpu_64); + LibDesc[7].size = builtins_gpugen_diff_gpu_64_size; + LibDescSize = 8; + break; + +#endif // WITH_TARGET_AMDIL + +#if defined(WITH_TARGET_X86) + case CPU64_Library_Generic: + LibDesc[0].start = reinterpret_cast(builtins_cpucommon_x86_64); + LibDesc[0].size = builtins_cpucommon_x86_64_size; + LibDesc[1].start = reinterpret_cast(builtins_cpugen_x86_64); + LibDesc[1].size = builtins_cpugen_x86_64_size; + LibDescSize = 2; + break; + + case CPU64_Library_AVX: + LibDesc[0].start = reinterpret_cast(builtins_avx_x86_64); + LibDesc[0].size = builtins_avx_x86_64_size; + LibDesc[1].start = reinterpret_cast(builtins_cpucommon_x86_64); + LibDesc[1].size = builtins_cpucommon_x86_64_size; + LibDesc[2].start = reinterpret_cast(builtins_cpugen_x86_64); + LibDesc[2].size = builtins_cpugen_x86_64_size; + LibDescSize = 3; + break; + + case CPU64_Library_FMA4: + LibDesc[0].start = reinterpret_cast(builtins_fma4_x86_64); + LibDesc[0].size = builtins_fma4_x86_64_size; + LibDesc[1].start = reinterpret_cast(builtins_avx_x86_64); + LibDesc[1].size = builtins_avx_x86_64_size; + LibDesc[2].start = reinterpret_cast(builtins_cpucommon_x86_64); + LibDesc[2].size = builtins_cpucommon_x86_64_size; + LibDesc[3].start = reinterpret_cast(builtins_cpugen_x86_64); + LibDesc[3].size = builtins_cpugen_x86_64_size; + LibDescSize = 4; + break; + + case CPU_Library_Generic: + LibDesc[0].start = reinterpret_cast(builtins_cpucommon_x86); + LibDesc[0].size = builtins_cpucommon_x86_size; + LibDesc[1].start = reinterpret_cast(builtins_cpugen_x86); + LibDesc[1].size = builtins_cpugen_x86_size; + LibDescSize = 2; + break; + + case CPU_Library_AVX: + LibDesc[0].start = reinterpret_cast(builtins_avx_x86); + LibDesc[0].size = builtins_avx_x86_size; + LibDesc[1].start = reinterpret_cast(builtins_cpucommon_x86); + LibDesc[1].size = builtins_cpucommon_x86_size; + LibDesc[2].start = reinterpret_cast(builtins_cpugen_x86); + LibDesc[2].size = builtins_cpugen_x86_size; + LibDescSize = 3; + break; + + case CPU_Library_FMA4: + LibDesc[0].start = reinterpret_cast(builtins_fma4_x86); + LibDesc[0].size = builtins_fma4_x86_size; + LibDesc[1].start = reinterpret_cast(builtins_avx_x86); + LibDesc[1].size = builtins_avx_x86_size; + LibDesc[2].start = reinterpret_cast(builtins_cpucommon_x86); + LibDesc[2].size = builtins_cpucommon_x86_size; + LibDesc[3].start = reinterpret_cast(builtins_cpugen_x86); + LibDesc[3].size = builtins_cpugen_x86_size; + LibDescSize = 4; + break; +#endif // WITH_TARGET_X86 + +#if defined(WITH_TARGET_ARM) + case CPU_Library_Generic: + LibDesc[0].start = reinterpret_cast(builtins_cpucommon_arm); + LibDesc[0].size = builtins_cpucommon_arm_size; + LibDesc[1].start = reinterpret_cast(builtins_cpugen_arm); + LibDesc[1].size = builtins_cpugen_arm_size; + LibDescSize = 2; + break; +#endif // WITH_TARGET_ARM + +#if defined(WITH_TARGET_HSAIL) + case GPU_Library_HSAIL: + // Library order is important! + LibDesc[0].start = reinterpret_cast(builtins_gcn); + LibDesc[0].size = builtins_gcn_size; + LibDesc[1].start = reinterpret_cast(builtins_hsail_amd_ci); + LibDesc[1].size = builtins_hsail_amd_ci_size; + LibDesc[2].start = reinterpret_cast(builtins_hsail); + LibDesc[2].size = builtins_hsail_size; + LibDescSize = 3; + break; +#endif // WITH_TARGET_HSAIL + + default: + // Failed + return 1; // + } + return 0; +} + +} // namespace amd diff --git a/rocclr/compiler/lib/backends/common/library.hpp b/rocclr/compiler/lib/backends/common/library.hpp new file mode 100644 index 0000000000..2d563a81f4 --- /dev/null +++ b/rocclr/compiler/lib/backends/common/library.hpp @@ -0,0 +1,59 @@ +// +// Copyright (c) 2008 Advanced Micro Devices, Inc. All rights reserved. +// + +#ifndef LIBRARY_H_ +#define LIBRARY_H_ + +#include +#include +namespace amd { + +typedef enum _library_selector_0_7 { + LibraryUndefined = 0, + GPU_Library_7xx, + GPU_Library_Evergreen, + GPU_Library_SI, + CPU_Library_Generic, + CPU_Library_AVX, + CPU_Library_FMA4, + GPU_Library_Generic, + CPU64_Library_Generic, + CPU64_Library_AVX, + CPU64_Library_FMA4, + GPU64_Library_Evergreen, + GPU64_Library_SI, + GPU64_Library_Generic, + GPU_Library_CI, + GPU64_Library_CI, + GPU_Library_HSAIL, + LibraryTotal +} LibrarySelector; + +/** Integrated Bitcode Libararies **/ +class LibraryDescriptor { +public: + enum {MAX_NUM_LIBRARY_DESCS = 11}; + + const char* start; + size_t size; +}; + +int getLibDescs ( + LibrarySelector LibType, // input + LibraryDescriptor* LibDesc, // output + int& LibDescSize // output -- LibDesc[0:LibDescSize-1] +); + +static const char* amdRTFuns[] = { + "__amdrt_div_i64", + "__amdrt_div_u64", + "__amdrt_mod_i64", + "__amdrt_mod_u64", + "__amdrt_cvt_f64_to_u64", + "__amdrt_cvt_f32_to_u64" +}; +} //amd + +#endif // LIBRARY_H_ + diff --git a/rocclr/compiler/lib/backends/common/linker.cpp b/rocclr/compiler/lib/backends/common/linker.cpp new file mode 100644 index 0000000000..f85e366a47 --- /dev/null +++ b/rocclr/compiler/lib/backends/common/linker.cpp @@ -0,0 +1,1299 @@ +// +// Copyright (c) 2008 Advanced Micro Devices, Inc. All rights reserved. +// +// TODO: The entire linker implementation should be a pass in LLVM and +// the code in the compiler library should only call this pass. + +#include "top.hpp" +#include "library.hpp" +#include "linker.hpp" +#include "os/os.hpp" +#include "thread/monitor.hpp" +#include "utils/libUtils.h" +#include "utils/options.hpp" +#include "utils/target_mappings.h" + +#include "acl.h" + +#include "llvm/Instructions.h" +#include "llvm/Linker.h" +#include "llvm/GlobalValue.h" +#include "llvm/GlobalVariable.h" + +#include "llvm/AMDResolveLinker.h" +#include "llvm/AMDPrelinkOpt.h" +#include "llvm/ADT/Triple.h" +#include "llvm/ADT/StringMap.h" +#include "llvm/Analysis/AMDLocalArrayUsage.h" +#include "llvm/Analysis/CodeMetrics.h" +#include "llvm/Analysis/LoopPass.h" +#include "llvm/Analysis/Passes.h" +#include "llvm/Analysis/Verifier.h" +#include "llvm/Bitcode/ReaderWriter.h" + +#include "llvm/CodeGen/LinkAllAsmWriterComponents.h" +#include "llvm/CodeGen/LinkAllCodegenComponents.h" +#if 1 || LLVM_TRUNK_INTEGRATION_CL >= 2270 +#else +#include "llvm/CodeGen/ObjectCodeEmitter.h" +#endif +#include "llvm/Config/config.h" + +#include "llvm/MC/SubtargetFeature.h" + +#include "llvm/Support/CallSite.h" +#include "llvm/Support/FileSystem.h" +#include "llvm/Support/FileUtilities.h" +#include "llvm/Support/FormattedStream.h" +#include "llvm/Support/Host.h" +#include "llvm/Support/ManagedStatic.h" +#include "llvm/Support/MemoryBuffer.h" +#include "llvm/Support/Path.h" +#include "llvm/Support/PluginLoader.h" +#include "llvm/Support/raw_ostream.h" +#include "llvm/Support/Signals.h" +#include "llvm/Support/system_error.h" +#include "llvm/Support/TargetRegistry.h" +#include "llvm/Support/TargetSelect.h" +#include "llvm/DataLayout.h" +#include "llvm/Target/TargetMachine.h" +#include "llvm/Target/TargetOptions.h" + +#include "llvm/Transforms/IPO.h" +#include "llvm/Transforms/Scalar.h" +#include "llvm/ValueSymbolTable.h" + +#ifdef _DEBUG +#include "llvm/Assembly/Writer.h" +#endif + +// need to undef DEBUG before using DEBUG macro in llvm/Support/Debug.h +#ifdef DEBUG +#undef DEBUG +#endif +#include "llvm/Support/Debug.h" + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#ifdef _WIN32 +#include +#endif // _WIN32 + +#ifdef DEBUG_TYPE +#undef DEBUG_TYPE +#endif +#define DEBUG_TYPE "ocl_linker" + +static const char* OptionMaskFName = "__option_mask"; +extern llvm::Module* +clpVectorExpansion(llvm::Module *srcModules[], std::string &errorMsg); +namespace amd { + +namespace { + +using namespace llvm; + +// LoadFile - Read the specified bitcode file in and return it. This routine +// searches the link path for the specified file to try to find it... +// +inline llvm::Module* + LoadFile(const std::string &Filename, LLVMContext& Context) + { + bool Exists; + if (sys::fs::exists(Filename, Exists) || !Exists) { + // dbgs() << "Bitcode file: '" << Filename.c_str() << "' does not exist.\n"; + return 0; + } + + llvm::Module* M; + std::string ErrorMessage; + OwningPtr Buffer; + if (error_code ec = MemoryBuffer::getFileOrSTDIN(Filename, Buffer)) { + // Error + M = NULL; + } + else { + M = ParseBitcodeFile(Buffer.get(), Context, &ErrorMessage); + } + + return M; + } + +inline llvm::Module* + LoadLibrary(const std::string& libFile, LLVMContext& Context, MemoryBuffer** Buffer) { + bool Exists; + if (sys::fs::exists(libFile, Exists) || !Exists) { + // dbgs() << "Bitcode file: '" << Filename.c_str() << "' does not exist.\n"; + return 0; + } + + llvm::Module* M = NULL; + std::string ErrorMessage; + + static Monitor mapLock; + static std::map FileMap; + MemoryBuffer* statBuffer; + { + ScopedLock sl(mapLock); + statBuffer = (MemoryBuffer*) FileMap[libFile]; + if (statBuffer == NULL) { + OwningPtr PtrBuffer; + if (error_code ec = MemoryBuffer::getFileOrSTDIN(libFile, PtrBuffer)) { + // Error + return NULL; + } + else + statBuffer = PtrBuffer.take(); + M = ParseBitcodeFile(statBuffer, Context, &ErrorMessage); + FileMap[libFile] = statBuffer; + } + } + *Buffer = MemoryBuffer::getMemBufferCopy(StringRef(statBuffer->getBufferStart(), statBuffer->getBufferSize()), ""); + if ( *Buffer ) { + M = getLazyBitcodeModule(*Buffer, Context, &ErrorMessage); + if (!M) { + delete *Buffer; + *Buffer = 0; + } + } + return M; + } + +// Load bitcode libary from an array of const char. This assumes that +// the array has a valid ending zero ! +llvm::Module* + LoadLibrary(const char* libBC, size_t libBCSize, + LLVMContext& Context, MemoryBuffer** Buffer) + { + llvm::Module* M = 0; + std::string ErrorMessage; + + *Buffer = MemoryBuffer::getMemBuffer(StringRef(libBC, libBCSize), ""); + if ( *Buffer ) { + M = getLazyBitcodeModule(*Buffer, Context, &ErrorMessage); + if (!M) { + delete *Buffer; + *Buffer = 0; + } + } + return M; + } + + +static std::set *getAmdRtFunctions() +{ + std::set *result = new std::set(); + for (size_t i = 0; i < sizeof(amdRTFuns)/sizeof(amdRTFuns[0]); ++i) + result->insert(amdRTFuns[i]); + return result; +} + +// Remove NoInline attribute to functions in a module +void +RemoveNoInlineAttr(llvm::Module* M) +{ + LLVMContext &Context = M->getContext(); + for (llvm::Module::iterator I = M->begin(), E = M->end(); I != E; ++I) { + I->removeFnAttr(Attributes::get(Context, Attributes::NoInline)); + } +} + +bool +IsKernel(llvm::Function* F) +{ + return F->getName().startswith("__OpenCL_") && + F->getName().endswith("_kernel"); +} + +// Add NoInline attribute to functions in a module +void +AddNoInlineAttr(llvm::Module* M) +{ + LLVMContext &Context = M->getContext(); + for (llvm::Module::iterator I = M->begin(), E = M->end(); I != E; ++I) { + if (I->getLinkage() != Function::InternalLinkage && I->hasName() && + !I->isDeclaration() && + !I->isIntrinsic() && + !I->getName().startswith("__amdil") && + !I->getFnAttributes().hasAttribute(Attributes::AlwaysInline) && + !IsKernel(I)) { + DEBUG_WITH_TYPE("noinline", + dbgs() << "[Candidate] " << I->getName() << '\n'); + I->addFnAttr(Attributes::NoInline); + } + } +} + +unsigned +CountCallSites(llvm::Function* F, llvm::Module* M, + std::map& counts) { + std::map::iterator iter = counts.find(F); + if (iter != counts.end()) + return iter->second; + + unsigned numCalled = 0; + for (Function::use_iterator I = F->use_begin(), E = F->use_end(); I != E; + ++I) { + User *UI = *I; + if (isa(UI) || isa(UI)) { + ImmutableCallSite CS(cast(UI)); + Function* caller = const_cast(CS.getCaller()); + unsigned callerCount = CountCallSites(caller, M, counts); + if (caller->getFnAttributes().hasAttribute(Attributes::NoInline) && + callerCount > 0) + numCalled++; + else + numCalled += callerCount; + } + } + if (numCalled == 0 && IsKernel(F)) + numCalled = 1; + + counts[F] = numCalled; + return numCalled; +} + +unsigned +CalculateSize(llvm::Function* F, llvm::Module* M, + std::map& sizes) { + std::map::iterator iter = sizes.find(F); + if (iter != sizes.end()) + return iter->second; + + CodeMetrics metrics; + metrics.analyzeFunction(F); + unsigned size = metrics.NumInsts; + for (Function::iterator I = F->begin(), E = F->end(); I != E; ++I) { + for (BasicBlock::iterator BI = I->begin(), BE = I->end(); BI != BE; ++BI) { + if (CallInst* callInst = dyn_cast(BI)) { + Function* called = callInst->getCalledFunction(); + if (called && + !called->getFnAttributes().hasAttribute(Attributes::NoInline)) + size += CalculateSize(called, M, sizes); + } + } + } + sizes[F] = size; + return size; +} + +// Identify functions with image arguments. +// Callers may pass images with different resource ids to the callee. +// Currently pointer manager cannot handle this. +// ToDo: Should remove this after we find a way to handle image in function. +bool +IsImageFunc(Function* F) { + for (Function::arg_iterator I = F->arg_begin(), E = F->arg_end(); I != E; + ++I) { + if (PointerType *PT = dyn_cast(I->getType())) { + if (PT->getAddressSpace() != 1) { + continue; + } + if (StructType *ST = dyn_cast(PT->getElementType())) { + if (ST->getName().startswith("struct._image")) { + DEBUG_WITH_TYPE("noinline", dbgs() << "[image function] " << + F->getName() << " inline\n"); + return true; + } + } + } + } + return false; +} + +bool +MustInline(Function* F) { + if (F->getFnAttributes().hasAttribute(Attributes::AlwaysInline)) + return true; + return IsImageFunc(F); +} + +bool +CallerMustInline(Function* F) { + return IsImageFunc(F); +} + +bool +CallsNoInlineFunc(Function* F, std::map& work) { + DEBUG_WITH_TYPE("noinline", dbgs() << "[CallsNoInlineFunc:" << F->getName() << " "); + std::map::iterator loc = work.find(F); + if (loc != work.end()) { + DEBUG_WITH_TYPE("noinline", dbgs() << loc->second << "(cached)]\n"); + return loc->second; + } + for (Function::iterator I = F->begin(), E = F->end(); I != E; ++I) { + for (BasicBlock::iterator BI = I->begin(), BE = I->end(); BI != BE; ++BI) { + if (CallInst* callInst = dyn_cast(BI)) { + Function* called = callInst->getCalledFunction(); + if (called) { + if (called->getFnAttributes().hasAttribute(Attributes::NoInline) || + CallerMustInline(called) || + CallsNoInlineFunc(called, work)) { + work[F] = true; + DEBUG_WITH_TYPE("noinline", dbgs() << "1(" << called->getName() <<")]\n"); + return true; + } + } + } + } + } + work[F] = false; + DEBUG_WITH_TYPE("noinline", dbgs() << "0]\n"); + return false; +} + +bool +CalledByNoInlineFunc(Function* F, std::map& work) { + DEBUG_WITH_TYPE("noinline", dbgs() << "[CalledByNoInlineFunc: " << F->getName() << " "); + std::map::iterator loc = work.find(F); + if (loc != work.end()) { + DEBUG_WITH_TYPE("noinline", dbgs() << loc->second << "]\n"); + return loc->second; + } + for (Function::use_iterator I = F->use_begin(), E = F->use_end(); I != E; + ++I) { + User *UI = *I; + if (isa(UI) || isa(UI)) { + ImmutableCallSite CS(cast(UI)); + Function* caller = const_cast(CS.getCaller()); + if (caller->getFnAttributes().hasAttribute(Attributes::NoInline) || + CalledByNoInlineFunc(caller, work)) { + work[F] = true; + DEBUG_WITH_TYPE("noinline", dbgs() << "1(" << caller->getName() <<")]\n"); + return true; + } + } + } + work[F] = false; + DEBUG_WITH_TYPE("noinline", dbgs() << "0]\n"); + return false; +} + +bool +CanBeNoInline(Function* F, std::map& callsNoInline, + std::map& calledByNoInline, bool allowMultiLevelCall) { + return !MustInline(F) && (allowMultiLevelCall || + (!CallsNoInlineFunc(F, callsNoInline) && + !CalledByNoInlineFunc(F, calledByNoInline))); +} + +struct CostInfo { + unsigned count; + unsigned size; + unsigned cost; +}; + +unsigned +CalculateMaxKernelSize(llvm::Module* M) { + std::map sizes; + unsigned maxSize = 0; + for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I) { + if (IsKernel(I)) { + unsigned kernelSize = CalculateSize(I, M, sizes); + DEBUG_WITH_TYPE("noinlines", dbgs() << "[Kernel size] " << + I->getName() << " : " << kernelSize << '\n'); + if (maxSize < kernelSize) + maxSize = kernelSize; + } + } + return maxSize; +} + +void +RefineNoInlineAttr(llvm::Module* M, int thresh, int sizeThresh, + int kernelSizeThresh, bool allowMultiLevelCall) +{ + if (thresh == 0 && sizeThresh == 0) + return; + + std::set candidates; + LLVMContext &Context = M->getContext(); + + for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I) { + if (I->getFnAttributes().hasAttribute(Attributes::NoInline)) { + candidates.insert(I); + I->removeFnAttr(Attributes::get(Context, Attributes::NoInline)); + } + } + + unsigned maxKernelSize = CalculateMaxKernelSize(M); + if (maxKernelSize < unsigned(kernelSizeThresh)) + return; + + while (true) { + std::map counts; + std::map sizes; + std::map costInfos; + std::map callsNoInline; + std::map calledByNoInline; + for (std::set::iterator I = candidates.begin(), + E = candidates.end(); I != E; ++I) { + Function* F = *I; + unsigned count = CountCallSites(F, M, counts); + if (count > 0 && CanBeNoInline(F, callsNoInline, calledByNoInline, + allowMultiLevelCall)) { + unsigned size = CalculateSize(F, M, sizes); + if (size > unsigned(sizeThresh)) { + CostInfo& info = costInfos[F]; + info.count = count; + info.size = size; + info.cost = (count - 1) * size; + DEBUG_WITH_TYPE("noinline", dbgs() << F->getName() << + " : " << count - 1 << " * " << size << " = " << (count-1) * size << + "\n"); + } + } + } + + int maxCost = -1; + Function* select = NULL; + for (std::map::iterator I = costInfos.begin(), + E = costInfos.end(); I != E; ++I) { + CostInfo& info = I->second; + if (int(info.cost) > maxCost) { + maxCost = int(info.cost); + select = I->first; + } + } + if (select == NULL || maxCost < thresh) + break; + CostInfo& info = costInfos[select]; + DEBUG_WITH_TYPE("noinlines", llvm::dbgs() << "select " << select->getName().str() + << " cost = " << info.count << " x " << info.size << " = " << + info.cost << "\n"); + + select->addFnAttr(Attributes::NoInline); + candidates.erase(select); + if (candidates.empty()) + break; + } + + if (getenv("AMD_OCL_INLINE")) { + for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I) { + if(I->hasName() && strstr(getenv("AMD_OCL_INLINE"), + I->getName().str().c_str())) { + I->removeFnAttr(Attributes::get(Context, Attributes::NoInline)); + printf("force inline %s\n", I->getName().data()); + } + } + } + + if (getenv("AMD_OCL_NOINLINE")) { + for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I) { + if(I->hasName() && strstr(getenv("AMD_OCL_NOINLINE"), + I->getName().str().c_str())) { + I->addFnAttr(Attributes::NoInline); + printf("force noinline %s\n", I->getName().data()); + } + } + } + +} + +} // unnamed namespace +} // namespace amd + +// create a llvm function which simply returns the given mask +static void createConstIntFunc(const char* fname, + int mask, + llvm::Module* module) +{ + llvm::LLVMContext& context = module->getContext(); + + llvm::Type* int32Ty = llvm::Type::getInt32Ty(context); + llvm::FunctionType* fType = llvm::FunctionType::get(int32Ty, false); + llvm::Function* function + = llvm::cast(module->getOrInsertFunction(fname, fType)); + function->setDoesNotThrow(); + function->setDoesNotAccessMemory(); + function->addFnAttr(llvm::Attributes::AlwaysInline); + llvm::BasicBlock* bb = llvm::BasicBlock::Create(context, "entry", function); + llvm::Value* retVal = llvm::ConstantInt::get(int32Ty, mask); + llvm::ReturnInst* retInst = llvm::ReturnInst::Create(context, retVal); + bb->getInstList().push_back(retInst); + assert(!verifyFunction(*function) && "verifyFunction failed"); +} + +// create a llvm function that returns a mask of several compile options +// which are used by the built-in library +void amdcl::OCLLinker::createOptionMaskFunction(llvm::Module* module) +{ + unsigned mask = 0; + if (Options()->oVariables->NoSignedZeros) { + mask |= MASK_NO_SIGNED_ZEROES; + } + if (Options()->oVariables->UnsafeMathOpt) { + mask |= MASK_UNSAFE_MATH_OPTIMIZATIONS; + mask |= MASK_NO_SIGNED_ZEROES; + } + if (Options()->oVariables->FiniteMathOnly) { + mask |= MASK_FINITE_MATH_ONLY; + } + if (Options()->oVariables->FastRelaxedMath) { + mask |= MASK_FAST_RELAXED_MATH; + mask |= MASK_FINITE_MATH_ONLY; + mask |= MASK_UNSAFE_MATH_OPTIMIZATIONS; + mask |= MASK_NO_SIGNED_ZEROES; + } + + if (Options()->oVariables->UniformWorkGroupSize) { + mask |= MASK_UNIFORM_WORK_GROUP_SIZE; + } + + createConstIntFunc(OptionMaskFName, mask, module); +} + +// Create functions that returns true or false for some features which +// are used by the built-in library +void amdcl::OCLLinker::createASICIDFunctions(llvm::Module* module) +{ + if (!isAMDILTarget(Elf()->target)) + return; + + uint64_t features = aclGetChipOptions(Elf()->target); + + llvm::StringRef chip(aclGetChip(Elf()->target)); + llvm::StringRef family(aclGetFamily(Elf()->target)); + + createConstIntFunc("__amdil_have_hw_fma32", + chip == "Cypress" + || chip == "Cayman" + || family == "SI" + || family == "CI" + || family == "KV" + || family == "TN" + || family == "VI" + || family == "CZ", + module); + createConstIntFunc("__amdil_have_fast_fma32", + chip == "Cypress" + || chip == "Cayman" + || chip == "Tahiti" + || chip == "Hawaii", + module); + createConstIntFunc("__amdil_have_bitalign", !!(features & F_EG_BASE), module); + createConstIntFunc("__amdil_is_cypress", chip == "Cypress", module); + createConstIntFunc("__amdil_is_ni", + chip == "Cayman" + || family == "TN", + module); + createConstIntFunc("__amdil_is_gcn", + family == "SI" + || family == "CI" + || family == "VI" + || family == "KV" + || family == "CZ", + module); +} + +bool +amdcl::OCLLinker::linkWithModule( + llvm::Module* Dst, llvm::Module* Src, + std::map *ModuleRefMap) +{ +#ifndef NDEBUG + if (Options()->oVariables->EnableDebugLinker) { + llvm::DebugFlag = true; + llvm::setCurrentDebugType(DEBUG_TYPE); + } +#endif + std::string ErrorMessage; + if (llvm::linkWithModule(Dst, Src, ModuleRefMap, &ErrorMessage)) { + DEBUG(llvm::dbgs() << "Error: " << ErrorMessage << "\n"); + BuildLog() += "\nInternal Error: linking libraries failed!\n"; + LogError("linkWithModule(): linking bc libraries failed!"); + return true; + } + return false; +} + + + +static void delete_llvm_module(llvm::Module *a) +{ + delete a; +} + bool +amdcl::OCLLinker::linkLLVMModules(std::vector &libs) +{ + // Load input modules first + bool Failed = false; + for (size_t i = 0; i < libs.size(); ++i) { + std::string ErrorMsg; + if (!libs[i]) { + char ErrStr[128]; + sprintf(ErrStr, + "Error: cannot load input %d bc for linking: %s\n", + (int)i, ErrorMsg.c_str()); + BuildLog() += ErrStr; + Failed = true; + break; + } + + if (Options()->isDumpFlagSet(amd::option::DUMP_BC_ORIGINAL)) { + std::string MyErrorInfo; + char buf[128]; + sprintf(buf, "_original%d.bc", (int)i); + std::string fileName = Options()->getDumpFileName(buf); + llvm::raw_fd_ostream outs(fileName.c_str(), MyErrorInfo, + llvm::raw_fd_ostream::F_Binary); + if (MyErrorInfo.empty()) + llvm::WriteBitcodeToFile(libs[i], outs); + else + printf(MyErrorInfo.c_str()); + } + } + + if (!Failed) { + // Link input modules together + for (size_t i = 0; i < libs.size(); ++i) { + DEBUG(llvm::dbgs() << "LinkWithModule " << i << ":\n"); + if (amdcl::OCLLinker::linkWithModule(LLVMBinary(), libs[i], NULL)) { + Failed = true; + } + } + } + + if (Failed) { + delete LLVMBinary(); + } + std::for_each(libs.begin(), libs.end(), std::ptr_fun(delete_llvm_module)); + libs.clear(); + return Failed; + +} + +void amdcl::OCLLinker::fixupOldTriple(llvm::Module *module) +{ + llvm::Triple triple(module->getTargetTriple()); + + // Bug 9357: "amdopencl" used to be a hacky "OS" that was Linux or Windows + // depending on the host. It only really matters for x86. If we are trying to + // use an old binary module still using the old triple, replace it with a new + // one. + if (triple.getOSName() == "amdopencl") { + if (triple.getArch() == llvm::Triple::amdil || + triple.getArch() == llvm::Triple::amdil64) { + triple.setOS(llvm::Triple::UnknownOS); + } else { + llvm::Triple hostTriple(llvm::sys::getDefaultTargetTriple()); + triple.setOS(hostTriple.getOS()); + } + + triple.setEnvironment(llvm::Triple::AMDOpenCL); + module->setTargetTriple(triple.str()); + } +} + +//Modify module for targets before linking. +//Report error by buildLog. +//Return false on error. +static bool fixUpModule(llvm::Module *M, + llvm::StringRef TargetTriple, + llvm::StringRef TargetLayout, + bool RunSPIRLoader, + bool DemangleBuiltins, + bool RunEDGAdapter, + bool SetSPIRCallingConv, + bool RunX86Adpater, + bool RunPrintfRuntimeBinding, + bool RunPrintfCpuLowering, + bool RunLowerEnqueueKernel, + const amd::option::OptionVariables *oVariables, + std::string& buildLog) { + llvm::PassManager Passes; + + DEBUG_WITH_TYPE("linkTriple", llvm::dbgs() << + "[fixUpModule] module triple: " << M->getTargetTriple() << + " target triple: " << TargetTriple); + llvm::Triple triple(M->getTargetTriple()); +#if OPENCL_MAJOR < 2 + if (triple.getArch() == llvm::Triple::spir || + triple.getArch() == llvm::Triple::spir64 || + triple.getArch() == llvm::Triple::x86 || + triple.getArch() == llvm::Triple::x86_64 || + M->getTargetTriple().empty()) +#endif + { + M->setTargetTriple(TargetTriple); + M->setDataLayout(TargetLayout); + } +#if OPENCL_MAJOR < 2 + if (M->getTargetTriple() != TargetTriple) { + //ToDo: There is bug 9996 in compiler library about converting BIF30 to BIF21 + //which causes regressions in ocltst if the following check is enabled. + //Fix the bugs then enable the following check + #if 0 + assert(0 && "Inconsistent module and library target"); + buildLog += "Internal Error: failed to link modules correctly.\n"; + return false; + #else + LogWarning("Inconsistent module and library target"); + return true; + #endif + } +#endif + + Passes.add(new llvm::DataLayout(M)); + + if (RunPrintfRuntimeBinding == true || RunPrintfCpuLowering == true) + Passes.add(llvm::createAMDPrintfRuntimeBinding(RunPrintfCpuLowering)); + + if (oVariables->LowerAtomics) + Passes.add(llvm::createAMDLowerAtomicsPass()); + + if (oVariables->LowerPipeBuiltins) + Passes.add(llvm::createAMDLowerPipeBuiltinsPass()); + + if (RunEDGAdapter) { + assert(!RunSPIRLoader); + Passes.add(llvm::createAMDEDGToIA64TranslatorPass(SetSPIRCallingConv)); + } + + if (RunSPIRLoader) { + assert(!RunEDGAdapter); + Passes.add(llvm::createSPIRLoader(DemangleBuiltins)); + } + + if (RunX86Adpater) { + // One of them should run before the AMDX86Adapter Pass. + assert(RunSPIRLoader || RunEDGAdapter); + Passes.add(llvm::createAMDX86AdapterPass()); + } + + if (RunLowerEnqueueKernel) { + Passes.add(llvm::createAMDLowerEnqueueKernelPass()); + Passes.add(llvm::createAMDGenerateDevEnqMetadataPass()); + } + + Passes.run(*M); + return true; +} + +static void CheckSPIRVersion(const llvm::Module *M, + const aclTargetInfo& Target) { + const llvm::NamedMDNode *SPIRVersion + = M->getNamedMetadata("opencl.spir.version"); + assert(SPIRVersion); + // When multiple llvm modules are linked together to create a single module + // Metadata's of llvm modules are added into destination module and + // it results in a more than one SPIR MDNode value. + // Marking this fix as temporary and it will be tracked in bugzilla id 9775 + if (SPIRVersion->getNumOperands() > 1) + LogWarning("\nIncorrect SPIR MDNode value"); + assert(SPIRVersion->getNumOperands() >= 1); + + const llvm::MDNode *VersionMD = SPIRVersion->getOperand(0); + assert(VersionMD->getNumOperands() == 2); + + const llvm::ConstantInt *CMajor + = llvm::cast(VersionMD->getOperand(0)); + assert(CMajor->getType()->getIntegerBitWidth() == 32); + + unsigned VersionMajor = CMajor->getZExtValue(); + switch (VersionMajor) { + case 1: + break; + case 2: + assert(!isAMDILTarget(Target)); + break; + default: + llvm_unreachable("Unknown SPIR version"); + break; + } +} + +// On 64 bit device, aclBinary target is set to 64 bit by default. When 32 bit +// LLVM or SPIR binary is loaded, aclBinary target needs to be modified to +// match LLVM or SPIR bitness. +// Returns false on error. +static bool +checkAndFixAclBinaryTarget(llvm::Module* module, aclBinary* elf, + std::string& buildLog) { + if (module->getTargetTriple().empty()) { + LogWarning("Module has no target triple"); + return true; + } + + llvm::Triple triple(module->getTargetTriple()); + const char* newArch = NULL; + if (elf->target.arch_id == aclAMDIL64 && + (triple.getArch() == llvm::Triple::amdil || + triple.getArch() == llvm::Triple::spir)) + newArch = "amdil"; + else if (elf->target.arch_id == aclX64 && + (triple.getArch() == llvm::Triple::x86 || + triple.getArch() == llvm::Triple::spir)) + newArch = "x86"; + else if (elf->target.arch_id == aclHSAIL64 && + (triple.getArch() == llvm::Triple::hsail || + triple.getArch() == llvm::Triple::spir)) + newArch = "hsail"; + if (newArch != NULL) { + acl_error errorCode; + elf->target = aclGetTargetInfo(newArch, aclGetChip(elf->target), + &errorCode); + if (errorCode != ACL_SUCCESS) { + assert(0 && "Invalid arch id or chip id in elf target"); + buildLog += "Internal Error: failed to link modules correctlty.\n"; + return false; + } + } + + reinterpret_cast(elf->options)->libraryType_ = + getLibraryType(&elf->target); + + // Check consistency between module triple and aclBinary target + if (elf->target.arch_id == aclAMDIL64 && + (triple.getArch() == llvm::Triple::amdil64 || + triple.getArch() == llvm::Triple::spir64)) + return true; + if (elf->target.arch_id == aclAMDIL && + (triple.getArch() == llvm::Triple::amdil || + triple.getArch() == llvm::Triple::spir)) + return true; + if (elf->target.arch_id == aclHSAIL64 && + (triple.getArch() == llvm::Triple::hsail_64 || + triple.getArch() == llvm::Triple::spir64)) + return true; + if (elf->target.arch_id == aclHSAIL && + (triple.getArch() == llvm::Triple::hsail || + triple.getArch() == llvm::Triple::spir)) + return true; + if (elf->target.arch_id == aclX64 && + (triple.getArch() == llvm::Triple::x86_64 || + triple.getArch() == llvm::Triple::spir64)) + return true; + if (elf->target.arch_id == aclX86 && + (triple.getArch() == llvm::Triple::x86 || + triple.getArch() == llvm::Triple::spir)) + return true; + DEBUG_WITH_TYPE("linkTriple", llvm::dbgs() << + "[checkAndFixAclBinaryTarget] " << + " aclBinary target: " << elf->target.arch_id << + " chipId: " << elf->target.chip_id << + " module triple: " << module->getTargetTriple() << + '\n'); + + //ToDo: There is bug 9996 in compiler library about converting BIF30 to BIF21 + //which causes regressions in ocltst if the following check is enabled. + //Fix the bugs then enable the following check +#if 0 + assert(0 && "Inconsistent LLVM target and elf target"); + buildLog += "Internal Error: failed to link modules correctlty.\n"; + return false; +#else + LogWarning("Inconsistent LLVM target and elf target"); + return true; +#endif +} + int +amdcl::OCLLinker::link(llvm::Module* input, std::vector &libs) +{ + bool IsGPUTarget = isGpuTarget(Elf()->target); + uint64_t start_time = 0ULL, time_link = 0ULL, time_prelinkopt = 0ULL; + if (Options()->oVariables->EnableBuildTiming) { + start_time = amd::Os::timeNanos(); + } + + fixupOldTriple(input); + + if (!checkAndFixAclBinaryTarget(input, Elf(), BuildLog())) + return 1; + + int ret = 0; + if (Options()->oVariables->UseJIT) { + hookup_.amdrtFunctions = amd::getAmdRtFunctions(); + } else { + hookup_.amdrtFunctions = NULL; + } + if (Options()->isOptionSeen(amd::option::OID_LUThreshold) || !IsGPUTarget) { + setUnrollScratchThreshold(Options()->oVariables->LUThreshold); + } else { + setUnrollScratchThreshold(500); + } + setGPU(IsGPUTarget); + + setPreLinkOpt(false); + + // We are doing whole program optimization + setWholeProgram(true); + + llvmbinary_ = input; + + if ( !LLVMBinary() ) { + BuildLog() += "Internal Error: cannot load bc application for linking\n"; + return 1; + } + + if (linkLLVMModules(libs)) { + BuildLog() += "Internal Error: failed to link modules correctlty.\n"; + return 1; + } + + // Don't link in built-in libraries if we are only creating the library. + if (Options()->oVariables->clCreateLibrary) { + return 0; + } + + if (Options()->isDumpFlagSet(amd::option::DUMP_BC_ORIGINAL)) { + std::string MyErrorInfo; + std::string fileName = Options()->getDumpFileName("_original.bc"); + llvm::raw_fd_ostream outs(fileName.c_str(), MyErrorInfo, llvm::raw_fd_ostream::F_Binary); + if (MyErrorInfo.empty()) + WriteBitcodeToFile(LLVMBinary(), outs); + else + printf(MyErrorInfo.c_str()); + } + std::vector LibMs; + + // The AMDIL GPU libraries include 32 bit specific, 64 bit specific and common + // libraries. The common libraries do not have target triple. A search is + // performed to find the first library containing non-empty target triple + // and use it for translating SPIR. + amd::LibraryDescriptor LibDescs[ + amd::LibraryDescriptor::MAX_NUM_LIBRARY_DESCS]; + int sz; + std::string LibTargetTriple; + std::string LibDataLayout; + if (amd::getLibDescs(Options()->libraryType_, LibDescs, sz) != 0) { + // FIXME: If we error here, we don't clean up, so we crash in debug build + // on compilerfini(). + BuildLog() += "Internal Error: finding libraries failed!\n"; + return 1; + } + for (int i=0; i < sz; i++) { + llvm::MemoryBuffer* Buffer = 0; + llvm::Module* Library = amd::LoadLibrary(LibDescs[i].start, LibDescs[i].size, Context(), &Buffer); + DEBUG(llvm::dbgs() << "Loaded library " << i << "\n"); + if ( !Library ) { + BuildLog() += "Internal Error: cannot load library!\n"; + delete LLVMBinary(); + for (int j = 0; j < i; ++j) { + delete LibMs[j]; + } + LibMs.clear(); + return 1; +#ifndef NDEBUG + } else { + if ( llvm::verifyModule( *Library ) ) { + BuildLog() += "Internal Error: library verification failed!\n"; + exit(1); + } +#endif + } + DEBUG_WITH_TYPE("linkTriple", llvm::dbgs() << "Library[" << i << "] " << + Library->getTargetTriple() << ' ' << Library->getDataLayout() << '\n'); + // Find the first library whose target triple is not empty. + if (LibTargetTriple.empty() && !Library->getTargetTriple().empty()) { + LibTargetTriple = Library->getTargetTriple(); + LibDataLayout = Library->getDataLayout(); + } + LibMs.push_back(Library); + } + + // Check consistency of target and data layout + assert (!LibTargetTriple.empty() && "At least one library should have triple"); +#ifndef NDEBUG + for (size_t i = 0, e = LibMs.size(); i < e; ++i) { + if (LibMs[i]->getTargetTriple().empty()) + continue; + assert (LibMs[i]->getTargetTriple() == LibTargetTriple && + "Library target triple should match"); + assert (LibMs[i]->getDataLayout() == LibDataLayout && + "Library data layout should match"); + } +#endif + + + // Under various situations, the LLVM dialect used in the kernel + // module does not match the dialect used in the builtin library. We + // need to fix-up the kernel module to eliminate this mismatch. + // + // SPIRLoader is required to consume a SPIR kernel: + // SPIR 1.2 on all targets. + // SPIR 2.0 on x86 and HSAIL only. + // + // The AMDIL libary is compiled by EDG, and hence it does not use + // the SPIR mangling scheme. To allow a SPIR 1.2 kernel to link with + // this library, the SPIRLoader must fix the mangling in the kernel. + // + // EDGAdapter is required to consume a non-SPIR (EDG) kernel on x86 + // and HSAIL targets. The builtins library for these targets are + // built by Clang, but OpenCL 1.2 kernels are compiled by EDG. + // + // A non-SPIR kernel module is not expected on the HSAIL target in a + // normal OpenCL 2.0 build. We should actually flag an error if this + // occurs, but we let it through to facilitate custom builds created + // to test this combination. In this situation, the EDGAdapter must + // additionally set the calling conventions correctly, because the + // HSAIL library is in SPIR format. + // + // RunX86Adpater is required to run only on the CPU path. It is + // expected to the solve the link issues between the user kernel + // (SPIR/EDG) vs. Clang compiled x86 builtins library. + + // Enabled for: + bool RunSPIRLoader = false; // SPIR -> x86/HSAIL/AMDIL + bool DemangleBuiltins = false; // SPIR -> AMDIL + bool RunEDGAdapter = false; // EDG -> x86/HSAIL + bool SetSPIRCallingConv = false; // EDG -> HSAIL + bool RunX86Adapter = false; // SPIR/EDG -> x86 + bool RunLowerEnqueueKernel = false; + bool RunPrintfRuntimeBinding = false; + bool RunPrintfCpuLowering = false; + bool LowerToPreciseFunctions = false; + + llvm::Triple ModuleTriple(LLVMBinary()->getTargetTriple()); + + bool isSPIRModuleTriple = ((ModuleTriple.getArch() == llvm::Triple::spir) || + (ModuleTriple.getArch() == llvm::Triple::spir64)); + + if(isSPIRModuleTriple) { + + CheckSPIRVersion(LLVMBinary(), Elf()->target); + RunSPIRLoader = true; +#if OPENCL_MAJOR >= 2 // this will become default + DemangleBuiltins |= isAMDILTarget(Elf()->target); +#ifdef BUILD_HSA_TARGET // special case for HSA build + DemangleBuiltins |= isHSAILTarget(Elf()->target); +#endif + // Never demangle for x86 target on 200 build. +#else // OpenCL 1.2 build (this will go away) + DemangleBuiltins = true; +#endif + } else { +#if OPENCL_MAJOR >= 2 + // Decide if we need to adapt the non-SPIR (EDG) kernel module. + // + // FIXME: Remove the #ifdef when x86 and HSAIL libraries are + // always built by Clang. +#ifndef BUILD_HSA_TARGET + // Run the adapter for HSAIL, only if this is an ORCA build! + // + // On an HSA build, the HSAIL library is always built with EDG. + // This assumption must match the settings in + // "opencl/library/hsa/hsail/build/Makefile.hsail" + RunEDGAdapter |= isHSAILTarget(Elf()->target); +#endif + // HSAIL requires SPIR calling conventions since the library is in + // SPIR format. This doesn't matter if the EDGAdapter is not run. + SetSPIRCallingConv = isHSAILTarget(Elf()->target); + + // Run the EDG Adapter if OPENCL_MAJOR >= 2 and for x86 target. + RunEDGAdapter |= isCpuTarget(Elf()->target); +#endif // OPENCL_MAJOR >= 2 + } + +// It should run for both EDG generated LLVM IR and SPIR for x86 path. +// FIXME: Remove the #ifdef when x86 is always built by Clang on +// OpenCL 1.2 builds. +#if OPENCL_MAJOR >=2 + RunX86Adapter = isCpuTarget(Elf()->target); + RunLowerEnqueueKernel = isSPIRModuleTriple; + // For HSAIL targets, when the option -cl-fp32-correctly-rounded-divide-sqrt + // lower divide and sqrt functions to precise HSAIL builtin library functions. + LowerToPreciseFunctions = (isHSAILTarget(Elf()->target) + && Options()->oVariables->FP32RoundDivideSqrt); +#endif + if (strcmp(Options()->oVariables->CLStd, "CL2.0") == 0) { + if (isHSAILTarget(Elf()->target)) { + RunPrintfRuntimeBinding = true; + } else if (isCpuTarget(Elf()->target)) { + RunPrintfCpuLowering = true; + } + } + + // The first member in the list of libraries is assumed to be + // representative of the target device. + if(!fixUpModule(LLVMBinary(), LibTargetTriple, LibDataLayout, + RunSPIRLoader, DemangleBuiltins, + RunEDGAdapter, SetSPIRCallingConv, + RunX86Adapter, RunPrintfRuntimeBinding, RunPrintfCpuLowering, + RunLowerEnqueueKernel, Options()->oVariables, + BuildLog())) + return 1; + + // Before doing anything else, quickly optimize Module + if (Options()->oVariables->OptLevel) { + if (Options()->oVariables->EnableBuildTiming) { + time_prelinkopt = amd::Os::timeNanos(); + } + + AMDPrelinkOpt(LLVMBinary(), true /*Whole*/, + !Options()->oVariables->OptSimplifyLibCall, + Options()->oVariables->UnsafeMathOpt, + Options()->oVariables->OptUseNative, + LowerToPreciseFunctions); + + if (Options()->oVariables->EnableBuildTiming) { + time_prelinkopt = amd::Os::timeNanos() - time_prelinkopt; + } + } + // Now, do linking by extracting from the builtins library only those + // functions that are used in the kernel(s). + if (Options()->oVariables->EnableBuildTiming) { + time_link = amd::Os::timeNanos(); + } + + std::string ErrorMessage; + + // CL pre-link processing + llvm::Module *clp_inputs[2]; + clp_inputs[0] = LLVMBinary(); + clp_inputs[1] = NULL; + std::string clp_errmsg; + llvm::Module *OnFlyLib = clpVectorExpansion (clp_inputs, clp_errmsg); + if (clp_errmsg.empty() == false) { + delete LLVMBinary(); + for (unsigned int i = 0; i < LibMs.size(); ++ i) { + delete LibMs[i]; + } + LibMs.clear(); + BuildLog() += clp_errmsg; + BuildLog() += "Internal Error: on-fly library generation failed\n"; + return 1; + } + + unsigned int offset = (unsigned int)LibMs.size(); + + if (OnFlyLib) { + // OnFlyLib must be the last! + LibMs.push_back(OnFlyLib); + } + + // build the reference map + llvm::ReferenceMapBuilder RefMapBuilder(LLVMBinary(), LibMs); + + RefMapBuilder.InitReferenceMap(); + + if (IsGPUTarget && RefMapBuilder.isInExternFuncs("printf")) { + DEBUG(llvm::dbgs() << "Adding printf funs:\n"); + // The following functions need forcing as printf-conversion happens + // after this link stage + static const char* forcedRefs[] = { + "___initDumpBuf", + "___dumpBytes_v1b8", + "___dumpBytes_v1b16", + "___dumpBytes_v1b32", + "___dumpBytes_v1b64", + "___dumpBytes_v1b128", + "___dumpBytes_v1b256", + "___dumpBytes_v1b512", + "___dumpBytes_v1b1024", + "___dumpBytes_v1bs", + "___dumpStringID" + }; + RefMapBuilder.AddForcedReferences(forcedRefs, + sizeof(forcedRefs)/sizeof(forcedRefs[0])); + } + if (!IsGPUTarget && Options()->oVariables->UseJIT) { + RefMapBuilder.AddForcedReferences(amd::amdRTFuns, + sizeof(amd::amdRTFuns)/sizeof(amd::amdRTFuns[0])); + } + + RefMapBuilder.AddReferences(); + + // inject an llvm function that returns the mask of several compile + // options, which are used by the built-in library + const std::list& ExternFuncs + = RefMapBuilder.getExternFunctions(); + const std::list::const_iterator it + = std::find(ExternFuncs.begin(), ExternFuncs.end(), OptionMaskFName); + if (it != ExternFuncs.end()) { + createOptionMaskFunction(LLVMBinary()); + } + + createASICIDFunctions(LLVMBinary()); + + if (!isHSAILTarget(Elf()->target)) { + // Add NoInline attribute to user functions + llvm::StringRef family(aclGetFamily(Elf()->target)); + llvm::StringRef chip(aclGetChip(Elf()->target)); + + // Add NoInline attribute to library functions so that they + // can be considered for not inlining in codegen. + if (IsGPUTarget && + (Options()->oVariables->OptMem2reg || Options()->oVariables->DebugCall) && + !Options()->oVariables->clInternalKernel && + !(family == "NI" || family == "Evergreen" || family == "Sumo" || + family == "TN")) { + if (Options()->oVariables->AddUserNoInline) + amd::AddNoInlineAttr(LLVMBinary()); + if (Options()->oVariables->AddLibNoInline) + for (unsigned int i=0; i < LibMs.size(); i++) + amd::AddNoInlineAttr(LibMs[i]); + } + + // Disable outline macro for mem2reg=0 unless -fdebug-call + // is on. + if (!Options()->oVariables->OptMem2reg && !Options()->oVariables->DebugCall) { + Options()->oVariables->UseMacroForCall = false; + } + } + + // Link libraries to get every functions that are referenced. + std::string ErrorMsg; + if (resolveLink(LLVMBinary(), LibMs, RefMapBuilder.getModuleRefMaps(), + &ErrorMsg)) { + BuildLog() += ErrorMsg; + BuildLog() += "\nInternal Error: linking libraries failed!\n"; + return 1; + } + LibMs.clear(); + + + if (Options()->oVariables->EnableBuildTiming) { + time_link = amd::Os::timeNanos() - time_link; + std::stringstream tmp_ss; + tmp_ss << " LLVM time (link+opt): " + << (amd::Os::timeNanos() - start_time)/1000ULL + << " us\n" + << " prelinkopt: " << time_prelinkopt/1000ULL << " us\n" + << " link: " << time_link/1000ULL << " us\n" + ; + appendLogToCL(CL(), tmp_ss.str()); + } + + if (!isHSAILTarget(Elf()->target)) { + // Refine NoInline attribute of functions + if (IsGPUTarget && !Options()->oVariables->clInternalKernel) { + amd::RefineNoInlineAttr(LLVMBinary(), + Options()->oVariables->InlineCostThreshold, + Options()->oVariables->InlineSizeThreshold, + Options()->oVariables->InlineKernelSizeThreshold, + Options()->oVariables->AllowMultiLevelCall && + Options()->oVariables->UseMacroForCall ); + } + } + + if (Options()->isDumpFlagSet(amd::option::DUMP_BC_LINKED)) { + std::string MyErrorInfo; + std::string fileName = Options()->getDumpFileName("_linked.bc"); + llvm::raw_fd_ostream outs(fileName.c_str(), MyErrorInfo, llvm::raw_fd_ostream::F_Binary); + // FIXME: Need to add this to the elf binary! + if (MyErrorInfo.empty()) + WriteBitcodeToFile(LLVMBinary(), outs); + else + printf(MyErrorInfo.c_str()); + } + + // Check if kernels containing local arrays are called by other kernels. + std::string localArrayUsageError; + if (!llvm::AMDCheckLocalArrayUsage(*LLVMBinary(), &localArrayUsageError)) { + BuildLog() += "Error: " + localArrayUsageError + '\n'; + return 1; + } + + return 0; +} diff --git a/rocclr/compiler/lib/backends/common/linker.hpp b/rocclr/compiler/lib/backends/common/linker.hpp new file mode 100644 index 0000000000..6f8a8a647d --- /dev/null +++ b/rocclr/compiler/lib/backends/common/linker.hpp @@ -0,0 +1,103 @@ +// +// Copyright (c) 2008 Advanced Micro Devices, Inc. All rights reserved. +// +#ifndef _BE_LINKER_HPP_ +#define _BE_LINKER_HPP_ +#include "compiler_stage.hpp" +#include "aclTypes.h" +#include +#include + + +namespace llvm { + class Module; + class Value; +}; // namespace llvm + +namespace amdcl +{ + /*! \addtogroup Compiler Library + * + * \copydoc amdcl::Linker + * + * @{ + */ + class Linker : public LLVMCompilerStage{ + Linker(Linker&); // DO NOT IMPLEMENT. + Linker(); // DO NOT IMPLEMENT. + + public: + Linker(aclCompiler *cl, aclBinary* elf, aclLogFunction log) + : LLVMCompilerStage(cl, elf, log) {} + + virtual ~Linker() {} + + + + /*! Function that takes as in a llvm::Module that contains LLVM-IR + * binary and links in a vector of libraries. + * Returns 0 on success, non-zero on failure. + */ + virtual int link(llvm::Module* input, std::vector &libs) = 0; + + }; // class Linker + /*@}*/ + + /*! \addtogroup Compiler Library + * + * \copydoc amdcl::OCLLinker + * + * @{ + * \brief Linker that is unique to OpenCL. + */ + class OCLLinker : public Linker { + enum { + MASK_NO_SIGNED_ZEROES = 0x1, + MASK_UNSAFE_MATH_OPTIMIZATIONS = 0x2, + MASK_FINITE_MATH_ONLY = 0x4, + MASK_FAST_RELAXED_MATH = 0x8, + MASK_UNIFORM_WORK_GROUP_SIZE = 0x10 + }; + + public: + OCLLinker(aclCompiler* cl, aclBinary* bin, aclLogFunction log) + : Linker(cl, bin, log) {} + + virtual ~OCLLinker() { + for (unsigned j = 0, i = (unsigned)mathLibs_.size(); j < i; ++j) { + if (mathLibs_[j]) { + delete mathLibs_[j]; + } + } + }; + void setPreLinkOpt(bool Val) { hookup_.amdoptions.IsPreLinkOpt = Val; } + void setUnrollScratchThreshold(uint32_t ust) { hookup_.amdoptions.UnrollScratchThreshold = ust; } + + bool getWholeProgram() { return hookup_.amdoptions.WholeProgram; } + uint32_t getUnrollScratchThreshold() { return hookup_.amdoptions.UnrollScratchThreshold; } + + + /*! Function that takes as input a std::string which + * contains LLVM-IR binary and links in a vector of libraries. + * This version also links in the OpenCL math libraries along with + * the list of libraries that are passed in. + */ + int link(llvm::Module* input, std::vector &libs); + protected: + void createOptionMaskFunction(llvm::Module* module); + void createASICIDFunctions(llvm::Module* module); + bool linkLLVMModules(std::vector &libs); + bool linkWithModule(llvm::Module* Dst, llvm::Module* Src, + std::map *ModuleRefMap); + + + private: + static void fixupOldTriple(llvm::Module* module); + /*! Vector of modules that stores the math libraries. + */ + std::vector mathLibs_; + }; // class OCLLinker + /*@}*/ + +}; // namespace amdcl +#endif // _BE_LINKER_HPP_ diff --git a/rocclr/compiler/lib/backends/common/opt_level.cpp b/rocclr/compiler/lib/backends/common/opt_level.cpp new file mode 100644 index 0000000000..51456c2268 --- /dev/null +++ b/rocclr/compiler/lib/backends/common/opt_level.cpp @@ -0,0 +1,186 @@ +// +// Copyright (c) 2008 Advanced Micro Devices, Inc. All rights reserved. +// +#include "top.hpp" +#include "opt_level.hpp" +#include "library.hpp" +#include "utils/options.hpp" +#include "llvm/Module.h" +#include "llvm/Analysis/Passes.h" +#include "llvm/DataLayout.h" +#include "llvm/Transforms/IPO/PassManagerBuilder.h" +#include "llvm/LinkAllPasses.h" +#include "llvm/Transforms/IPO/AMDOptOptions.h" +#include "compiler_stage.hpp" +using namespace amdcl; +using namespace llvm; + +void +OptLevel::setup(bool isGPU, uint32_t OptLevel) +{ + // Add an appropriate DataLayout instance for this module. + Passes().add(new DataLayout(module_)); + fpasses_ = new FunctionPassManager(module_); + fpasses_->add(new DataLayout(module_)); + + PassManagerBuilder Builder; + Builder.OptLevel = OptLevel; + + if (Options()->libraryType_ == amd::GPU_Library_HSAIL) { + if (OptLevel == 0) return; + } + + if (!Options()->oVariables->Inline) { + // No inlining pass + } else if (isGPU) { +#ifdef WITH_TARGET_HSAIL + if (Options()->libraryType_ == amd::GPU_Library_HSAIL) { + if (HLC_Experimental_Enable_Calls) { + HLC_Disable_Amd_Inline_All = true; + } + // Always create Inliner regardless of OptLevel + if (HLC_Force_Always_Inliner_Pass) { + Builder.Inliner = createAlwaysInlinerPass(); + } else { + Builder.Inliner = createFunctionInliningPass(500); + } + } else +#endif + { + // Always create Inliner regardless of OptLevel + Builder.Inliner = createFunctionInliningPass(500); + } + } else if (OptLevel > 1) { + unsigned Threshold = 225; + if (OptLevel > 2) + Threshold = 275; +#ifdef WITH_TARGET_HSAIL + if (Options()->libraryType_ == amd::GPU_Library_HSAIL) { + // Don't do inlining (including createAlwaysInlinerPass()) if OptimizationLevel + // is zero becaue we are generating code for -g + if (OptLevel > 0) { + Builder.Inliner = createFunctionInliningPass(Threshold); + } + } else +#endif + { + Builder.Inliner = createFunctionInliningPass(Threshold); + } + } + Builder.SizeLevel = 0; + Builder.DisableUnitAtATime = false; + Builder.DisableUnrollLoops = OptLevel == 0; + if (Options()->libraryType_ != amd::GPU_Library_HSAIL) + Builder.DisableSimplifyLibCalls = true; + Builder.AMDpopulateFunctionPassManager(*fpasses_, &module_->getContext()); + Builder.AMDpopulateModulePassManager(passes_, &module_->getContext(), module_); +} + +void +OptLevel::run() +{ + if (Options()->oVariables->OptPrintLiveness) { + Passes().add(createAMDLivenessPrinterPass()); + } + fpasses_->doInitialization(); + for (Module::iterator I = module_->begin(), E = module_->end(); I != E; ++I) + fpasses_->run(*I); + fpasses_->doFinalization(); + // Now that we have all of the passes ready, run them. + passes_.run(*module_); + + delete fpasses_; +} + +int +O0OptLevel::optimize(Module *input, bool isGPU) +{ + // With -O0, we don't do anything + module_ = input; +#ifdef WITH_TARGET_HSAIL + if (Options()->libraryType_ == amd::GPU_Library_HSAIL) { + // Mark all non-kernel functions as having internal linkage + Passes().add(createAMDSymbolLinkagePass(true, NULL)); + } else +#endif + { + setup(false, 0); + run(); + } + return 0; +} + +int +GPUO0OptLevel::optimize(Module *input, bool isGPU) +{ + module_ = input; + assert(isGPU && "Only a GPU can use GPUO0OptLevel!\n"); + setup(true, 0); +#ifdef WITH_TARGET_HSAIL + if (Options()->libraryType_ == amd::GPU_Library_HSAIL) { + // On the GPU, even with -O0, we must do some optimizations. One + // goal is to ensure that all functions are inlined. This requires + // three steps in that order: + // + // 1. Mark all non-kernel functions as having internal linkage. + // 2. Invoke the GlobalOptimizer to resolve function aliases. + // 3. Force inlining using our custom inliner pass. + Passes().add(createAMDSymbolLinkagePass(true, NULL)); + Passes().add(createGlobalOptimizerPass()); + if (!HLC_Disable_Amd_Inline_All && !DisableInline ) { + if (HLC_Force_Always_Inliner_Pass) { + Passes().add(createAlwaysInlinerPass()); + } else { + Passes().add(createAMDInlineAllPass(true)); + } + } + } +#endif + run(); + return 0; +} + +int +O1OptLevel::optimize(Module *input, bool isGPU) +{ + module_ = input; + setup(isGPU, 1); + run(); + return 0; +} + +int +O2OptLevel::optimize(Module *input, bool isGPU) +{ + module_ = input; + setup(isGPU, 2); + run(); + return 0; +} + +int +O3OptLevel::optimize(Module *input, bool isGPU) +{ + module_ = input; + setup(isGPU, 3); + run(); + return 0; +} + +int +O4OptLevel::optimize(Module *input, bool isGPU) +{ + module_ = input; + setup(isGPU, 4); + run(); + return 0; +} + +int +OsOptLevel::optimize(Module *input, bool isGPU) +{ + module_ = input; + setup(isGPU, 5); + run(); + return 0; +} diff --git a/rocclr/compiler/lib/backends/common/opt_level.hpp b/rocclr/compiler/lib/backends/common/opt_level.hpp new file mode 100644 index 0000000000..87dfc90b0c --- /dev/null +++ b/rocclr/compiler/lib/backends/common/opt_level.hpp @@ -0,0 +1,188 @@ +// +// Copyright (c) 2008 Advanced Micro Devices, Inc. All rights reserved. +// +#ifndef _BE_OPT_LEVEL_HPP_ +#define _BE_OPT_LEVEL_HPP_ +#include "top.hpp" +#include "utils/options.hpp" +#include "llvm/PassManager.h" +#include "llvm/Analysis/Passes.h" +namespace llvm { + class Module; + class FunctionPassManager; +}; // llvm namespace +namespace amdcl +{ + /*! \addtogroup Compiler Library + * + * \copydoc amdcl::OptLevel + * + * @{ + */ + class OptLevel { + OptLevel(OptLevel&); // DO NOT IMPLEMENT. + OptLevel(); // DO NOT IMPLEMENT. + + public: + OptLevel(amd::option::Options *OptionsObj) + : opts_(OptionsObj) {} + + virtual ~OptLevel() {} + + virtual int optimize(llvm::Module *input, bool isGPU) = 0; + protected: + void setup(bool isGPU, uint32_t OptLevel); + void run(); + llvm::PassManager& Passes() { return passes_; } + llvm::FunctionPassManager& FPasses() { return (*fpasses_); } + amd::option::Options* Options() { return opts_; } + llvm::Module* module_; + private: + llvm::FunctionPassManager *fpasses_; + llvm::PassManager passes_; + amd::option::Options *opts_; + }; // class OptLevel + /*@}*/ + + /*! \addtogroup Compiler Library + * + * \copydoc amdcl::O0OptLevel + * + * @{ + */ + class O0OptLevel : public OptLevel { + O0OptLevel(O0OptLevel&); // DO NOT IMPLEMENT. + O0OptLevel(); // DO NOT IMPLEMENT. + + public: + O0OptLevel(amd::option::Options *opts) + : OptLevel(opts) {} + + virtual ~O0OptLevel() {} + + virtual int optimize(llvm::Module *input, bool isGPU); + }; // class O0OptLevel + /*@}*/ + + /*! \addtogroup Compiler Library + * + * \copydoc amdcl::GPUO0OptLevel + * + * @{ + */ + class GPUO0OptLevel : public O0OptLevel { + GPUO0OptLevel(GPUO0OptLevel&); // DO NOT IMPLEMENT. + GPUO0OptLevel(); // DO NOT IMPLEMENT. + + public: + GPUO0OptLevel(amd::option::Options *opts) + : O0OptLevel(opts) {} + + virtual ~GPUO0OptLevel() {} + + virtual int optimize(llvm::Module *input, bool isGPU); + }; // class O0OptLevel + /*@}*/ + + /*! \addtogroup Compiler Library + * + * \copydoc amdcl::O1OptLevel + * + * @{ + */ + class O1OptLevel : public OptLevel { + O1OptLevel(O1OptLevel&); // DO NOT IMPLEMENT. + O1OptLevel(); // DO NOT IMPLEMENT. + + public: + O1OptLevel(amd::option::Options *opts) + : OptLevel(opts) {} + + virtual ~O1OptLevel() {} + + virtual int optimize(llvm::Module *input, bool isGPU); + }; // class O1OptLevel + /*@}*/ + + /*! \addtogroup Compiler Library + * + * \copydoc amdcl::O2OptLevel + * + * @{ + */ + class O2OptLevel : public OptLevel { + O2OptLevel(O2OptLevel&); // DO NOT IMPLEMENT. + O2OptLevel(); // DO NOT IMPLEMENT. + + public: + O2OptLevel(amd::option::Options *opts) + : OptLevel(opts) {} + + virtual ~O2OptLevel() {} + + virtual int optimize(llvm::Module *input, bool isGPU); + }; // class O2OptLevel + /*@}*/ + + /*! \addtogroup Compiler Library + * + * \copydoc amdcl::O3OptLevel + * + * @{ + */ + class O3OptLevel : public OptLevel { + O3OptLevel(O3OptLevel&); // DO NOT IMPLEMENT. + O3OptLevel(); // DO NOT IMPLEMENT. + + public: + O3OptLevel(amd::option::Options *opts) + : OptLevel(opts) {} + + virtual ~O3OptLevel() {} + + virtual int optimize(llvm::Module *input, bool isGPU); + }; // class O3OptLevel + /*@}*/ + + /*! \addtogroup Compiler Library + * + * \copydoc amdcl::O4OptLevel + * + * @{ + */ + class O4OptLevel : public OptLevel { + O4OptLevel(O4OptLevel&); // DO NOT IMPLEMENT. + O4OptLevel(); // DO NOT IMPLEMENT. + + public: + O4OptLevel(amd::option::Options *opts) + : OptLevel(opts) {} + + virtual ~O4OptLevel() {} + + virtual int optimize(llvm::Module *input, bool isGPU); + }; // class O4OptLevel + /*@}*/ + + /*! \addtogroup Compiler Library + * + * \copydoc amdcl::OsOptLevel + * + * @{ + */ + class OsOptLevel : public OptLevel { + OsOptLevel(OsOptLevel&); // DO NOT IMPLEMENT. + OsOptLevel(); // DO NOT IMPLEMENT. + + public: + OsOptLevel(amd::option::Options *opts) + : OptLevel(opts) {} + + virtual ~OsOptLevel() {} + + virtual int optimize(llvm::Module *input, bool isGPU); + }; // class OsOptLevel + /*@}*/ + +}; // amdcl namespace +#endif // _BE_OPT_LEVEL_HPP_ diff --git a/rocclr/compiler/lib/backends/common/optimizer.cpp b/rocclr/compiler/lib/backends/common/optimizer.cpp new file mode 100644 index 0000000000..e0c8150362 --- /dev/null +++ b/rocclr/compiler/lib/backends/common/optimizer.cpp @@ -0,0 +1,152 @@ +// +// Copyright (c) 2008 Advanced Micro Devices, Inc. All rights reserved. +// +#include "top.hpp" +#include "optimizer.hpp" +#include "opt_level.hpp" +#include "os/os.hpp" +#include "utils/bif_section_labels.hpp" +#include "utils/libUtils.h" +#include "utils/options.hpp" + +#include "llvm/DataLayout.h" +#include "llvm/Analysis/Passes.h" +#include "llvm/Support/raw_ostream.h" +#include "llvm/LinkAllPasses.h" +#include +#include +using namespace amdcl; +using namespace llvm; + +static OptLevel* getOptLevel(amd::option::Options* Options, bool isGPU) { + switch(Options->oVariables->OptLevel) { + case amd::option::OPT_O0: + return (isGPU) ? new GPUO0OptLevel(Options) : new O0OptLevel(Options); + case amd::option::OPT_O1: + return new O1OptLevel(Options); + default: + assert(!"Found an invalid optimization level!"); + case amd::option::OPT_O2: + return new O2OptLevel(Options); + case amd::option::OPT_O3: + return new O3OptLevel(Options); + case amd::option::OPT_O4: + return new O4OptLevel(Options); + case amd::option::OPT_OS: + return new OsOptLevel(Options); + } + assert(!"Unreachable!"); + return NULL; +} +int +CPUOptimizer::preOptimizer(llvm::Module* M) +{ + llvm::PassManager Passes; + Passes.add(new llvm::DataLayout(M)); + + Passes.add(createAMDExportKernelNaturePass()); + + Passes.run(*M); + + return 0; +} + + int +CPUOptimizer::optimize(llvm::Module *input) +{ + if (!input) { + return 1; + } + int ret = 0; + uint64_t start_time = 0ULL, time_opt = 0ULL; + llvmbinary_ = input; + setWholeProgram(true); + + setGPU(false); + if (Options()->oVariables->EnableBuildTiming) { + start_time = amd::Os::timeNanos(); + } + ret = preOptimizer(LLVMBinary()); + OptLevel* cpuOpt = getOptLevel(Options(), false); + if (Options()->oVariables->EnableBuildTiming) { + time_opt = amd::Os::timeNanos(); + } + ret = cpuOpt->optimize(LLVMBinary(), false); + if (Options()->oVariables->EnableBuildTiming) { + time_opt = amd::Os::timeNanos() - time_opt; + std::stringstream tmp_ss; + tmp_ss << " LLVM Opt time: " + << time_opt/1000ULL + << "us\n"; + appendLogToCL(CL(), tmp_ss.str()); + } + delete cpuOpt; + + if ( ret ) { + BuildLog() += "Internal Error: optimizer failed!\n"; + return 1; + } + if (Options()->isDumpFlagSet(amd::option::DUMP_BC_OPTIMIZED)) { + std::string MyErrorInfo; + std::string fileName = Options()->getDumpFileName("_optimized.bc"); + raw_fd_ostream outs (fileName.c_str(), MyErrorInfo, raw_fd_ostream::F_Binary); + // FIXME: Need to add this to the elf binary! + if (MyErrorInfo.empty()) + WriteBitcodeToFile(LLVMBinary(), outs); + else + printf(MyErrorInfo.c_str()); + } + return ret; +} + + int +GPUOptimizer::optimize(llvm::Module *input) +{ + if (!input) { + return 1; + } + int ret = 0; + uint64_t start_time = 0ULL, time_opt = 0ULL; + llvmbinary_ = input; + + setGPU(true); + setWholeProgram(true); +#ifdef WITH_TARGET_HSAIL + if (isHSAILTarget(Elf()->target)) { + if (Options()->NumAvailGPRs == -1) + Options()->NumAvailGPRs = 128; // Default HSAIL number of GPRs + if (hookup_.amdoptions.NumAvailGPRs == ~0u) + hookup_.amdoptions.NumAvailGPRs = Options()->NumAvailGPRs; + } +#endif + OptLevel* gpuOpt = getOptLevel(Options(), true); + if (Options()->oVariables->EnableBuildTiming) { + time_opt = amd::Os::timeNanos(); + } + ret = gpuOpt->optimize(LLVMBinary(), true); + if (Options()->oVariables->EnableBuildTiming) { + time_opt = amd::Os::timeNanos() - time_opt; + std::stringstream tmp_ss; + tmp_ss << " LLVM Opt time: " + << time_opt/1000ULL + << "us\n"; + appendLogToCL(CL(), tmp_ss.str()); + } + delete gpuOpt; + + if ( ret ) { + BuildLog() += "Internal Error: optimizer failed!\n"; + return 1; + } + if (Options()->isDumpFlagSet(amd::option::DUMP_BC_OPTIMIZED)) { + std::string MyErrorInfo; + std::string fileName = Options()->getDumpFileName("_optimized.bc"); + raw_fd_ostream outs (fileName.c_str(), MyErrorInfo, raw_fd_ostream::F_Binary); + // FIXME: Need to add this to the elf binary! + if (MyErrorInfo.empty()) + WriteBitcodeToFile(LLVMBinary(), outs); + else + printf(MyErrorInfo.c_str()); + } + return ret; +} diff --git a/rocclr/compiler/lib/backends/common/optimizer.hpp b/rocclr/compiler/lib/backends/common/optimizer.hpp new file mode 100644 index 0000000000..5528eaf6e1 --- /dev/null +++ b/rocclr/compiler/lib/backends/common/optimizer.hpp @@ -0,0 +1,106 @@ +// +// Copyright (c) 2008 Advanced Micro Devices, Inc. All rights reserved. +// +#ifndef _BE_OPTIMIZER_HPP_ +#define _BE_OPTIMIZER_HPP_ +#include "aclTypes.h" +#include "compiler_stage.hpp" +#include "llvm/Module.h" + +namespace amdcl +{ + /*! \addtogroup Compiler Library + * + * \copydoc amdcl::Optimizer + * + * @{ + */ + class Optimizer : public LLVMCompilerStage { + Optimizer(Optimizer&); // DO NOT IMPLEMENT. + Optimizer(); // DO NOT IMPLEMENT. + public: + Optimizer(aclCompiler *cl, aclBinary* elf, aclLogFunction log) + : LLVMCompilerStage(cl, elf, log) { + // Expose some options to LLVM + llvm::AMDOptions *amdopts = &hookup_.amdoptions; + if (opts_) { + amdopts->OptLiveness = opts_->oVariables->OptLiveness; + amdopts->NumAvailGPRs = opts_->NumAvailGPRs; + } + } + + virtual ~Optimizer() {} + + /*! Function that takes in the LLVM module as input + * and optimizes it. + * Returns 0 on success and non-zero on failure. + */ + virtual int optimize(llvm::Module *input) = 0; + + }; // class Optimizer + /*@}*/ + + /*! \addtogroup Compiler Library + * + * \copydoc amdcl::CLOptimizer + * + * @{ + */ + class CLOptimizer : public Optimizer { + public: + CLOptimizer(aclCompiler *cl, aclBinary *elf, aclLogFunction log) + : Optimizer(cl, elf, log) {} + virtual ~CLOptimizer() {} + + /*! Function that takes in the LLVM module as input + * and optimizes it. + * Returns 0 on success and non-zero on failure. + */ + virtual int optimize(llvm::Module *input) = 0; + }; // class CLOptimizer + /*@}*/ + + /*! \addtogroup Compiler Library + * + * \copydoc amdcl::GPUOptimizer + * + * @{ + */ + class GPUOptimizer : public CLOptimizer { + public: + GPUOptimizer(aclCompiler *cl, aclBinary *elf, aclLogFunction log) + : CLOptimizer(cl, elf, log) {} + virtual ~GPUOptimizer() {} + + /*! Function that takes in the LLVM module as input + * and optimizes it. + * Returns 0 on success and non-zero on failure. + */ + virtual int optimize(llvm::Module *input); + }; // class GPUOptimizer + /*@}*/ + + /*! \addtogroup Compiler Library + * + * \copydoc amdcl::CPUOptimizer + * + * @{ + */ + class CPUOptimizer : public CLOptimizer { + public: + CPUOptimizer(aclCompiler *cl, aclBinary *elf, aclLogFunction log) + : CLOptimizer(cl, elf, log) {} + virtual ~CPUOptimizer() {} + + /*! Function that takes in the LLVM module as input + * and optimizes it. + * Returns 0 on success and non-zero on failure. + */ + virtual int optimize(llvm::Module *input); + protected: + int preOptimizer(llvm::Module *m); + }; // class CPUOptimizer + /*@}*/ + +}; // amdcl namespace +#endif // _BE_OPTIMIZER_HPP_ diff --git a/rocclr/compiler/lib/backends/common/spir.cpp b/rocclr/compiler/lib/backends/common/spir.cpp new file mode 100644 index 0000000000..1ac9dbc231 --- /dev/null +++ b/rocclr/compiler/lib/backends/common/spir.cpp @@ -0,0 +1,138 @@ +// +// Copyright (c) 2011 Advanced Micro Devices, Inc. All rights reserved. +// +#include "top.hpp" +#include "spir.hpp" +#include "aclTypes.h" +#include "bif/bifbase.hpp" +#include "utils/libUtils.h" +#include "utils/options.hpp" +#include "utils/target_mappings.h" +#include "os/os.hpp" + +#include +#include +#include +#include +#include + +#include "llvm/DataLayout.h" +#include "llvm/Module.h" +#include "llvm/Pass.h" +#include "llvm/PassManager.h" +#include "llvm/ADT/SmallString.h" +#include "llvm/Analysis/Verifier.h" +#include "llvm/Analysis/SPIRVerifier.h" +#include "llvm/Assembly/PrintModulePass.h" +#include "llvm/Bitcode/ReaderWriter.h" +#include "llvm/Bitcode/BitstreamWriter.h" +#include "llvm/Support/raw_ostream.h" +#include "llvm/Transforms/Scalar.h" + +using namespace llvm; +using namespace amdcl; + +Module* +amdcl::SPIR::loadSPIR(std::string &spirBinary) +{ + // Need to use the namespace here since a parent function is called Module(). + llvm::Module *bc = NULL; + std::string errors; + source_ = spirBinary; + SPIRState State = {"", "", 1, 0, 1, 2}; + bc = amdcl::LLVMCompilerStage::loadBitcode(source_); + if (!bc) + { + errors = "loadBitcode failed"; + log_ += errors; + return NULL; + } + verifyModule(*bc, ReturnStatusAction, &errors); + if (!errors.empty()) { + log_ += errors; + errors.clear(); + } + FunctionPassManager FPM(bc); + if (Options()->oVariables->verifyHWSpir) { + if (!isHSAILTarget(Elf()->target)) { + verifySPIRModule(*bc, ReturnStatusAction, State, false, &errors); + } + if (!errors.empty()) { + log_ += errors; + errors.clear(); + delete bc; + return NULL; + } + } + if (Options()->oVariables->verifyLWSpir) { + if (!isHSAILTarget(Elf()->target)) { + verifySPIRModule(*bc, ReturnStatusAction, State, true, &errors); + } + if (!errors.empty()) { + log_ += errors; + errors.clear(); + delete bc; + return NULL; + } + } + return bc; +} +Module* +amdcl::SPIR::loadBitcode(std::string &binary) +{ + llvm::Module *bc = loadSPIR(binary); + if (!bc) return NULL; + + // FIXME: It is not clear why SPIRLoader is invoked so early here. + // The current view is to keep SPIRLoader as a pure pre-link pass to + // be called only by the linker. + StringRef LayoutStr = is64BitTarget(Elf()->target) ? + DATA_LAYOUT_64BIT : DATA_LAYOUT_32BIT; + bc->setDataLayout(LayoutStr); + bc->setTargetTriple(familySet[Elf()->target.arch_id].triple); + + llvm::PassManager SPIRPasses; + SPIRPasses.add(new llvm::DataLayout(bc)); + SPIRPasses.add(createSPIRLoader(/*demangleBuiltin=*/ true)); + SPIRPasses.run(*bc); + return bc; +} + +const void* +SPIR::toBinary(const void *text, size_t text_size, size_t *binary_size) +{ + std::string text_buf(reinterpret_cast(text), text_size); + // Need to use the namespace here since a parent function is called Module(). + llvm::Module *mod = loadSPIR(text_buf); + SmallString<256> char_buf; + raw_svector_ostream outstream(char_buf); + WriteBitcodeToFile(mod, outstream); + std::string str_buf(char_buf.begin(), char_buf.end()); + (*binary_size) = char_buf.size(); + void *ptr = aclutAlloc(CL())(*binary_size); + std::copy(char_buf.begin(), char_buf.end(), reinterpret_cast(ptr)); + return ptr; +} + +const void* +SPIR::toText(const void *binary, size_t binary_size, size_t *text_size) +{ + std::string text_buf(reinterpret_cast(binary), binary_size); + // Need to use the namespace here since a parent function is called Module(). + llvm::Module *mod = loadSPIR(text_buf); + std::string errors; + if (!mod) + { + errors = "loadSPIR failed"; + log_ += errors; + return NULL; + } + std::string bin_buf; + raw_string_ostream buf(bin_buf); + mod->print(buf, NULL); + (*text_size) = bin_buf.size(); + void *ptr = aclutAlloc(CL())(*text_size); + std::copy(bin_buf.begin(), bin_buf.end(), reinterpret_cast(ptr)); + return ptr; +} + diff --git a/rocclr/compiler/lib/backends/common/spir.hpp b/rocclr/compiler/lib/backends/common/spir.hpp new file mode 100644 index 0000000000..73d346df8e --- /dev/null +++ b/rocclr/compiler/lib/backends/common/spir.hpp @@ -0,0 +1,40 @@ +// +// Copyright (c) 2008 Advanced Micro Devices, Inc. All rights reserved. +// +#ifndef _BE_SPIR_HPP_ +#define _BE_SPIR_HPP_ +#include +#include "aclTypes.h" +#include "compiler_stage.hpp" +namespace amdcl +{ + /*@}*/ + + /*! \addtogroup CompilerLibrary + * + * \copydoc amdcl::SPIR + * + * @{ + * \brief Implementation of the Frontend interface to compile + * from OpenCL C to LLVM-IR. + */ + class SPIR : public LLVMCompilerStage { + SPIR(SPIR&); // DO NOT IMPLEMENT. + SPIR(); // DO NOT IMPLEMENT. + + public: + SPIR(aclCompiler* cl, aclBinary* elf, aclLogFunction log) + : LLVMCompilerStage(cl, elf, log) {} + + virtual ~SPIR() {} + virtual llvm::Module* loadBitcode(std::string &spirBinary); + virtual llvm::Module* loadSPIR(std::string &spirBinary); + const void* + toBinary(const void *text, size_t text_size, size_t *binary_size); + const void* + toText(const void *binary, size_t binary_size, size_t *text_size); + + }; // class SPIR + /*@}*/ +} // namespac amdcl +#endif // _BE_SPIR_HPP_ diff --git a/rocclr/compiler/lib/backends/common/v0_8/if_acl.cpp b/rocclr/compiler/lib/backends/common/v0_8/if_acl.cpp new file mode 100644 index 0000000000..8f901d3ea7 --- /dev/null +++ b/rocclr/compiler/lib/backends/common/v0_8/if_acl.cpp @@ -0,0 +1,2736 @@ +// +// Copyright (c) 2012 Advanced Micro Devices, Inc. All rights reserved. +// +#ifdef WITH_TARGET_HSAIL +#include "libHSAIL/HSAILBrigContainer.h" +#include "libHSAIL/HSAILDisassembler.h" +#include "libHSAIL/HSAILBrigObjectFile.h" + +//prevent macro redefinition in drivers\hsa\compiler\lib\promotions\oclutils\top.hpp +//as it's already defined in drivers\hsa\compiler\llvm\include\llvm\Support\Format.h +#undef snprintf +#endif + +#include "acl.h" +#include "aclTypes.h" +#include "compiler_stage.hpp" +#include "frontend.hpp" +#include "spir.hpp" +#include "codegen.hpp" +#include "library.hpp" +#include "linker.hpp" +#include "optimizer.hpp" +#include "amdil_be.hpp" +#include "hsail_be.hpp" +#include "x86_be.hpp" +#include "scCompileBase.h" +#include "bif/bifbase.hpp" +#include "os/os.hpp" +#include "utils/bif_section_labels.hpp" +#include "utils/libUtils.h" +#include "utils/options.hpp" +#include "utils/target_mappings.h" +#include "utils/versions.hpp" + +#include "llvm/LLVMContext.h" +#include "llvm/Analysis/Passes.h" +#include "llvm/Support/CommandLine.h" +#include "llvm/Support/ManagedStatic.h" +#include "llvm/Support/PrettyStackTrace.h" +#include "llvm/Support/raw_ostream.h" +#include "llvm/Support/Signals.h" +#include "llvm/Support/TargetSelect.h" +#include "llvm/Support/Threading.h" +#include "llvm/Bitcode/BitstreamWriter.h" +#include "llvm/Bitcode/ReaderWriter.h" +#include "llvm/Support/IRReader.h" +#include "llvm/Transforms/Scalar.h" +#include +#include +#include +#include +#include + +aclLoaderData * ACL_API_ENTRY +if_aclCompilerInit(aclCompiler *cl, aclBinary *bin, + aclLogFunction log, acl_error *error) +{ + llvm::llvm_acquire_global_lock(); + char* timing = ::getenv("AMD_DEBUG_HLC_ENABLE_TIMING"); + if (timing && (timing[0] == '1')) + llvm::TimePassesIsEnabled = true; + else + llvm::TimePassesIsEnabled = false; + if (cl->llvm_shutdown == NULL) { + cl->llvm_shutdown = reinterpret_cast + (new llvm::llvm_shutdown_obj(false)); + } + // Initialize targets first. + llvm::InitializeAllTargets(); + + llvm::InitializeAllAsmPrinters(); + llvm::InitializeAllTargetMCs(); + // Initialize passes + llvm::PassRegistry &Registry = *llvm::PassRegistry::getPassRegistry(); + llvm::initializeCore(Registry); + llvm::initializeTransformUtils(Registry); + llvm::initializeScalarOpts(Registry); + llvm::initializeInstCombine(Registry); + llvm::initializeIPO(Registry); + llvm::initializeInstrumentation(Registry); + llvm::initializeAnalysis(Registry); + llvm::initializeIPA(Registry); + llvm::initializeCodeGen(Registry); + llvm::initializeTarget(Registry); + llvm::initializeVerifierPass(Registry); + llvm::initializeDominatorTreePass(Registry); + llvm::initializePreVerifierPass(Registry); + llvm::llvm_release_global_lock(); + if (error) (*error) = ACL_SUCCESS; + return reinterpret_cast(cl); +} +acl_error ACL_API_ENTRY +if_aclCompilerFini(aclLoaderData *ald) +{ + if (ald == NULL) return ACL_INVALID_ARG; + aclCompiler *cl = reinterpret_cast(ald); + return ACL_SUCCESS; +} + + +#define LOADER_FUNCS(NAME, TYPE) \ + aclLoaderData* ACL_API_ENTRY \ +NAME##Init(aclCompiler *cl,\ + aclBinary *bin, \ + aclLogFunction callback,\ + acl_error *error)\ +{\ + acl_error error_code = ACL_SUCCESS;\ + TYPE *acl = new TYPE(cl, bin, callback);\ + if (acl == NULL) {\ + error_code = ACL_OUT_OF_MEM;\ + }\ + if (error != NULL) (*error) = error_code;\ + return reinterpret_cast(acl);\ +}\ +acl_error ACL_API_ENTRY \ +NAME##Fini(aclLoaderData *ald)\ +{\ + acl_error error_code = ACL_SUCCESS;\ + TYPE *acl = reinterpret_cast(ald);\ + if (acl == NULL) {\ + error_code = ACL_INVALID_ARG;\ + } else {\ + delete acl;\ + }\ + return error_code;\ +} + +#define LOADER_FUNCS_ERROR(NAME, TYPE) \ + aclLoaderData* ACL_API_ENTRY \ +NAME##Init(aclCompiler *cl,\ + aclBinary *bin, \ + aclLogFunction callback,\ + acl_error *error)\ +{\ + assert(!"Cannot go down this path without enabling support!"); \ + if (error) (*error) = ACL_SYS_ERROR; \ + return NULL; \ +}\ +acl_error ACL_API_ENTRY \ +NAME##Fini(aclLoaderData *ald)\ +{\ + assert(!"Cannot go down this path without enabling support!"); \ + return ACL_SYS_ERROR; \ +} + +#if defined(WITH_TARGET_AMDIL) +LOADER_FUNCS(AMDIL, amdcl::AMDIL); +LOADER_FUNCS(AMDILOpt, amdcl::GPUOptimizer); +#else +LOADER_FUNCS_ERROR(AMDIL, amdcl::AMDIL); +LOADER_FUNCS_ERROR(AMDILOpt, amdcl::GPUOptimizer); +#endif + +#if defined(WITH_TARGET_HSAIL) +LOADER_FUNCS(HSAILAsm, amdcl::HSAIL); +LOADER_FUNCS(HSAILFE, amdcl::OCLFrontend); +LOADER_FUNCS(HSAILOpt, amdcl::GPUOptimizer); +#else +LOADER_FUNCS_ERROR(HSAILAsm, amdcl::HSAIL); +LOADER_FUNCS_ERROR(HSAILFE, amdcl::OCLFrontend); +LOADER_FUNCS_ERROR(HSAILOpt, amdcl::GPUOptimizer); +#endif + +#if defined(WITH_TARGET_X86) +LOADER_FUNCS(X86Asm, amdcl::X86); +LOADER_FUNCS(X86Opt, amdcl::CPUOptimizer); +#else +LOADER_FUNCS_ERROR(X86Asm, amdcl::X86); +LOADER_FUNCS_ERROR(X86Opt, amdcl::CPUOptimizer); +#endif + +LOADER_FUNCS(OCL, amdcl::OCLFrontend); +LOADER_FUNCS(Link, amdcl::OCLLinker); +LOADER_FUNCS(Codegen, amdcl::CLCodeGen); +LOADER_FUNCS(SPIR, amdcl::SPIR); +#undef LOADER_FUNCS + +// CLC Frontend phase +aclModule* ACL_API_ENTRY +OCLFEToLLVMIR( + aclLoaderData *ald, + const char *source, + size_t data_size, + aclContext *ctx, + acl_error *error) +{ + if (error != NULL) (*error) = ACL_SUCCESS; + amdcl::Frontend *aclFE = reinterpret_cast(ald); + aclFE->setContext(ctx); + int ret; + std::string src_str(source, data_size); + ret = aclFE->compileCommand(src_str); + if (!aclFE->BuildLog().empty()) { + appendLogToCL(aclFE->CL(), aclFE->BuildLog()); + } + if (ret) { + if (error != NULL) (*error) = ACL_FRONTEND_FAILURE; + return NULL; + } + return aclFE->Module(); +} + +aclModule* ACL_API_ENTRY +OCLFEToSPIR( + aclLoaderData *ald, + const char *source, + size_t data_size, + aclContext *ctx, + acl_error *error) +{ + if (error != NULL) (*error) = ACL_SUCCESS; + amdcl::Frontend *aclFE = reinterpret_cast(ald); + aclFE->setContext(ctx); + int ret; + std::string src_str(source, data_size); + ret = aclFE->compileCommand(src_str); + if (!aclFE->BuildLog().empty()) { + appendLogToCL(aclFE->CL(), aclFE->BuildLog()); + } + if (ret) { + if (error != NULL) (*error) = ACL_FRONTEND_FAILURE; + return NULL; + } + return aclFE->Module(); +} +aclModule* ACL_API_ENTRY +SPIRToModule( + aclLoaderData *ald, + const char *source, + size_t data_size, + aclContext *ctx, + acl_error *error) +{ + if (error != NULL) (*error) = ACL_SUCCESS; + amdcl::SPIR *aclSPIR = reinterpret_cast(ald); + aclSPIR->setContext(ctx); + std::string dataStr(source, data_size); + aclModule *module = reinterpret_cast(aclSPIR->loadBitcode(dataStr)); + if (!aclSPIR->BuildLog().empty()) { + appendLogToCL(aclSPIR->CL(), aclSPIR->BuildLog()); + } + if (module == NULL) { + if (error != NULL) (*error) = ACL_FRONTEND_FAILURE; + return NULL; + } + return module; +} + +aclModule * ACL_API_ENTRY +RSLLVMIRToModule( + aclLoaderData *ald, + const char *source, + size_t data_size, + aclContext *ctx, + acl_error *error) +{ + if (error != NULL) (*error) = ACL_SUCCESS; + std::string llvmBinary(source, data_size); + std::string ErrorMessage; + llvm::LLVMContext * Context = reinterpret_cast(ctx); + llvm::MemoryBuffer *Buffer = + llvm::MemoryBuffer::getMemBufferCopy( + llvm::StringRef(llvmBinary), "input.bc"); + llvm::Module *M = NULL; + + if (llvm::isBitcode((const unsigned char *)Buffer->getBufferStart(), + (const unsigned char *)Buffer->getBufferEnd())) { + M = llvm::ParseBitcodeFile(Buffer, *Context, &ErrorMessage); + } + + if (M == NULL) { + if (error != NULL) (*error) = ACL_INVALID_BINARY; + return NULL; + } + + amdcl::CompilerStage *cs = reinterpret_cast(ald); + aclDevType arch_id = cs->Elf()->target.arch_id; + if ((arch_id != aclAMDIL) && (arch_id != aclHSAIL)) { + assert("Unsupported architecture, expect amdil."); + return NULL; + } + + const char * NewTriple = familySet[aclAMDIL].triple; + std::string OldTriple = M->getTargetTriple(); + + if (OldTriple.compare("armv7-none-linux-gnueabi")) { + assert("Input target is unknown, expect armv7-none-linux-gnueabi."); + return NULL; + } + + M->setTargetTriple(NewTriple); + const char * LayoutStr = is64BitTarget(cs->Elf()->target) ? + DATA_LAYOUT_64BIT : DATA_LAYOUT_32BIT; + M->setDataLayout(LayoutStr); + llvm::PassManager TransformPasses; + TransformPasses.add(llvm::createOpenCLIRTransform()); + if (!TransformPasses.run(*M)) { + if (error != NULL) (*error) = ACL_FRONTEND_FAILURE; + return NULL; + } + + aclModule *module = reinterpret_cast(M); + return module; +} + +aclModule* ACL_API_ENTRY +OCLFEToModule( + aclLoaderData *ald, + const char *source, + size_t data_size, + aclContext *ctx, + acl_error *error) +{ + if (error != NULL) (*error) = ACL_SUCCESS; + amdcl::Frontend *aclFE = reinterpret_cast(ald); + aclFE->setContext(ctx); + std::string dataStr(source, data_size); + aclModule *module = reinterpret_cast(aclFE->loadBitcode(dataStr)); + if (!aclFE->BuildLog().empty()) { + appendLogToCL(aclFE->CL(), aclFE->BuildLog()); + } + if (module == NULL) { + if (error != NULL) (*error) = ACL_FRONTEND_FAILURE; + return NULL; + } + return module; +} +acl_error ACL_API_ENTRY +AMDILFEToISA( + aclLoaderData *ald, + const char *source, + size_t data_size) +{ +#ifdef WITH_TARGET_AMDIL + acl_error error_code = ACL_SUCCESS; + amdcl::AMDIL *acl = reinterpret_cast(ald); + if (acl == NULL) { + error_code = ACL_FRONTEND_FAILURE; + } + else { + amd::option::Options* Opts = acl->Options(); + const char *kernel = Opts->getCurrKernelName(); + const char *name = (kernel == NULL) ? "main" : kernel; + if (acl->compile(source, name)) { + error_code = ACL_FRONTEND_FAILURE; + } + } + if (!acl->BuildLog().empty()) { + appendLogToCL(acl->CL(), acl->BuildLog()); + } + if (!checkFlag(aclutGetCaps(acl->Elf()), capSaveAMDIL)) { + acl->CL()->clAPI.remSec(acl->CL(), acl->Elf(), aclSOURCE); + } + return error_code; +#else + assert(!"Cannot go down this path without AMDIL support!"); + return ACL_SYS_ERROR; +#endif +} + +acl_error ACL_API_ENTRY +OCLFEToISA( + aclLoaderData *ald, + const char *source, + size_t data_size) +{ + assert(!"Not implemented!"); + return ACL_UNSUPPORTED; +} + +aclModule* ACL_API_ENTRY +OCLLinkToLLVMIR( + aclLoaderData *data, + aclModule *llvmBin, + aclContext *ctx, + acl_error *error) +{ + if (error != NULL) (*error) = ACL_UNSUPPORTED; + assert(!"Not implemented!"); + return NULL; +} +aclModule* ACL_API_ENTRY +OCLLinkToSPIR( + aclLoaderData *data, + aclModule *llvmBin, + aclContext *ctx, + acl_error *error) +{ + if (error != NULL) (*error) = ACL_UNSUPPORTED; + assert(!"Not implemented!"); + return NULL; +} + +// LLVM Link phase +aclModule* ACL_API_ENTRY +OCLLinkPhase( + aclLoaderData *data, + aclModule *llvmBin, + unsigned int numLibs, + aclModule **libs, + aclContext *ctx, + acl_error *error) +{ + if (error != NULL) (*error) = ACL_SUCCESS; + amdcl::OCLLinker *aclLink = reinterpret_cast(data); + if (aclLink == NULL || llvmBin == NULL || ctx == NULL) { + if (error != NULL) (*error) = ACL_INVALID_ARG; + return NULL; + } + const char* argv[] = { "", + "-loop-unswitch-threshold=0", + "-binomial-coefficient-limit-bitwidth=64" + }; + + aclLink->setContext(ctx); + amd::option::Options* Opts = reinterpret_cast(aclLink->Elf()->options); + int args = sizeof(argv) / sizeof(argv[0]); + llvm::cl::ParseCommandLineOptions(args, (char**)argv, "OpenCL"); + + if (Opts->getLLVMArgc()) + llvm::cl::ParseCommandLineOptions(Opts->getLLVMArgc(), + Opts->getLLVMArgv(), "OpenCL"); + + // LLVM Link phase + std::vector libvec; + for (unsigned x = 0; x < numLibs; ++x) { + if (libs[x] != NULL) { + libvec.push_back(reinterpret_cast(libs[x])); + } + } + int ret = aclLink->link(reinterpret_cast(llvmBin), libvec); + if (!aclLink->BuildLog().empty()) { + appendLogToCL(aclLink->CL(), aclLink->BuildLog()); + } + if (ret) { + if (error != NULL) (*error) = ACL_LINKER_ERROR; + return NULL; + } + return aclLink->Module(); +} + +aclModule* ACL_API_ENTRY +GPUOptPhase(aclLoaderData *data, + aclModule *llvmBin, + aclContext *ctx, + acl_error *error) +{ +#if defined(WITH_TARGET_AMDIL) || defined(WITH_TARGET_HSAIL) + amdcl::CompilerStage *cs = reinterpret_cast(data); + if (isGpuTarget(cs->Elf()->target)) { + if (error != NULL) (*error) = ACL_SUCCESS; + amdcl::GPUOptimizer *aclOpt = reinterpret_cast(data); + if (aclOpt == NULL || llvmBin == NULL || ctx == NULL) { + if (error != NULL) (*error) = ACL_INVALID_ARG; + return NULL; + } + // LLVM Optimize phase + aclOpt->setContext(ctx); + amd::option::Options* Opts = reinterpret_cast(aclOpt->Elf()->options); + if (Opts->getLLVMArgc()) + llvm::cl::ParseCommandLineOptions(Opts->getLLVMArgc(), + Opts->getLLVMArgv(), "OpenCL"); + + int ret = aclOpt->optimize(reinterpret_cast(llvmBin)); + if (!aclOpt->BuildLog().empty()) { + appendLogToCL(aclOpt->CL(), aclOpt->BuildLog()); + } + if (ret) { + if (error != NULL) (*error) = ACL_OPTIMIZER_ERROR; + return NULL; + } + return aclOpt->Module(); + } else { + assert(!"GPUOptPhase should be called only for AMDIL or HSAIL target."); + if (error) (*error) = ACL_SYS_ERROR; + return NULL; + } +#else + assert(!"Cannot go down this path without GPU support!"); + if (error) (*error) = ACL_SYS_ERROR; + return NULL; +#endif +} + +aclModule* ACL_API_ENTRY +X86OptPhase(aclLoaderData *data, + aclModule *llvmBin, + aclContext *ctx, + acl_error *error) +{ +#if defined(WITH_TARGET_X86) + if (error != NULL) (*error) = ACL_SUCCESS; + amdcl::CPUOptimizer *aclOpt = reinterpret_cast(data); + if (aclOpt == NULL || llvmBin == NULL || ctx == NULL) { + if (error != NULL) (*error) = ACL_INVALID_ARG; + return NULL; + } + // LLVM Optimize phase + aclOpt->setContext(ctx); + amd::option::Options* Opts = reinterpret_cast(aclOpt->Elf()->options); + if (Opts->getLLVMArgc()) + llvm::cl::ParseCommandLineOptions(Opts->getLLVMArgc(), + Opts->getLLVMArgv(), "OpenCL"); + int ret = aclOpt->optimize(reinterpret_cast(llvmBin)); + if (!aclOpt->BuildLog().empty()) { + appendLogToCL(aclOpt->CL(), aclOpt->BuildLog()); + } + if (ret) { + if (error != NULL) (*error) = ACL_OPTIMIZER_ERROR; + return NULL; + } + return aclOpt->Module(); +#else + assert(!"Cannot go down this path without X86 support!"); + if (error) (*error) = ACL_SYS_ERROR; + return NULL; +#endif +} + +const void* ACL_API_ENTRY +CodegenPhase(aclLoaderData *data, + aclModule *llvmBin, + aclContext *ctx, + acl_error *error) +{ + if (error != NULL) (*error) = ACL_SUCCESS; + amdcl::CLCodeGen *aclCG = reinterpret_cast(data); + if (aclCG == NULL || llvmBin == NULL || ctx == NULL) { + if (error != NULL) (*error) = ACL_INVALID_ARG; + return NULL; + } + aclCG->setContext(ctx); + amd::option::Options* Opts = reinterpret_cast(aclCG->Elf()->options); + if (Opts->getLLVMArgc()) + llvm::cl::ParseCommandLineOptions(Opts->getLLVMArgc(), + Opts->getLLVMArgv(), "OpenCL"); + // LLVM Codegen phase + int ret = aclCG->codegen(reinterpret_cast(llvmBin)); + if (!aclCG->BuildLog().empty()) { + appendLogToCL(aclCG->CL(), aclCG->BuildLog()); + } + if (ret) { + if (error != NULL) (*error) = ACL_CODEGEN_ERROR; + return NULL; + } +#ifdef WITH_TARGET_HSAIL + if (isHSAILTarget(aclCG->Elf()->target)) { + if (checkFlag(&aclCG->Elf()->caps, capSaveCG)) { + const oclBIFSymbolStruct* symbol = findBIF30SymStruct(symHSAILText); + assert(symbol && "symbol not found"); + std::string name = symbol->str[PRE] + std::string("main") + + symbol->str[POST]; + + /*@todo r=Leonid, modify hsa\drivers\opencl\runtime\device\hsa\hsaprogram.cpp to parse BRIG section + * for kernel names instead searching for "kernel &" in text HSAIL. + */ + HSAIL_ASM::BrigContainer c; + + int result=HSAIL_ASM::BrigStreamer::load(c, aclCG->Source().data(), aclCG->Source().length()); + if (result!=0) + { + (*error) = ACL_CODEGEN_ERROR; + return NULL; + } + + HSAIL_ASM::Disassembler disasm(c); + std::ostringstream hsail_stream; + int ret = disasm.run(hsail_stream); + if (ret) { + (*error) = ACL_CODEGEN_ERROR; + return NULL; + } + hsail_stream.flush(); + // TBD extra string copy + const std::string& hsail = hsail_stream.str(); + amdcl::HSAIL *acl = reinterpret_cast(data); + // Dumping of HSAIL to file if needed + if (acl) + acl->dumpHSAIL(hsail, Opts, ".hsail"); + aclCG->CL()->clAPI.insSym(aclCG->CL(), aclCG->Elf(), + hsail.data(), hsail.size(), aclCODEGEN, name.c_str()); + } + return reinterpret_cast(&(aclCG->Source())); + } else +#endif + { + if (checkFlag(aclutGetCaps(aclCG->Elf()), capSaveCG)) { + aclCG->CL()->clAPI.insSec(aclCG->CL(), aclCG->Elf(), + aclCG->Source().data(), + aclCG->Source().size(), aclCODEGEN); + } + return reinterpret_cast(aclCG->Source().c_str()); + } +} + +acl_error ACL_API_ENTRY +AMDILAsmPhase(aclLoaderData *data, + const char *source, + size_t data_size) +{ +#ifdef WITH_TARGET_AMDIL + acl_error error_code = ACL_SUCCESS; + if (source == NULL) { + return ACL_INVALID_BINARY; + } + amdcl::AMDIL *acl = reinterpret_cast(data); + if (acl == NULL || acl->jit(source)) { + error_code = ACL_CODEGEN_ERROR; + } + if (!acl->BuildLog().empty()) { + appendLogToCL(acl->CL(), acl->BuildLog()); + } + return error_code; +#else + assert(!"Cannot go down this path without AMDIL support!"); + return ACL_CODEGEN_ERROR; +#endif +} +acl_error ACL_API_ENTRY +AMDILDisassemble(aclLoaderData *data, + const char *kernel, + const void *isa_code, + size_t isa_size) +{ +#ifdef WITH_TARGET_AMDIL + std::string isaDump = ""; + std::string isaName = ""; + acl_error error_code = ACL_SUCCESS; + if (isa_code == NULL || isa_size == 0 || kernel == NULL) { + return ACL_INVALID_ARG; + } + amdcl::AMDIL *acl = reinterpret_cast(data); + if (acl == NULL) { + error_code = ACL_INVALID_ARG; + } + isaDump = acl->disassemble(isa_code, isa_size); + const oclBIFSymbolStruct* symbol = findBIF30SymStruct(symISAText); + assert(symbol && "symbol not found"); + isaName = symbol->str[PRE] + std::string(kernel) + symbol->str[POST]; + if (!isaDump.empty()) { + error_code = acl->CL()->clAPI.insSym(acl->CL(), acl->Elf(), + isaDump.data(), isaDump.size(), + symbol->sections[0], isaName.c_str()); + } + if (acl->Options()) { + std::string kernelFileName = acl->Options()->getDumpFileName("_" + std::string(kernel) + ".isa"); + amdcl::dumpISA(kernelFileName, isaDump, acl->Options()); + } + if (acl->Callback()) { + acl->Callback()(isaDump.data(), isaDump.size()); + } + return error_code; +#else + assert(!"Cannot go down this path without AMDIL support!"); + return ACL_SYS_ERROR; +#endif +} + +acl_error ACL_API_ENTRY +AMDILAssemble(aclLoaderData *data, + const char *source, + size_t data_size) +{ +#ifdef WITH_TARGET_AMDIL + assert(!"Not implemented!"); + return ACL_UNSUPPORTED; +#else + assert(!"Cannot go down this path without AMDIL support!"); + return ACL_SYS_ERROR; +#endif +} + +acl_error ACL_API_ENTRY +HSAILAsmPhase(aclLoaderData *data, + const char *source, + size_t data_size) +{ +#ifdef WITH_TARGET_HSAIL + acl_error error_code = ACL_SUCCESS; + if (source == NULL) { + return ACL_INVALID_BINARY; + } + amdcl::HSAIL *acl = reinterpret_cast(data); + if (acl == NULL) { + error_code = ACL_CODEGEN_ERROR; + } + SC_EXPORT_FUNCTIONS* scef = reinterpret_cast(acl->CL()->scAPI.scef); + if (scef[SC_HSAIL].SCCreate == NULL) { + // Fail if table has not been initialized, probably because dynamic SC has not been loaded. + // In this case, aclSCLoaderInit returns ACL_SUCCESS. + return ACL_CODEGEN_ERROR; + } + if (acl->finalize()) { + error_code = ACL_CODEGEN_ERROR; + } + if (!acl->BuildLog().empty()) { + appendLogToCL(acl->CL(), acl->BuildLog()); + } + return error_code; +#else + assert(!"Cannot go down this path without HSAIL support!"); + return ACL_SYS_ERROR; +#endif +} + +acl_error ACL_API_ENTRY +HSAILAssemble(aclLoaderData *data, + const char *source, + size_t data_size) +{ +#ifdef WITH_TARGET_HSAIL + acl_error error_code = ACL_SUCCESS; + amdcl::HSAIL *acl = reinterpret_cast(data); + if (acl == NULL || !acl->assemble(source)) { + // TODO_HSA: Should this be tagged as an assembler error? + // needs ACL_ASSEMBLER_ERROR + error_code = ACL_CODEGEN_ERROR; + appendLogToCL(acl->CL(), "Error assembling HSAIL text."); + } + if (!acl->BuildLog().empty()) + appendLogToCL(acl->CL(), acl->BuildLog()); + return error_code; +#else + assert(!"Cannot go down this path without HSAIL support!"); + return ACL_SYS_ERROR; +#endif +} + +acl_error ACL_API_ENTRY +HSAILDisassemble(aclLoaderData *data, + const char *kernel, + const void *isa_code, + size_t isa_size) +{ +#ifdef WITH_TARGET_HSAIL + std::string isaDump = ""; + std::string isaName = ""; + acl_error error_code = ACL_SUCCESS; + if (isa_code == NULL || isa_size == 0 || kernel == NULL) { + return ACL_INVALID_ARG; + } + amdcl::HSAIL *acl = reinterpret_cast(data); + if (acl == NULL) { + return ACL_INVALID_ARG; + } + isaDump = acl->disassemble(isa_code, isa_size); + const oclBIFSymbolStruct* symbol = findBIF30SymStruct(symISAText); + assert(symbol && "symbol not found"); + isaName = symbol->str[PRE] + std::string(kernel) + symbol->str[POST]; + if (!isaDump.empty()) { + error_code = acl->CL()->clAPI.insSym(acl->CL(), acl->Elf(), + isaDump.c_str(), isaDump.size(), + aclINTERNAL, isaName.c_str()); + } + if (acl->Options()) { + std::string kernelFileName = acl->Options()->getDumpFileName("_" + std::string(kernel) + ".isa"); + acl->dumpISA(kernelFileName, isaDump, acl->Options()); + } + if (acl->Callback()) { + acl->Callback()(isaDump.c_str(), isaDump.size()); + } + return error_code; +#else + assert(!"Cannot go down this path without HSAIL support!"); + return ACL_SYS_ERROR; +#endif +} + +acl_error ACL_API_ENTRY +X86AsmPhase(aclLoaderData *data, + const char *source, + size_t data_size) +{ +#ifdef WITH_TARGET_X86 + acl_error error_code = ACL_SUCCESS; + if (source == NULL) { + return ACL_INVALID_BINARY; + } + amdcl::X86 *acl = reinterpret_cast(data); + if (acl == NULL || acl->jit(source)) { + error_code = ACL_CODEGEN_ERROR; + } + if (!acl->BuildLog().empty()) { + appendLogToCL(acl->CL(), acl->BuildLog()); + } + return error_code; +#else + assert(!"Cannot go down this path without X86 support!"); + return ACL_SYS_ERROR; +#endif +} + + acl_error ACL_API_ENTRY +X86Assemble(aclLoaderData *data, + const char *source, + size_t data_size) +{ +#ifdef WITH_TARGET_X86 + assert(!"Not implemented!"); + return ACL_UNSUPPORTED; +#else + assert(!"Cannot go down this path without X86 support!"); + return ACL_SYS_ERROR; +#endif +} + +acl_error ACL_API_ENTRY +X86Disassemble(aclLoaderData *data, + const char *kernel, + const void *isa_code, + size_t isa_size) +{ +#ifdef WITH_TARGET_X86 + assert(!"Not implemented!"); + return ACL_UNSUPPORTED; +#else + assert(!"Cannot go down this path without X86 support!"); + return ACL_SYS_ERROR; +#endif +} + +static void +saveOptionsToComments(aclCompiler *cl, aclBinary *curElf, const char *str, std::string &symbol) +{ + if (str != NULL && !checkFlag(aclutGetCaps(curElf), capEncrypted) + && strlen(str)) { + size_t test = 0; + const void* ptr = cl->clAPI.extSym(cl, curElf, &test, aclCOMMENT, symbol.c_str(), NULL); + if (ptr == NULL || (ptr != NULL && (test != strlen(str) + || strcmp(reinterpret_cast(ptr), str)))) { + if (ptr != NULL) { + cl->clAPI.remSym(cl, curElf, aclCOMMENT, symbol.c_str()); + } + cl->clAPI.insSym(cl, curElf, str, strlen(str), aclCOMMENT, symbol.c_str()); + } + } +} + +aclLoaderData* ACL_API_ENTRY +OptInit(aclCompiler *cl, + aclBinary *bin, + aclLogFunction log, + acl_error *err) +{ + if (!bin) return NULL; + switch(bin->target.arch_id) + { + default: + assert(!"Found an unhandled architecture!"); + case aclX64: + case aclX86: return X86OptInit(cl, bin, log, err); + case aclHSAIL64: + case aclHSAIL: return HSAILOptInit(cl, bin, log, err); + case aclAMDIL64: + case aclAMDIL: return AMDILOptInit(cl, bin, log, err); + } + return NULL; +} + +acl_error ACL_API_ENTRY +OptFini(aclLoaderData *ptr) { + if (!ptr) return ACL_ERROR; + amdcl::CompilerStage *cs = reinterpret_cast(ptr); + switch (cs->Elf()->target.arch_id) { + default: + assert(!"Found an unhandled architecture!"); + case aclX64: + case aclX86: return X86OptFini(ptr); + case aclHSAIL64: + case aclHSAIL: return HSAILOptFini(ptr); + case aclAMDIL64: + case aclAMDIL: return AMDILOptFini(ptr); + } + return ACL_ERROR; +} + +aclModule* ACL_API_ENTRY +OptOptimize(aclLoaderData *data, + aclModule *llvmBin, + aclContext *ctx, + acl_error *error) +{ + if (!data) return NULL; + amdcl::CompilerStage *cs = reinterpret_cast(data); + switch (cs->Elf()->target.arch_id) { + default: + assert(!"Found an unhandled architecture!"); + case aclX64: + case aclX86: return X86OptPhase(data, llvmBin, ctx, error); + case aclHSAIL64: + case aclHSAIL: return GPUOptPhase(data, llvmBin, ctx, error); + case aclAMDIL64: + case aclAMDIL: return GPUOptPhase(data, llvmBin, ctx, error); + } + return NULL; +} + +aclLoaderData* ACL_API_ENTRY +BEInit(aclCompiler *cl, + aclBinary *bin, + aclLogFunction log, + acl_error *err) +{ + if (!bin) return NULL; + switch(bin->target.arch_id) + { + default: + assert(!"Found an unhandled architecture!"); + case aclX64: + case aclX86: return X86AsmInit(cl, bin, log, err); + case aclHSAIL64: + case aclHSAIL: return HSAILAsmInit(cl, bin, log, err); + case aclAMDIL64: + case aclAMDIL: return AMDILInit(cl, bin, log, err); + } + return NULL; +} + +acl_error ACL_API_ENTRY +BEFini(aclLoaderData *ptr) +{ + if (!ptr) return ACL_ERROR; + amdcl::CompilerStage *cs = reinterpret_cast(ptr); + switch (cs->Elf()->target.arch_id) { + default: + assert(!"Found an unhandled architecture!"); + case aclX64: + case aclX86: return X86AsmFini(ptr); + case aclHSAIL64: + case aclHSAIL: return HSAILAsmFini(ptr); + case aclAMDIL64: + case aclAMDIL: return AMDILFini(ptr); + } + return ACL_ERROR; +} + +acl_error ACL_API_ENTRY +BEAsmPhase(aclLoaderData *data, + const char *source, + size_t data_size) +{ + if (!data) return ACL_ERROR; + amdcl::CompilerStage *cs = reinterpret_cast(data); + switch (cs->Elf()->target.arch_id) { + default: + assert(!"Found an unhandled architecture!"); + case aclX64: + case aclX86: return X86AsmPhase(data, source, data_size); + case aclHSAIL64: + case aclHSAIL: return HSAILAsmPhase(data, source, data_size); + case aclAMDIL64: + case aclAMDIL: return AMDILAsmPhase(data, source, data_size); + } + return ACL_ERROR; + +} + + +acl_error ACL_API_ENTRY +BEAssemble(aclLoaderData *data, + const char *source, + size_t data_size) +{ + if (!data) return ACL_ERROR; + amdcl::CompilerStage *cs = reinterpret_cast(data); + switch (cs->Elf()->target.arch_id) { + default: + assert(!"Found an unhandled architecture!"); + case aclX64: + case aclX86: return X86Assemble(data, source, data_size); + case aclHSAIL64: + case aclHSAIL: return HSAILAssemble(data, source, data_size); + case aclAMDIL64: + case aclAMDIL: return AMDILAssemble(data, source, data_size); + } + return ACL_ERROR; + +} + +acl_error ACL_API_ENTRY +BEDisassemble(aclLoaderData *data, + const char *kernel, + const void *isa_code, + size_t data_size) +{ + if (!data) return ACL_ERROR; + amdcl::CompilerStage *cs = reinterpret_cast(data); + switch (cs->Elf()->target.arch_id) { + default: + assert(!"Found an unhandled architecture!"); + case aclX64: + case aclX86: return X86Disassemble(data, kernel, isa_code, data_size); + case aclHSAIL64: + case aclHSAIL: return HSAILDisassemble(data, kernel, isa_code, data_size); + case aclAMDIL64: + case aclAMDIL: return AMDILDisassemble(data, kernel, isa_code, data_size); + } + return ACL_ERROR; + +} + +acl_error +finalizeBinary(aclCompiler *cl, aclBinary *bin) +{ + if (!bin || !bin->bin || !bin->options) return ACL_INVALID_ARG; + if (cl) { + size_t test = 0; + const void* ptr = cl->clAPI.extSym(cl, bin, &test, aclCOMMENT, "acl_version_string", NULL); + if (ptr == NULL || (ptr != NULL && (test != strlen(AMD_COMPILER_INFO) + || strcmp(reinterpret_cast(ptr), "acl_version_string")))) { + if (ptr != NULL) { + cl->clAPI.remSym(cl, bin, aclCOMMENT, "acl_version_string"); + } + cl->clAPI.insSym(cl, bin, + reinterpret_cast(AMD_COMPILER_INFO), + strlen(AMD_COMPILER_INFO), aclCOMMENT, + "acl_version_string"); + } +#ifdef WITH_TARGET_HSAIL + if (isHSAILTarget(bin->target)) { + // Dumping of BIF to file if needed + amd::option::Options* Opts = reinterpret_cast(bin->options); + if (Opts && Opts->isDumpFlagSet(amd::option::DUMP_BIF)) { + std::string fileName = Opts->getDumpFileName(".bif"); + if (aclWriteToFile(bin, fileName.c_str()) != ACL_SUCCESS) + printf("Error - Failure in saving BIF file %s.\n", fileName.c_str()); + } + } +#endif + } + return ACL_SUCCESS; +} + +acl_error ACL_API_ENTRY +HSAILFEToISA( + aclLoaderData *ald, + const char *source, + size_t data_size) +{ + acl_error error_code = HSAILAssemble(ald, source, data_size); + if (error_code != ACL_SUCCESS) + return error_code; + return BEAsmPhase(ald, source, data_size); +} + +static char * readFile(const char *source, size_t& size) { + FILE *fp = ::fopen( source, "rb" ); + unsigned int length; + size_t offset = 0; + char *ptr; + + if (!fp) { + return NULL; + } + + // obtain file size. + ::fseek (fp , 0 , SEEK_END); + length = ::ftell (fp); + ::rewind (fp); + + ptr = new char[offset + length + 1]; + + if (length != fread(&ptr[offset], 1, length, fp)) + { + delete [] ptr; + return NULL; + } + + ptr[offset + length] = '\0'; + size = offset + length; + ::fclose(fp); + + return ptr; +} + +static acl_error +aclCompileInternal( + aclCompiler *cl, + aclBinary *bin, + const char *data, + size_t data_size, + aclLogFunction compile_callback, + bool useFE, + bool useLinker, + bool useOpt, + bool useCG, + bool useISA) +{ + llvm::LLVMContext myCtx; + aclContext *context = reinterpret_cast(&myCtx); + aclModule *module = NULL; + std::string dataStr = std::string(data, data_size); + acl_error error_code = ACL_SUCCESS; + aclLoaderData *ald; + + + // Load the frontend to convert from Source to LLVM-IR + if (useFE) { + ald = cl->feAPI.init(cl, bin, compile_callback, &error_code); + if (!useLinker && !useCG && !useOpt && !useISA && cl->feAPI.toISA != NULL) { + error_code = cl->feAPI.toISA(ald, data, data_size); + } else { + if (cl->feAPI.toIR == NULL) { + error_code = ACL_SYS_ERROR; + goto internal_compile_failure; + } + module = cl->feAPI.toIR(ald, data, data_size, context, &error_code); + } + cl->feAPI.fini(ald); + if (error_code != ACL_SUCCESS) { + goto internal_compile_failure; + } + } else if (useLinker || useOpt || useCG) { + // Load a temp frontend object to convert from string LLVM-IR to LLVM Module. + ald = cl->feAPI.init(cl, bin, compile_callback, &error_code); + module = cl->feAPI.toModule(ald, data, data_size, context, &error_code); + cl->feAPI.fini(ald); + if (error_code != ACL_SUCCESS) { + goto internal_compile_failure; + } + } + + // Use the linker to link in the libraries to the current module. + if (useLinker) { + ald = cl->linkAPI.init(cl, bin, compile_callback, &error_code); + module = cl->linkAPI.link(ald, module, 0, NULL, context, &error_code); + cl->linkAPI.fini(ald); + if (error_code != ACL_SUCCESS) { + goto internal_compile_failure; + } + } + + // Use the optimizer on the module at the given optimization level. + if (useOpt) { + ald = cl->optAPI.init(cl, bin, compile_callback, &error_code); + module = cl->optAPI.optimize(ald, module, context, &error_code); + cl->optAPI.fini(ald); + if (error_code != ACL_SUCCESS) { + goto internal_compile_failure; + } + } + + // Use the code generators to generate the ISA/IL string. + if (useCG) { + ald = cl->cgAPI.init(cl, bin, compile_callback, &error_code); +#ifdef WITH_TARGET_HSAIL + amdcl::CompilerStage *cs = reinterpret_cast(ald); + if (isHSAILTarget(cs->Elf()->target)) { + bool bHsailTextInput = false; + const char *hsail_text_input = getenv("AMD_DEBUG_HSAIL_TEXT_INPUT"); + if (hsail_text_input != NULL && strcmp(hsail_text_input, "") != 0) { + bHsailTextInput = true; + if (bin && bin->options) { + amd::option::Options* Opts = reinterpret_cast(bin->options); + // Verify that the internal (blit) kernel is not being compiled + size_t ifind = Opts->origOptionStr.find("-cl-internal-kernel"); + if (ifind != std::string::npos) + bHsailTextInput = false; + } + } + if (!bHsailTextInput) + { + std::string* output = (std::string*) cl->cgAPI.codegen(ald, module, context, &error_code); + + if (error_code != ACL_SUCCESS) { + goto internal_compile_failure; + } + + amdcl::HSAIL *acl = reinterpret_cast(ald); + if (acl == NULL || !acl->insertBRIG(*output)) { + assert(!"Inserting BRIG failed\n"); + } + } + else + { + if (bHsailTextInput) { + static std::string sHsailFileNames; + if (sHsailFileNames.empty()) + sHsailFileNames = hsail_text_input; + std::string sCurHsailFileName; + size_t iFind = sHsailFileNames.find_first_not_of(";"); + if (iFind == std::string::npos) { + sCurHsailFileName = sHsailFileNames; + sHsailFileNames.clear(); + } + else { + size_t iFindEnd = sHsailFileNames.find_first_of(";", iFind+1); + size_t iCount = sHsailFileNames.size(); + if (iFindEnd == std::string::npos) { + sCurHsailFileName = sHsailFileNames.substr(iFind, iCount-iFind); + sHsailFileNames.clear(); + } + else { + sCurHsailFileName = sHsailFileNames.substr(iFind, iFindEnd-iFind); + sHsailFileNames = sHsailFileNames.substr(iFindEnd+1, iCount-iFindEnd-1); + } + } + size_t size = 0; + char * str = readFile(sCurHsailFileName.c_str(), size); + dataStr = (str == NULL) ? "" : str; + if (size == 0 || dataStr.length() == 0) { + const char* error = "ERROR: AMD_DEBUG_HSAIL_TEXT_INPUT file does not exist."; + appendLogToCL(cl, error); + error_code = ACL_ERROR; + goto internal_compile_failure; + } + // adding symHSAILText section to binary + // TODO_HSA: remove it after changing RT's hsaprogram.cpp to parse BRIG section + // for kernel names instead searching for "kernel &" in text HSAIL. + amdcl::CLCodeGen *aclCG = reinterpret_cast(ald); + if (checkFlag(&aclCG->Elf()->caps, capSaveCG)) { + const oclBIFSymbolStruct* symbol = findBIF30SymStruct(symHSAILText); + assert(symbol && "symbol not found"); + std::string name = symbol->str[PRE] + std::string("main") + + symbol->str[POST]; + aclCG->CL()->clAPI.insSym(aclCG->CL(), aclCG->Elf(), + dataStr.data(), dataStr.size(), aclCODEGEN, name.c_str()); + } + } + else { + const char *ptr = reinterpret_cast( + cl->cgAPI.codegen(ald, module, context, &error_code)); + if (error_code == ACL_SUCCESS) { + dataStr = ptr; + } + else { + goto internal_compile_failure; + } + } + // Use the assembler to generate the binary format of the IL string. + if (HSAILAssemble(ald, dataStr.c_str(), dataStr.length()) != ACL_SUCCESS) { + const char* error = "ASSEMBLER_FAILURE"; + appendLogToCL(cl, error); + error_code = ACL_ERROR; + goto internal_compile_failure; + } + } + bifbase *elfBin = reinterpret_cast(bin->bin); + elfBin->setType(ET_EXEC); + } else +#endif + { + const char *ptr = reinterpret_cast( + cl->cgAPI.codegen(ald, module, context, &error_code)); + if (error_code == ACL_SUCCESS) { + dataStr = ptr; + } + } + + cl->cgAPI.fini(ald); + if (error_code != ACL_SUCCESS) { + goto internal_compile_failure; + } + } + + // Convert the input string into the device ISA binary. + if (useISA) { + ald = cl->beAPI.init(cl, bin, compile_callback, &error_code); + error_code = cl->beAPI.finalize(ald, dataStr.data(), dataStr.length()); + cl->beAPI.fini(ald); + if (error_code != ACL_SUCCESS) { + goto internal_compile_failure; + } + } +internal_compile_failure: + if (module) { + delete reinterpret_cast(module); + } + + return error_code; +} +#define CONDITIONAL_ASSIGN(A, B) A = (A) ? (A) : (B) + +#define CONDITIONAL_CMP_ASSIGN(A, B, C) A = (A && B != A) ? (A) : (C) + + +acl_error ACL_API_ENTRY +if_aclCompile(aclCompiler *cl, + aclBinary *bin, + const char *options, + aclType from, + aclType to, + aclLogFunction compile_callback) +{ + acl_error error_code = ACL_SUCCESS; + if (bin == NULL || cl == NULL) { + return ACL_INVALID_ARG; + } +#ifdef WITH_TARGET_HSAIL + if (isHSAILTarget(bin->target)) { +#ifndef DEBUG + // Do not install signal handlers for the pretty stack trace. + llvm::DisablePrettyStackTrace = true; +#else + llvm::sys::PrintStackTraceOnErrorSignal(); +#endif + } else +#endif + { + llvm::InitializeAllAsmParsers(); + llvm::DisablePrettyStackTrace = true; + llvm::PassRegistry &Registry = *llvm::PassRegistry::getPassRegistry(); + llvm::initializeSPIRVerifierPass(Registry); + } + // Default 'to' is ISA. + if (to == ACL_TYPE_DEFAULT) { + to = ACL_TYPE_ISA; + } + if ((from == ACL_TYPE_X86_TEXT + || from == ACL_TYPE_X86_BINARY) + && (bin->target.arch_id != aclX86 + && bin->target.arch_id != aclX64)) { + return ACL_INVALID_BINARY; + } + + if ((from == ACL_TYPE_AMDIL_TEXT + || from == ACL_TYPE_AMDIL_BINARY) + && !isAMDILTarget(bin->target)) { + return ACL_INVALID_BINARY; + } + + if ((from == ACL_TYPE_HSAIL_TEXT + || from == ACL_TYPE_HSAIL_BINARY) + && bin->target.arch_id != aclHSAIL + && bin->target.arch_id != aclHSAIL64) { + return ACL_INVALID_BINARY; + } + if ((from == ACL_TYPE_HSAIL_TEXT && to == ACL_TYPE_HSAIL_BINARY) + || (from == ACL_TYPE_HSAIL_BINARY && to == ACL_TYPE_HSAIL_TEXT) + || (from == ACL_TYPE_AMDIL_TEXT && to == ACL_TYPE_AMDIL_BINARY) + || (from == ACL_TYPE_AMDIL_BINARY && to == ACL_TYPE_AMDIL_TEXT) + || (from == ACL_TYPE_SPIR_TEXT && to == ACL_TYPE_SPIR_BINARY) + || (from == ACL_TYPE_SPIR_BINARY && to == ACL_TYPE_SPIR_TEXT) + || (from == ACL_TYPE_LLVMIR_TEXT && to == ACL_TYPE_LLVMIR_BINARY) + || (from == ACL_TYPE_LLVMIR_BINARY && to == ACL_TYPE_LLVMIR_TEXT) + || (from == ACL_TYPE_X86_TEXT && to == ACL_TYPE_X86_BINARY) + || (from == ACL_TYPE_X86_BINARY && to == ACL_TYPE_X86_TEXT) + ) { + amd::option::Options* Opts = reinterpret_cast(bin->options); + const char *kernel = Opts->oVariables->Kernel; + return aclConvertType(cl, bin, kernel, from); + } + + if ((from == ACL_TYPE_AMDIL_TEXT + || from == ACL_TYPE_AMDIL_BINARY + || from == ACL_TYPE_HSAIL_TEXT + || from == ACL_TYPE_HSAIL_BINARY + || from == ACL_TYPE_X86_TEXT + || from == ACL_TYPE_X86_BINARY) + && to != ACL_TYPE_ISA) { + return ACL_INVALID_ARG; + } + + bool stages[5] = {false}; + uint8_t sectable[ACL_TYPE_LAST] = + { 0, 0, 1, 1, 1, 1, 0, 6, 0, 6, 4, 4, 4, 0, 5, 0, 1 }; + aclSections d_section[7] = + { aclSOURCE, aclLLVMIR, aclSPIR, aclSOURCE, aclCODEGEN, aclTEXT, aclINTERNAL }; + uint8_t start = sectable[from]; + uint8_t stop = sectable[to]; + const void* data = NULL; + size_t data_size = 0; + if (from == ACL_TYPE_DEFAULT) { + aclSections sections[] = { aclSOURCE, aclSPIR, aclLLVMIR, aclCODEGEN, aclTEXT }; + uint8_t table[] = { 0, 1, 1, 4, 5 }; + aclType type[] = { ACL_TYPE_SOURCE, ACL_TYPE_SPIR_BINARY, ACL_TYPE_LLVMIR_BINARY, + ACL_TYPE_CG, ACL_TYPE_ISA }; + for (int y = 0, x = sizeof(sections) / sizeof(sections[0]) - 1; + x >= y; --x) { + data = (const char*)cl->clAPI.extSec(cl, bin, &data_size, + sections[x], &error_code); + if (data != NULL && data_size > 0 && error_code == ACL_SUCCESS) { + start = table[x]; + from = type[x]; + break; + } + } + } else { + if (from == ACL_TYPE_SPIR_BINARY || + from == ACL_TYPE_SPIR_TEXT) { + data = cl->clAPI.extSec(cl, bin, &data_size, aclSPIR, &error_code); + } + else if (from == ACL_TYPE_RSLLVMIR_BINARY) { + data = cl->clAPI.extSec(cl, bin, &data_size, aclLLVMIR, &error_code); + } + else { + data = cl->clAPI.extSec(cl, bin, &data_size, d_section[start], &error_code); + } + } + if (error_code != ACL_SUCCESS) { + return error_code; + } + // Based on our compiler options, we need to change the functors to use + // the correct pointers unless they are custom loaded, then we should + // not modify them. This code is ugly and needs to be designed better. + if (start == 0) { + if (from == ACL_TYPE_OPENCL + || from == ACL_TYPE_SOURCE + || from == ACL_TYPE_DEFAULT) { + const oclBIFSymbolStruct* symbol + = findBIF30SymStruct(symOpenclCompilerOptions); + assert(symbol && "symbol not found"); + std::string optSec = std::string(symbol->str[PRE]) + std::string(symbol->str[POST]); + assert(symbol->sections[0] == aclCOMMENT + && symbol->sections[0] == symbol->sections[1] + && "not in comment section"); + saveOptionsToComments(cl, bin, options, optSec); + CONDITIONAL_CMP_ASSIGN(cl->feAPI.init, &SPIRInit, &OCLInit); + CONDITIONAL_CMP_ASSIGN(cl->feAPI.init, &AMDILInit, &OCLInit); + CONDITIONAL_CMP_ASSIGN(cl->feAPI.init, &HSAILFEInit, &OCLInit); + CONDITIONAL_CMP_ASSIGN(cl->feAPI.fini, &SPIRFini, &OCLFini); + CONDITIONAL_CMP_ASSIGN(cl->feAPI.fini, &AMDILFini, &OCLFini); + CONDITIONAL_CMP_ASSIGN(cl->feAPI.fini, &HSAILFEFini, &OCLFini); + CONDITIONAL_CMP_ASSIGN(cl->feAPI.toISA, &AMDILFEToISA, NULL); + CONDITIONAL_CMP_ASSIGN(cl->feAPI.toISA, &HSAILFEToISA, NULL); + if (to == ACL_TYPE_LLVMIR_BINARY + || to == ACL_TYPE_LLVMIR_TEXT) { + cl->feAPI.toISA = NULL; + cl->feAPI.toIR = &OCLFEToLLVMIR; + } else if(to == ACL_TYPE_SPIR_BINARY + || to == ACL_TYPE_SPIR_TEXT) { + cl->feAPI.toISA = NULL; + cl->feAPI.toIR = &OCLFEToSPIR; + } + } else if (from == ACL_TYPE_AMDIL_TEXT) { + amd::option::Options* Opts = reinterpret_cast(bin->options); + const char *kernel = Opts->oVariables->Kernel; + const oclBIFSymbolStruct* symbol + = findBIF30SymStruct(symAMDILCompilerOptions); + assert(symbol && "symbol not found"); + std::string optSec = std::string(symbol->str[PRE]) + + std::string((kernel == NULL) ? "main" : kernel) + + std::string(symbol->str[POST]); + assert(symbol->sections[0] == aclCOMMENT && "not in comment section"); + saveOptionsToComments(cl, bin, options, optSec); + if (to == ACL_TYPE_ISA || to == ACL_TYPE_DEFAULT) { + stop = 1; + cl->feAPI.init = &AMDILInit; + cl->feAPI.fini = &AMDILFini; + cl->feAPI.toISA = &AMDILFEToISA; + cl->feAPI.toIR = NULL; + cl->feAPI.toModule = NULL; + } else { + return ACL_UNSUPPORTED; + } + } else if (from == ACL_TYPE_HSAIL_TEXT) { + amd::option::Options* Opts = reinterpret_cast(bin->options); + const char *kernel = Opts->oVariables->Kernel; + const oclBIFSymbolStruct* symbol + = findBIF30SymStruct(symHSACompilerOptions); + assert(symbol && "symbol not found"); + std::string optSec = std::string(symbol->str[PRE]) + + std::string((kernel == NULL) ? "main" : kernel) + + std::string(symbol->str[POST]); + assert(symbol->sections[0] == aclCOMMENT && "not in comment section"); + saveOptionsToComments(cl, bin, options, optSec); + if (to == ACL_TYPE_ISA || to == ACL_TYPE_DEFAULT) { + stop = 1; + CONDITIONAL_CMP_ASSIGN(cl->feAPI.init, &OCLInit, &HSAILFEInit); + CONDITIONAL_CMP_ASSIGN(cl->feAPI.fini, &OCLFini, &HSAILFEFini); + CONDITIONAL_CMP_ASSIGN(cl->feAPI.toISA, &OCLFEToISA, &HSAILFEToISA); + cl->feAPI.toIR = NULL; + cl->feAPI.toModule = NULL; + } else { + return ACL_UNSUPPORTED; + } + } + } else if (start == 1) { + if ((from == ACL_TYPE_SPIR_BINARY || from == ACL_TYPE_SPIR_TEXT) && + (to == ACL_TYPE_LLVMIR_BINARY || to == ACL_TYPE_LLVMIR_TEXT)) { + CONDITIONAL_CMP_ASSIGN(cl->feAPI.init, &OCLInit, &SPIRInit); + CONDITIONAL_CMP_ASSIGN(cl->feAPI.init, &AMDILInit, &SPIRInit); + CONDITIONAL_CMP_ASSIGN(cl->feAPI.init, &HSAILFEInit, &SPIRInit); + CONDITIONAL_CMP_ASSIGN(cl->feAPI.fini, &OCLFini, &SPIRFini); + CONDITIONAL_CMP_ASSIGN(cl->feAPI.fini, &AMDILFini, &SPIRFini); + CONDITIONAL_CMP_ASSIGN(cl->feAPI.fini, &HSAILFEFini, &SPIRFini); + CONDITIONAL_CMP_ASSIGN(cl->feAPI.toModule, &OCLFEToModule, &SPIRToModule); + } else if (from == ACL_TYPE_LLVMIR_BINARY || from == ACL_TYPE_LLVMIR_TEXT || + from == ACL_TYPE_SPIR_BINARY || from == ACL_TYPE_SPIR_TEXT || + from == ACL_TYPE_RSLLVMIR_BINARY) { + CONDITIONAL_CMP_ASSIGN(cl->feAPI.init, &SPIRInit, &OCLInit); + CONDITIONAL_CMP_ASSIGN(cl->feAPI.init, &AMDILInit, &OCLInit); + CONDITIONAL_CMP_ASSIGN(cl->feAPI.init, &HSAILFEInit, &OCLInit); + CONDITIONAL_CMP_ASSIGN(cl->feAPI.fini, &SPIRFini, &OCLFini); + CONDITIONAL_CMP_ASSIGN(cl->feAPI.fini, &AMDILFini, &OCLFini); + CONDITIONAL_CMP_ASSIGN(cl->feAPI.fini, &HSAILFEFini, &OCLFini); + + if (from == ACL_TYPE_RSLLVMIR_BINARY) { + cl->feAPI.toModule = &RSLLVMIRToModule; + } + } + } + + if (start > stop) { + return ACL_INVALID_ARG; + } + if (start == stop) { + return ACL_SUCCESS; + } + for (uint8_t x = start; x < stop; ++x) { + stages[x] = true; + } + error_code = aclCompileInternal(cl, bin, + reinterpret_cast(data), + data_size, compile_callback, + stages[0], stages[1], stages[2], stages[3], stages[4]); + if (error_code == ACL_SUCCESS) { + return finalizeBinary(cl, bin); + } + return error_code; +} +#undef CONDITIONAL_ASSIGN +#undef CONDITIONAL_CMP_ASSIGN + +acl_error ACL_API_ENTRY +if_aclLink(aclCompiler *cl, + aclBinary *src_bin, + unsigned int num_libs, + aclBinary **libs, + aclType link_mode, + const char *options, + aclLogFunction link_callback) +{ + aclLoaderData *ald; + size_t data_size = 0; + aclModule *module = NULL, *dst_module = NULL; + llvm::LLVMContext myCtx; + aclContext *context = reinterpret_cast(&myCtx); + + acl_error error_code = ACL_SUCCESS; + aclModule **mod_libs = NULL; + if (num_libs > 0) { + mod_libs = new aclModule*[num_libs]; + memset(mod_libs, 0, num_libs * sizeof(*mod_libs)); + } + + switch(link_mode) { + default: error_code = ACL_UNSUPPORTED; break; + case ACL_TYPE_LLVMIR_BINARY: + case ACL_TYPE_RSLLVMIR_BINARY: + { + ald = cl->feAPI.init(cl, src_bin, link_callback, &error_code); + const void *ptr = cl->clAPI.extSec(cl, src_bin, &data_size, aclLLVMIR, &error_code); + if (ptr == NULL) + ptr = cl->clAPI.extSec(cl, src_bin, &data_size, aclSPIR, &error_code); + char *mod = new char[data_size]; + memcpy(mod, ptr, data_size); + module = cl->feAPI.toModule(ald, mod, data_size, context, &error_code); + for (unsigned x = 0; x < num_libs; ++x) { + const void *ptr = cl->clAPI.extSec(cl, libs[x], &data_size, aclLLVMIR, NULL); + if (ptr == NULL) + ptr = cl->clAPI.extSec(cl, libs[x], &data_size, aclSPIR, NULL); + if (ptr == NULL) continue; + mod = new char[data_size]; + memcpy(mod, ptr, data_size); + mod_libs[x] = cl->feAPI.toModule(ald, mod, data_size, context, &error_code); + } + cl->feAPI.fini(ald); + } + break; + } + if (error_code != ACL_SUCCESS) { + goto internal_link_failure; + } + ald = cl->linkAPI.init(cl, src_bin, link_callback, &error_code); + dst_module = cl->linkAPI.link(ald, module, num_libs, mod_libs, + context, &error_code); + cl->linkAPI.fini(ald); + if (error_code == ACL_SUCCESS) { + switch (link_mode) { + default: error_code = ACL_UNSUPPORTED; break; + case ACL_TYPE_LLVMIR_BINARY: + case ACL_TYPE_RSLLVMIR_BINARY: + { +#if 1 || LLVM_TRUNK_INTEGRATION_CL >= 7710 + llvm::SmallVector array; + llvm::raw_svector_ostream outstream(array); + llvm::WriteBitcodeToFile(reinterpret_cast(dst_module), outstream); + cl->clAPI.remSec(cl, src_bin, aclLLVMIR); + outstream.flush(); + error_code = cl->clAPI.insSec(cl, src_bin, + &array[0], array.size(), aclLLVMIR); +#else + std::vector array; + array.reserve(4096); + llvm::BitstreamWriter stream(array); + llvm::WriteBitcodeToStream(reinterpret_cast(dst_module), + stream); + cl->clAPI.remSec(cl, src_bin, aclLLVMIR); + error_code = cl->clAPI.insSec(cl, src_bin, + &array[0], array.size(), aclLLVMIR); +#endif + if (dst_module != NULL && dst_module != module) { + delete reinterpret_cast(dst_module); + } + } + bifbase *elfBin = reinterpret_cast(src_bin->bin); + elfBin->setType(ET_DYN); + break; + } + return finalizeBinary(cl, src_bin); + } +internal_link_failure: + const char *error = aclGetErrorString(error_code); + appendLogToCL(cl, error); + if (link_callback) { + link_callback(cl->buildLog, cl->logSize); + } + if (!error && module) { + delete reinterpret_cast(module); + } + if (mod_libs) { + for (unsigned x = 0; x < num_libs; ++x) { + if (!error && mod_libs[x]) { + delete reinterpret_cast(mod_libs[x]); + } + } + delete [] mod_libs; + } + return error_code; +} + +const char* ACL_API_ENTRY +if_aclGetCompilerLog(aclCompiler *cl) +{ + return (cl->buildLog == 0) ? "" : cl->buildLog; +} + +static std::string getSymbolName(aclType type, const char *name, aclSections &id) +{ + const oclBIFSymbolStruct* symbol = NULL; + uint8_t targetType = 0; + std::string tmpname(name); + std::string prefix = ""; + std::string postfix = ""; + switch (type) { + default: + assert(!"Invalid type detected!"); + return tmpname; + case ACL_TYPE_AMDIL_TEXT: + symbol = findBIF30SymStruct(symAMDILText); + assert(symbol && "symbol not found"); + break; + case ACL_TYPE_HSAIL_TEXT: + symbol = findBIF30SymStruct(symHSAILText); + assert(symbol && "symbol not found"); + break; + case ACL_TYPE_LLVMIR_TEXT: + id = aclLLVMIR; + break; + case ACL_TYPE_SPIR_TEXT: + id = aclSPIR; + break; + case ACL_TYPE_X86_TEXT: + id = aclCODEGEN; + break; + case ACL_TYPE_AMDIL_BINARY: + symbol = findBIF30SymStruct(symAMDILBinary); + assert(symbol && "symbol not found"); + break; + case ACL_TYPE_HSAIL_BINARY: + symbol = findBIF30SymStruct(symHSABinary); + assert(symbol && "symbol not found"); + break; + case ACL_TYPE_LLVMIR_BINARY: + id = aclLLVMIR; + break; + case ACL_TYPE_RSLLVMIR_BINARY: + id = aclLLVMIR; + break; + case ACL_TYPE_SPIR_BINARY: + id = aclSPIR; + break; + case ACL_TYPE_X86_BINARY: + id = aclCODEGEN; + break; + }; + if (symbol) { + prefix = symbol->str[PRE]; + postfix = symbol->str[POST]; + id = symbol->sections[0]; + } + return prefix + tmpname + postfix; +} + +const void* ACL_API_ENTRY +if_aclRetrieveType(aclCompiler *cl, + const aclBinary *bin, + const char *name, + size_t *data_size, + aclType type, + acl_error *error_code) +{ + aclSections sec_id; + std::string symbol_name = getSymbolName(type, name, sec_id); + return cl->clAPI.extSym(cl, bin, data_size, sec_id, symbol_name.c_str(), error_code); +} + +acl_error ACL_API_ENTRY +if_aclSetType(aclCompiler *cl, + aclBinary *bin, + const char *name, + aclType type, + const void *data, + size_t size) +{ + aclSections sec_id; + std::string symbol_name = getSymbolName(type, name, sec_id); + return cl->clAPI.insSym(cl, bin, data, size, sec_id, symbol_name.c_str()); +} + +acl_error ACL_API_ENTRY +if_aclConvertType(aclCompiler *cl, + aclBinary *bin, + const char *name, + aclType type) +{ + acl_error error_code = ACL_SUCCESS; + aclType to; + aclSections sec = aclSOURCE; + bool need_name = true; + size_t from_data_size = 0; + const void *from_data = NULL; + switch (type) { + default: + return ACL_UNSUPPORTED; + case ACL_TYPE_LLVMIR_TEXT: + to = ACL_TYPE_LLVMIR_BINARY; + need_name = false; + sec = aclLLVMIR; + break; + case ACL_TYPE_LLVMIR_BINARY: + to = ACL_TYPE_LLVMIR_TEXT; + need_name = false; + sec = aclLLVMIR; + break; + case ACL_TYPE_SPIR_TEXT: + to = ACL_TYPE_SPIR_BINARY; + need_name = false; + sec = aclSPIR; + break; + case ACL_TYPE_SPIR_BINARY: + to = ACL_TYPE_SPIR_TEXT; + need_name = false; + sec = aclSPIR; + break; + case ACL_TYPE_AMDIL_TEXT: + { + to = ACL_TYPE_AMDIL_BINARY; + // extract from symbol __debugil_text in .internal section + const oclBIFSymbolStruct* symbol = findBIF30SymStruct(symDebugilText); + assert(symbol && "symbol not found"); + std::string debugilSym + = std::string(symbol->str[PRE] + std::string(symbol->str[POST])); + from_data = cl->clAPI.extSym(cl, bin, &from_data_size, + symbol->sections[0], + debugilSym.c_str(), &error_code); + break; + } + case ACL_TYPE_AMDIL_BINARY: + { + to = ACL_TYPE_AMDIL_TEXT; + // extract from symbol __debugil_binary in .internal section + const oclBIFSymbolStruct* symbol = findBIF30SymStruct(symDebugilBinary); + assert(symbol && "symbol not found"); + std::string debugilSym + = std::string(symbol->str[PRE] + std::string(symbol->str[POST])); + from_data = cl->clAPI.extSym(cl, bin, &from_data_size, + symbol->sections[0], + debugilSym.c_str(), &error_code); + break; + } + case ACL_TYPE_HSAIL_TEXT: + to = ACL_TYPE_HSAIL_BINARY; + break; + case ACL_TYPE_HSAIL_BINARY: + to = ACL_TYPE_HSAIL_TEXT; + break; + case ACL_TYPE_X86_TEXT: + to = ACL_TYPE_X86_BINARY; + break; + case ACL_TYPE_X86_BINARY: + to = ACL_TYPE_X86_TEXT; + break; + } + if (from_data == NULL) { + if (name == NULL || !need_name) { + if (need_name) { + return ACL_INVALID_ARG; + } + from_data = cl->clAPI.extSec(cl, bin, + &from_data_size, sec, &error_code); + } else { + from_data = cl->clAPI.retrieveType(cl, bin, name, + &from_data_size, type, &error_code); + } + } + if (error_code != ACL_SUCCESS) { + return error_code; + } + const void *to_data = from_data; + size_t to_data_size = from_data_size; + switch (to) { + default: + return ACL_UNSUPPORTED; + case ACL_TYPE_SPIR_TEXT: + { + amdcl::SPIR *spir = new amdcl::SPIR(cl, bin, NULL); + llvm::LLVMContext myCtx; + aclContext *context = reinterpret_cast(&myCtx); + spir->setContext(context); + if (spir == NULL) { + return ACL_OUT_OF_MEM; + } + to_data = spir->toText(from_data, from_data_size, &to_data_size); + if (!spir->BuildLog().empty()) { + appendLogToCL(cl, spir->BuildLog()); + } + if (to_data == NULL) { + return ACL_INVALID_SPIR; + } + delete spir; + } + break; + case ACL_TYPE_SPIR_BINARY: + { + amdcl::SPIR *spir = new amdcl::SPIR(cl, bin, NULL); + llvm::LLVMContext myCtx; + aclContext *context = reinterpret_cast(&myCtx); + spir->setContext(context); + if (spir == NULL) { + return ACL_OUT_OF_MEM; + } + to_data = spir->toBinary(from_data, from_data_size, &to_data_size); + if (!spir->BuildLog().empty()) { + appendLogToCL(cl, spir->BuildLog()); + } + if (to_data == NULL) { + return ACL_INVALID_SPIR; + } + delete spir; + } + break; + case ACL_TYPE_AMDIL_TEXT: + { +#if defined(WITH_TARGET_AMDIL) + if (isAMDILTarget(bin->target)) { + amdcl::AMDIL *acl = new amdcl::AMDIL(cl, bin, NULL); + if (acl == NULL) { + return ACL_OUT_OF_MEM; + } + to_data = acl->toText(from_data, from_data_size); + to_data_size = strlen(reinterpret_cast(to_data)); + delete acl; + // insert into .internal section under symbol __debugil_text + const oclBIFSymbolStruct* symbol = findBIF30SymStruct(symDebugilText); + assert(symbol && "symbol not found"); + std::string debugilSym + = std::string(symbol->str[PRE] + std::string(symbol->str[POST])); + return cl->clAPI.insSym(cl, bin, to_data, to_data_size, + symbol->sections[0], debugilSym.c_str()); + } else { + assert(!"Unsupported architecture, expect amdil."); + return ACL_SYS_ERROR; + } +#else + assert(!"Cannot go down this path without AMDIL support!"); + return ACL_SYS_ERROR; +#endif + } + break; + case ACL_TYPE_AMDIL_BINARY: + { +#if defined(WITH_TARGET_AMDIL) + if (isAMDILTarget(bin->target)) { + amdcl::AMDIL *acl = new amdcl::AMDIL(cl, bin, NULL); + if (acl == NULL) { + return ACL_OUT_OF_MEM; + } + to_data = acl->toBinary(reinterpret_cast(from_data), + &to_data_size); + delete acl; + // insert into .internal section under symbol __debugil_binary + const oclBIFSymbolStruct* symbol = findBIF30SymStruct(symDebugilBinary); + assert(symbol && "symbol not found"); + std::string debugilSym + = std::string(symbol->str[PRE] + std::string(symbol->str[POST])); + return cl->clAPI.insSym(cl, bin, to_data, to_data_size, + symbol->sections[0], debugilSym.c_str()); + } else { + assert(!"Unsupported architecture, expect amdil."); + return ACL_SYS_ERROR; + } +#else + assert(!"Cannot go down this path without AMDIL support!"); + return ACL_SYS_ERROR; +#endif + } + break; + } + + if (name == NULL || !need_name) { + return cl->clAPI.insSec(cl, bin, to_data, to_data_size, sec); + } else { + return cl->clAPI.setType(cl, bin, name, to, to_data, to_data_size); + } +} + +acl_error ACL_API_ENTRY +if_aclDisassemble(aclCompiler *cl, + aclBinary *bin, + const char *kernel, + aclLogFunction disasm_callback) +{ + acl_error error_code = ACL_SUCCESS; + size_t size = 0; + const void *code = NULL; + aclLoaderData *data = cl->beAPI.init(cl, bin, disasm_callback, &error_code); + if (error_code != ACL_SUCCESS) { + goto internal_disasm_failure; + } + code = cl->clAPI.devBinary(cl, bin, kernel, &size, &error_code); + if (error_code != ACL_SUCCESS) { + goto internal_disasm_failure; + } + error_code = cl->beAPI.disassemble(data, kernel, code, size); + if (error_code != ACL_SUCCESS) { + goto internal_disasm_failure; + } +#ifdef WITH_TARGET_HSAIL + { + amdcl::CompilerStage *cs = reinterpret_cast(data); + if (isHSAILTarget(cs->Elf()->target)) { + amdcl::HSAIL *hsail_be = reinterpret_cast(data); + if (!hsail_be) { + goto internal_disasm_failure; + } + hsail_be->disassembleBRIG(cl, bin); + } + } +#endif + error_code = cl->beAPI.fini(data); + if (error_code != ACL_SUCCESS) { + goto internal_disasm_failure; + } + return error_code; +internal_disasm_failure: + const char *error = aclGetErrorString(error_code); + appendLogToCL(cl, error); + if (disasm_callback) { + disasm_callback(cl->buildLog, cl->logSize); + } + return error_code; +} + +const void* ACL_API_ENTRY +if_aclGetDeviceBinary(aclCompiler *cl, + const aclBinary *bin, + const char *kernel, + size_t *size, + acl_error *error_code) +{ + const oclBIFSymbolStruct* symbol = findBIF30SymStruct(symISABinary); + assert(symbol && "symbol not found"); + std::string kernelName = symbol->str[PRE] + std::string(kernel) + symbol->str[POST]; + return cl->clAPI.extSym(cl, bin, size, + symbol->sections[0], kernelName.c_str(), error_code); +} + +acl_error ACL_API_ENTRY +if_aclInsertSection(aclCompiler *cl, + aclBinary *binary, + const void *data, + size_t data_size, + aclSections id) +{ + bifbase *elfBin = reinterpret_cast(binary->bin); + if (!elfBin) { + return ACL_ELF_ERROR; + } + if (!elfBin->addSection(id, data, data_size)) { + return ACL_ELF_ERROR; + } + return ACL_SUCCESS; + +} + +acl_error ACL_API_ENTRY +if_aclInsertSymbol(aclCompiler *cl, + aclBinary *binary, + const void *data, + size_t data_size, + aclSections id, + const char *symbol) +{ + bifbase *elfBin = reinterpret_cast(binary->bin); + if (!elfBin) { + return ACL_ELF_ERROR; + } + if (!elfBin->addSymbol(id, symbol, + reinterpret_cast(data), data_size)) { + return ACL_ELF_ERROR; + } + return ACL_SUCCESS; + +} + +const void* ACL_API_ENTRY +if_aclExtractSection(aclCompiler *cl, + const aclBinary *binary, + size_t *size, + aclSections id, + acl_error *error_code) +{ + bifbase *elfBin = reinterpret_cast(binary->bin); + if (!elfBin) { + if (error_code) (*error_code) = ACL_ELF_ERROR; + return NULL; + } + const void* a = elfBin->getSection(id, size); + if (a == NULL) { + if (error_code) (*error_code) = ACL_ELF_ERROR; + return NULL; + } + if (error_code) (*error_code) = ACL_SUCCESS; + return a; + +} + +const void* ACL_API_ENTRY +if_aclExtractSymbol(aclCompiler *cl, + const aclBinary *binary, + size_t *size, + aclSections id, + const char *symbol, + acl_error *error_code) +{ + bifbase *elfBin = reinterpret_cast(binary->bin); + if (!elfBin) { + if (error_code) (*error_code) = ACL_ELF_ERROR; + return NULL; + } + const void* a = elfBin->getSymbol(id, symbol, size); + if (a == NULL) { + if (error_code) (*error_code) = ACL_ELF_ERROR; + return NULL; + } + if (error_code) (*error_code) = ACL_SUCCESS; + return a; + +} + +acl_error ACL_API_ENTRY +if_aclRemoveSection(aclCompiler *cl, + aclBinary *binary, + aclSections id) +{ + bifbase *elfBin = reinterpret_cast(binary->bin); + if (!elfBin) { + return ACL_ELF_ERROR; + } + return elfBin->removeSection(id) ? ACL_SUCCESS : ACL_ELF_ERROR; +} + +acl_error ACL_API_ENTRY +if_aclRemoveSymbol(aclCompiler *cl, + aclBinary *binary, + aclSections id, + const char *symbol) +{ + bifbase *elfBin = reinterpret_cast(binary->bin); + if (!elfBin) { + return ACL_ELF_ERROR; + } + return elfBin->removeSymbol(id, symbol) ? ACL_SUCCESS : ACL_ELF_ERROR; +} + +// Function performs deserialization of aclMetadata into *md +// instead of changing source .rodata section in memory pointed by *ptr. +// Deserialization includes restoring of pointers, whereas +// serialized .rodata has pointers set to NULL by serializeMetadata function. +// We should leave serialized metaData unchanged (e.g. w/o garbage pointers) +// due to obtain the same binary from one compilation to another. +// Otherwise, OpenCL conformance "binary_create" test would fail on comparison +// of OpenCL "binaries" (bifs in our case). +void deserializeCLMetadata(const char* ptr, aclMetadata * const md, const size_t size) +{ + memcpy(md,ptr,size); + char *tmp_ptr = reinterpret_cast(md); + tmp_ptr += md->struct_size; + // de-serialize the kernel name + md->kernelName = tmp_ptr; + tmp_ptr += md->kernelNameSize + 1; + + // de-serialize the device name + md->deviceName = tmp_ptr; + tmp_ptr += md->deviceNameSize + 1; + + // de-serailize the arguments + md->args = reinterpret_cast(tmp_ptr); + tmp_ptr += (md->numArgs + 1) * sizeof(aclArgData); + + for (unsigned x = 0; x < md->numArgs; ++x) { + // Get a pointer to the structure + aclArgData *argPtr = md->args + x; + + // de-serialize the argument name string + argPtr->argStr = tmp_ptr; + tmp_ptr += argPtr->argNameSize + 1; + + // de-serialize the argument type string + argPtr->typeStr = tmp_ptr; + tmp_ptr += argPtr->typeStrSize + 1; + } + + // de-serialize the printf strings + md->printf = reinterpret_cast(tmp_ptr); + tmp_ptr += sizeof(aclPrintfFmt) * (md->numPrintf + 1); + for (unsigned x = 0; x < md->numPrintf; ++x) { + // Get a pointer to the printf structure + aclPrintfFmt *fmtPtr = md->printf + x; + + // de-serialize the arguments + fmtPtr->argSizes = const_cast(reinterpret_cast(tmp_ptr)); + tmp_ptr += sizeof(uint32_t) * fmtPtr->numSizes; + + // de-serialize the format string + fmtPtr->fmtStr = tmp_ptr; + tmp_ptr += fmtPtr->fmtStrSize + 1; + } + assert(md->data_size == size && "The size and data size calculations are off!"); + assert((size_t)(tmp_ptr - reinterpret_cast(md)) + == size && "Size of data and calculated sizes differ!"); +} + +acl_error ACL_API_ENTRY +if_aclQueryInfo(aclCompiler *cl, + const aclBinary *binary, + aclQueryType query, + const char *kernel, + void *ptr, + size_t *size) +{ + const oclBIFSymbolStruct* sym = findBIF30SymStruct(symOpenclMeta); + assert(sym && "symbol not found"); + std::string symbol = sym->str[PRE] + std::string(kernel) + sym->str[POST]; + size_t roSize; + acl_error error_code; + const void* roSec = cl->clAPI.extSym(cl, binary, &roSize, + sym->sections[0], symbol.c_str(), &error_code); + if (error_code != ACL_SUCCESS) return error_code; + if (roSec == NULL || roSize == 0) { + return ACL_ELF_ERROR; + } + bool success = true; + + switch (query) { + default: break; + case RT_CPU_BARRIER_NAMES: + if (size != NULL && ptr == NULL) { + (*size) = 0; + } else if (ptr != NULL && size != NULL) { + assert(!"Not implemented!"); + success = false; + } else { + success = false; + } + break; + case RT_ABI_VERSION: + if (size != NULL && ptr == NULL) { + (*size) = sizeof(uint32_t) * 3; + } else if (ptr != NULL && (*size) >= (sizeof(uint32_t) * 3)) { + uint32_t *tmp = reinterpret_cast(ptr); + const aclMetadata *md = reinterpret_cast(roSec); + tmp[0] = md->major; + tmp[1] = md->minor; + tmp[2] = md->revision; + } else { + success = false; + } + break; + case RT_DEVICE_NAME: + if (size != NULL && ptr == NULL) { + const aclMetadata *md = reinterpret_cast(roSec); + (*size) = md->deviceNameSize; + } else if (ptr != NULL) { + const aclMetadata *md = reinterpret_cast(roSec); + success = false; + if ((*size) >= md->deviceNameSize) { + strncpy(reinterpret_cast(ptr), reinterpret_cast(roSec) + + md->struct_size + md->kernelNameSize + 1, md->deviceNameSize); + success = true; + } + } else { + success = false; + } + break; + case RT_MEM_SIZES: + if (size != NULL && ptr == NULL) { + (*size) = sizeof(size_t) * RT_MEM_LAST; + } else if (ptr != NULL && (*size) >= (sizeof(size_t) * RT_MEM_LAST)) { + size_t *tmp = reinterpret_cast(ptr); + const aclMetadata *md = reinterpret_cast(roSec); + memcpy(tmp, md->mem, sizeof(size_t) * RT_MEM_LAST); + } else { + success = false; + } + break; + case RT_GPU_FUNC_CAPS: + if (binary->target.arch_id == aclX86) success = false; + if (size != NULL && ptr == NULL) { + (*size) = sizeof(uint32_t); + } else if (ptr != NULL && (*size) >= sizeof(uint32_t)) { + const aclMetadata *md = reinterpret_cast(roSec); + (*reinterpret_cast(ptr)) = md->gpuCaps; + } else { + success = false; + } + break; + case RT_GPU_FUNC_ID: + if (binary->target.arch_id == aclX86) success = false; + if (size != NULL && ptr == NULL) { + (*size) = sizeof(uint32_t); + } else if (ptr != NULL && (*size) >= sizeof(uint32_t)) { + const aclMetadata *md = reinterpret_cast(roSec); + (*reinterpret_cast(ptr)) = md->funcID; + } else { + success = false; + } + break; + case RT_GPU_DEFAULT_ID: + if (binary->target.arch_id == aclX86) success = false; + if (size != NULL && ptr == NULL) { + (*size) = sizeof(uint32_t) * RT_RES_LAST; + } else if (ptr != NULL && (*size) >= (sizeof(uint32_t) * RT_RES_LAST)) { + uint32_t *tmp = reinterpret_cast(ptr); + const aclMetadata *md = reinterpret_cast(roSec); + memcpy(tmp, md->gpuRes, sizeof(uint32_t) * RT_RES_LAST); + } else { + success = false; + } + break; + case RT_WORK_GROUP_SIZE: + if (size != NULL && ptr == NULL) { + (*size) = sizeof(size_t) * 3; + } else if (ptr != NULL && (*size) >= (sizeof(size_t) * 3)) { + size_t *tmp = reinterpret_cast(ptr); + const aclMetadata *md = reinterpret_cast(roSec); + memcpy(tmp, md->wgs, 3 * sizeof(size_t)); + } else { + success = false; + } + break; + case RT_WORK_REGION_SIZE: + if (size != NULL && ptr == NULL) { + (*size) = sizeof(uint32_t) * 3; + } else if (ptr != NULL && (*size) >= (sizeof(uint32_t) * 3)) { + uint32_t *tmp = reinterpret_cast(ptr); + const aclMetadata *md = reinterpret_cast(roSec); + memcpy(tmp, md->wrs, 3 * sizeof(uint32_t)); + } else { + success = false; + } + break; + case RT_ARGUMENT_ARRAY: { + aclMetadata *md = static_cast(malloc(roSize)); + if (size != NULL && ptr == NULL) { + deserializeCLMetadata(reinterpret_cast(roSec), md, roSize); + (*size) = sizeof(aclArgData) * (md->numArgs + 1); + for (unsigned x = 0; x < md->numArgs; ++x) { + (*size) += md->args[x].typeStrSize + md->args[x].argNameSize + 2; + } + } else if (ptr) { + deserializeCLMetadata(reinterpret_cast(roSec), md, roSize); + unsigned totSize = sizeof(aclArgData) * (md->numArgs + 1); + for (unsigned x = 0; x < md->numArgs; ++x) { + totSize += md->args[x].typeStrSize + md->args[x].argNameSize + 2; + } + if ((*size) >= totSize) { + char *tmp = reinterpret_cast(ptr); + memset(ptr, 0, (*size)); + memcpy(ptr, md->args, sizeof(aclArgData) * (md->numArgs + 1)); + tmp += (sizeof(aclArgData) * (md->numArgs + 1)); + for (unsigned x = 0; x < md->numArgs; ++x) { + memcpy(tmp, md->args[x].argStr, md->args[x].argNameSize); + reinterpret_cast(ptr)[x].argStr = tmp; + tmp += md->args[x].argNameSize + 1; + tmp[-1] = '\0'; + memcpy(tmp, md->args[x].typeStr, md->args[x].typeStrSize); + reinterpret_cast(ptr)[x].typeStr = tmp; + tmp += md->args[x].typeStrSize + 1; + tmp[-1] = '\0'; + } + } else { + success = false; + } + } else { + success = false; + } + free(md); + break; + } + case RT_GPU_PRINTF_ARRAY: { + aclMetadata *md = static_cast(malloc(roSize)); + if (size != NULL && ptr == NULL) { + deserializeCLMetadata(reinterpret_cast(roSec), md, roSize); + (*size) = 0; + if (md->numPrintf > 0) { + (*size) = sizeof(aclPrintfFmt) * (md->numPrintf + 1); + for (unsigned x = 0; x < md->numPrintf; ++x) { + (*size) += sizeof(uint32_t) * md->printf[x].numSizes; + (*size) += md->printf[x].fmtStrSize + 1; + } + } + } else if (ptr != NULL) { + deserializeCLMetadata(reinterpret_cast(roSec), md, roSize); + unsigned totSize = sizeof(aclPrintfFmt) * (md->numPrintf + 1); + for (unsigned x = 0; x < md->numPrintf; ++ x) { + totSize += sizeof(uint32_t) + md->printf[x].fmtStrSize + 1; + } + if ((*size) >= totSize) { + char *tmp = reinterpret_cast(ptr); + memcpy(ptr, md->printf, sizeof(aclPrintfFmt) * (md->numPrintf + 1)); + tmp += (sizeof(aclPrintfFmt) * (md->numPrintf + 1)); + for (unsigned x = 0; x < md->numPrintf; ++x) { + memcpy(tmp, md->printf[x].argSizes, sizeof(uint32_t) * md->printf[x].numSizes); + reinterpret_cast(ptr)[x].argSizes = reinterpret_cast(tmp); + tmp += sizeof(uint32_t) * md->printf[x].numSizes; + memcpy(tmp, md->printf[x].fmtStr, md->printf[x].fmtStrSize); + reinterpret_cast(ptr)[x].fmtStr = tmp; + tmp += md->printf[x].fmtStrSize + 1; + tmp[-1] = '\0'; + } + } else { + success = false; + } + } else { + success = false; + } + free(md); + break; + } + case RT_DEVICE_ENQUEUE: { + if (size != NULL && ptr == NULL) { + (*size) = sizeof(uint32_t); + } else if (ptr != NULL && (*size) >= (sizeof(uint32_t))) { + const aclMetadata *md = reinterpret_cast(roSec); + (*reinterpret_cast(ptr)) = md->enqueue_kernel; + } else { + success = false; + } + break; + } + // Temporary approach till the "ldk" instruction is supported. + case RT_KERNEL_INDEX: { + if (size != NULL && ptr == NULL) { + (*size) = sizeof(uint32_t); + } else if (ptr != NULL && (*size) >= (sizeof(uint32_t))) { + const aclMetadata *md = reinterpret_cast(roSec); + (*reinterpret_cast(ptr)) = md->kernel_index; + } else { + success = false; + } + break; + } + } + return (success) ? ACL_SUCCESS : ACL_ERROR; +} +static unsigned getSize(aclArgDataType data) +{ + switch(data) { + default: + return 4; + case DATATYPE_i64: + case DATATYPE_u64: + case DATATYPE_f64: + return 8; + case DATATYPE_f80: + case DATATYPE_f128: + return 16; + } + return 4; +} +acl_error ACL_API_ENTRY +if_aclDbgAddArgument(aclCompiler *cl, + aclBinary *bin, + const char *kernel, + const char *name, + bool byVal) +{ + if (!isAMDILTarget(bin->target)) { + return ACL_UNSUPPORTED; + } + const oclBIFSymbolStruct* sym = findBIF30SymStruct(symOpenclMeta); + assert(sym && "symbol not found"); + std::string symbol = sym->str[PRE] + std::string(kernel) + sym->str[POST]; + size_t roSize; + acl_error error_code; + aclMetadata *md = NULL; + { + const char* roSec = reinterpret_cast(cl->clAPI.extSym( + cl, bin, &roSize, sym->sections[0], symbol.c_str(), &error_code)); + if (error_code != ACL_SUCCESS) return error_code; + if (roSec == NULL || roSize == 0) { + return ACL_ELF_ERROR; + } + md = static_cast(malloc(roSize)); + if (md == NULL) return ACL_OUT_OF_MEM; + deserializeCLMetadata(roSec, md, roSize); + } + std::string dbg_name = name; + size_t newSize = roSize + sizeof(aclArgData) + dbg_name.size() + 9; + char *newMDptr = new char[newSize]; + char *tmp_ptr = newMDptr; + memset(newMDptr, 0, newSize); + aclMetadata *newMD = reinterpret_cast(newMDptr); + memcpy(tmp_ptr, md, md->struct_size + + (md->kernelNameSize + 1) + + (md->deviceNameSize + 1)); + tmp_ptr += md->struct_size; + tmp_ptr += md->kernelNameSize + 1; + tmp_ptr[-1] = '\0'; + tmp_ptr += md->deviceNameSize + 1; + tmp_ptr[-1] = '\0'; + newMD->args = reinterpret_cast(tmp_ptr); + unsigned cb_offset = 0; + const aclArgData *c_argPtr = reinterpret_cast( + reinterpret_cast(md) + (tmp_ptr - newMDptr)); + for (unsigned x = 0; x < md->numArgs; ++x) { + switch (c_argPtr[x].type) { + default: + case ARG_TYPE_ERROR: + assert(!"Unknown type!"); + break; + case ARG_TYPE_SAMPLER: + break; + case ARG_TYPE_COUNTER: + if (c_argPtr[x].arg.counter.cbOffset >= cb_offset) { + cb_offset = c_argPtr[x].arg.counter.cbOffset + 16; + } + break; + case ARG_TYPE_POINTER: + if (c_argPtr[x].arg.pointer.cbOffset >= cb_offset) { + cb_offset = c_argPtr[x].arg.pointer.cbOffset + 16; + } + break; + case ARG_TYPE_SEMAPHORE: + if (c_argPtr[x].arg.sema.cbOffset >= cb_offset) { + cb_offset = c_argPtr[x].arg.sema.cbOffset + 16; + } + break; + case ARG_TYPE_IMAGE: + if (c_argPtr[x].arg.image.cbOffset >= cb_offset) { + cb_offset = c_argPtr[x].arg.image.cbOffset + 16; + } + break; + case ARG_TYPE_VALUE: + if (c_argPtr[x].arg.value.cbOffset >= cb_offset) { + unsigned offs = c_argPtr[x].arg.value.numElements * getSize(c_argPtr[x].arg.value.data); + cb_offset = c_argPtr[x].arg.value.cbOffset + (offs > 16 ? offs : 16); + } + break; + } + size_t arg_size = c_argPtr[x].struct_size; + memcpy(tmp_ptr, &c_argPtr[x], arg_size); + tmp_ptr += arg_size; + } + // Skip the new one and the sentinal one. + tmp_ptr += (sizeof(aclArgData) * 2); + // Copy all of the name/type strings. + for (unsigned x = 0; x < md->numArgs; ++x) { + memcpy(tmp_ptr, md->args[x].argStr, md->args[x].argNameSize); + tmp_ptr += md->args[x].argNameSize + 1; + tmp_ptr[-1] = '\0'; + memcpy(tmp_ptr, md->args[x].typeStr, md->args[x].typeStrSize); + tmp_ptr += md->args[x].typeStrSize + 1; + tmp_ptr[-1] = '\0'; + } + size_t printf_offset = reinterpret_cast(md->printf) + - reinterpret_cast(md); + aclArgData *argPtr = &newMD->args[newMD->numArgs]; + newMD->numArgs++; + if (byVal) { + argPtr->type = ARG_TYPE_VALUE; + argPtr->arg.value.data = DATATYPE_u32; + argPtr->arg.value.numElements = 4; + argPtr->arg.value.cbNum = 2; + argPtr->arg.value.cbOffset = cb_offset; + } else { + argPtr->type = ARG_TYPE_POINTER; + argPtr->arg.pointer.data = DATATYPE_u32; + argPtr->arg.pointer.numElements = 1; + argPtr->arg.pointer.cbNum = 2; + argPtr->arg.pointer.cbOffset = cb_offset; + argPtr->arg.pointer.memory = PTR_MT_GLOBAL; + argPtr->arg.pointer.bufNum = md->gpuRes[RT_RES_UAV]; + argPtr->arg.pointer.align = 4; + argPtr->arg.pointer.type = ACCESS_TYPE_RW; + argPtr->arg.pointer.isVolatile = false; + argPtr->arg.pointer.isRestrict = false; + } + argPtr->argNameSize = dbg_name.size() + 7; + argPtr->typeStrSize = 0; + argPtr->typeStr = ""; + argPtr->isConst = false; + argPtr->struct_size = sizeof(aclArgData); + argPtr->argStr = tmp_ptr; + memcpy(tmp_ptr, "_debug_", 7); + tmp_ptr += 7; + memcpy(tmp_ptr, dbg_name.data(), dbg_name.size()); + tmp_ptr += dbg_name.size() + 1; + tmp_ptr[-1] = '\0'; + memcpy(tmp_ptr, argPtr->typeStr, argPtr->typeStrSize); + tmp_ptr += argPtr->typeStrSize + 1; + tmp_ptr[-1] = '\0'; + newMD->printf = reinterpret_cast(tmp_ptr); + newMD->data_size = newSize; + memcpy(tmp_ptr, reinterpret_cast(md) + printf_offset, roSize - printf_offset); + tmp_ptr += (roSize - printf_offset); + cl->clAPI.remSym(cl, bin, aclRODATA, symbol.c_str()); + error_code = cl->clAPI.insSym(cl, bin, newMDptr, newSize, + aclRODATA, symbol.c_str()); + assert((size_t)(tmp_ptr - newMDptr) == newSize && "allocated memory does not equal the amount of memory copied!"); + free(md); + delete [] newMDptr; + return error_code; +} + +acl_error ACL_API_ENTRY +if_aclDbgRemoveArgument(aclCompiler *cl, + aclBinary *bin, + const char* kernel, + const char* name) +{ + if (!isAMDILTarget(bin->target)) { + return ACL_UNSUPPORTED; + } + const oclBIFSymbolStruct* sym = findBIF30SymStruct(symOpenclMeta); + assert(sym && "symbol not found"); + std::string symbol = sym->str[PRE] + std::string(kernel) + sym->str[POST]; + size_t roSize; + acl_error error_code; + aclMetadata *md = NULL; + { + const char* roSec = reinterpret_cast(cl->clAPI.extSym(cl, bin, &roSize, + sym->sections[0], symbol.c_str(), &error_code)); + if (error_code != ACL_SUCCESS) return error_code; + if (roSec == NULL || roSize == 0) { + return ACL_ELF_ERROR; + } + md = static_cast(malloc(roSize)); + if (md == NULL) return ACL_OUT_OF_MEM; + deserializeCLMetadata(roSec, md, roSize); + } + const char* ro_ptr = reinterpret_cast(md); + ro_ptr += md->struct_size; + ro_ptr += md->kernelNameSize + 1; + ro_ptr += md->deviceNameSize + 1; + const aclArgData *argPtr = reinterpret_cast(ro_ptr); + const aclArgData *delArg = 0; + for (unsigned x = 0; x < md->numArgs; ++x) { + if (0 != argPtr[x].argStr + && !strncmp("_debug_", argPtr[x].argStr, 7) + && !strcmp(name, argPtr[x].argStr + 7)) { + delArg = &argPtr[x]; + break; + } + } + if (0 == delArg) { + return ACL_INVALID_ARG; + } + size_t newSize = roSize - (delArg->struct_size + delArg->argNameSize + delArg->typeStrSize + 2); + char *newMDptr = new char[newSize]; + memset(newMDptr, 0, newSize); + aclMetadata *newMD = reinterpret_cast(newMDptr); + char *tmp_ptr = newMDptr; + memcpy(tmp_ptr, reinterpret_cast(md), md->struct_size + + (md->kernelNameSize + 1) + + (md->deviceNameSize + 1)); + tmp_ptr += md->struct_size; + tmp_ptr += md->kernelNameSize + 1; + tmp_ptr[-1] = '\0'; + tmp_ptr += md->deviceNameSize + 1; + tmp_ptr[-1] = '\0'; + unsigned cb_offset = ((delArg->type == ARG_TYPE_VALUE) + ? delArg->arg.value.cbOffset : delArg->arg.pointer.cbOffset); + size_t printf_offset = reinterpret_cast(md->printf) + - reinterpret_cast(md); + newMD->numArgs--; + for (unsigned x = 0; x < md->numArgs; ++x) { + size_t arg_size = argPtr[x].struct_size; + if (strcmp(argPtr[x].argStr, delArg->argStr)) { + memcpy(tmp_ptr, &argPtr[x], arg_size); + aclArgData *tmpArg = reinterpret_cast(tmp_ptr); + tmp_ptr += arg_size; + switch (argPtr[x].type) { + default: + case ARG_TYPE_ERROR: + assert(!"Unknown type!"); + break; + case ARG_TYPE_SAMPLER: + break; + case ARG_TYPE_COUNTER: + if (tmpArg->arg.counter.cbOffset >= cb_offset) { + tmpArg->arg.counter.cbOffset -= 16; + } + break; + case ARG_TYPE_POINTER: + if (tmpArg->arg.pointer.cbOffset >= cb_offset) { + tmpArg->arg.pointer.cbOffset -= 16; + } + break; + case ARG_TYPE_SEMAPHORE: + if (tmpArg->arg.sema.cbOffset >= cb_offset) { + tmpArg->arg.sema.cbOffset -= 16; + } + break; + case ARG_TYPE_IMAGE: + if (tmpArg->arg.image.cbOffset >= cb_offset) { + tmpArg->arg.image.cbOffset -= 16; + } + break; + case ARG_TYPE_VALUE: + if (tmpArg->arg.value.cbOffset >= cb_offset) { + tmpArg->arg.value.cbOffset -= 16; + } + break; + } + } + } + memset(tmp_ptr, 0, delArg->struct_size); + tmp_ptr += delArg->struct_size; + for (unsigned x = 0; x < md->numArgs; ++x) { + size_t arg_size = argPtr[x].struct_size; + if (strcmp(argPtr[x].argStr, delArg->argStr)) { + memcpy(tmp_ptr, argPtr[x].argStr, argPtr[x].argNameSize); + tmp_ptr += argPtr[x].argNameSize + 1; + tmp_ptr[-1] = '\0'; + memcpy(tmp_ptr, argPtr[x].typeStr, argPtr[x].typeStrSize); + tmp_ptr += argPtr[x].typeStrSize + 1; + tmp_ptr[-1] = '\0'; + } + } + + memcpy(tmp_ptr, reinterpret_cast(md) + printf_offset, roSize - printf_offset); + tmp_ptr += (roSize - printf_offset); + newMD->data_size = newSize; + cl->clAPI.remSym(cl, bin, aclRODATA, symbol.c_str()); + error_code = cl->clAPI.insSym(cl, bin, newMDptr, newSize, + aclRODATA, symbol.c_str()); + assert((size_t)(tmp_ptr - newMDptr) == newSize && "allocated memory does not equal the amount of memory copied!"); + free(md); + delete [] newMDptr; + return error_code; +} + +void myLogFunc(const char * msg, size_t size) +{ + printf("%s\n", msg); +} + +extern "C" { +bool aclRenderscriptCompile( + char * srcFile, + char ** outBuf, + size_t * outLen +) +{ +#if 0 + // Consider using code here if aoc2 is not used. + llvm::Module *bc = NULL; + llvm::LLVMContext &Context = llvm::getGlobalContext(); + llvm::SMDiagnostic Err; + std::string Str(srcFile); + + bc = llvm::ParseIRFile(Str, Err, Context); + if (!bc) + return false; + + llvm::PassManager TransformPasses; + TransformPasses.add(llvm::createOpenCLIRTransform()); + TransformPasses.run(*bc); +#endif + + size_t size = 0; + acl_error error_code; + char * source = readFile(srcFile, size); + if (!size) + return false; + + aclCompiler *aoc = aclCompilerInit(NULL, &error_code); + if ((aoc == NULL) || (error_code != ACL_SUCCESS)) + return false; + + aclTargetInfo target = aclGetTargetInfo("hsail", "Bonaire", &error_code); + if (error_code != ACL_SUCCESS) + return false; + + aclBinary *aoe = aclBinaryInit(sizeof(aclBinary), &target, NULL, &error_code); + if (error_code != ACL_SUCCESS) + return false; + + error_code = aclInsertSection(aoc, aoe, source, size, aclLLVMIR); + if (error_code != ACL_SUCCESS) + return false; + +#if 1 + // Dump HSAIL and ISA to a temporary file in the working directory. + error_code = aclCompile(aoc, aoe, "-save-temps=tmp", ACL_TYPE_RSLLVMIR_BINARY, ACL_TYPE_ISA, myLogFunc); +#else + error_code = aclCompile(aoc, aoe, NULL, ACL_TYPE_RSLLVMIR_BINARY, ACL_TYPE_ISA, myLogFunc); +#endif + + if (error_code == ACL_FRONTEND_FAILURE) { + printf("ACL_FRONTEND_FAILURE.\n"); + return true; + } + + if (error_code != ACL_SUCCESS) + return false; + + if ((aoe == NULL) || (aoe->bin == NULL)) + return false; + + char *buffer = NULL; + size_t len; + acl_error errCode = aclWriteToMem(aoe, reinterpret_cast(&buffer), &len); + if (errCode != ACL_SUCCESS) + return false; + + *outLen = len; + *outBuf = buffer; + return true; +} +} diff --git a/rocclr/compiler/lib/backends/common/v0_8/if_acl.h b/rocclr/compiler/lib/backends/common/v0_8/if_acl.h new file mode 100644 index 0000000000..135e3c1e73 --- /dev/null +++ b/rocclr/compiler/lib/backends/common/v0_8/if_acl.h @@ -0,0 +1,129 @@ +// +// Copyright (c) 2012 Advanced Micro Devices, Inc. All rights reserved. +// +#ifndef _IF_ACL_0_8_H_ +#define _IF_ACL_0_8_H_ +#include "aclTypes.h" +aclLoaderData* ACL_API_ENTRY +if_aclCompilerInit(aclCompiler *cl, aclBinary *bin, aclLogFunction log, acl_error *error); +acl_error ACL_API_ENTRY +if_aclCompilerFini(aclLoaderData *ald); + +acl_error ACL_API_ENTRY +if_aclCompile(aclCompiler *cl, + aclBinary *bin, + const char *options, + aclType from, + aclType to, + aclLogFunction compile_callback) ACL_API_0_8; + +acl_error ACL_API_ENTRY +if_aclLink(aclCompiler *cl, + aclBinary *src_bin, + unsigned int num_libs, + aclBinary **libs, + aclType link_mode, + const char *options, + aclLogFunction link_callback) ACL_API_0_8; + +const char* ACL_API_ENTRY +if_aclGetCompilerLog(aclCompiler *cl) ACL_API_0_8; + +const void* ACL_API_ENTRY +if_aclRetrieveType(aclCompiler *cl, + const aclBinary *bin, + const char *name, + size_t *data_size, + aclType type, + acl_error *error_code) ACL_API_0_8; + +acl_error ACL_API_ENTRY +if_aclSetType(aclCompiler *cl, + aclBinary *bin, + const char *name, + aclType type, + const void *data, + size_t size) ACL_API_0_8; + +acl_error ACL_API_ENTRY +if_aclConvertType(aclCompiler *cl, + aclBinary *bin, + const char *name, + aclType type) ACL_API_0_8; + +acl_error ACL_API_ENTRY +if_aclDisassemble(aclCompiler *cl, + aclBinary *bin, + const char *kernel, + aclLogFunction disasm_callback) ACL_API_0_8; + +const void* ACL_API_ENTRY +if_aclGetDeviceBinary(aclCompiler *cl, + const aclBinary *bin, + const char *kernel, + size_t *size, + acl_error *error_code) ACL_API_0_8; + +acl_error ACL_API_ENTRY +if_aclInsertSection(aclCompiler *cl, + aclBinary *binary, + const void *data, + size_t data_size, + aclSections id) ACL_API_0_8; + +acl_error ACL_API_ENTRY +if_aclInsertSymbol(aclCompiler *cl, + aclBinary *binary, + const void *data, + size_t data_size, + aclSections id, + const char *symbol) ACL_API_0_8; + +const void* ACL_API_ENTRY +if_aclExtractSection(aclCompiler *cl, + const aclBinary *binary, + size_t *size, + aclSections id, + acl_error *error_code) ACL_API_0_8; + +const void* ACL_API_ENTRY +if_aclExtractSymbol(aclCompiler *cl, + const aclBinary *binary, + size_t *size, + aclSections id, + const char *symbol, + acl_error *error_code) ACL_API_0_8; + +acl_error ACL_API_ENTRY +if_aclRemoveSection(aclCompiler *cl, + aclBinary *binary, + aclSections id) ACL_API_0_8; + +acl_error ACL_API_ENTRY +if_aclRemoveSymbol(aclCompiler *cl, + aclBinary *binary, + aclSections id, + const char *symbol) ACL_API_0_8; + +acl_error ACL_API_ENTRY +if_aclQueryInfo(aclCompiler *cl, + const aclBinary *binary, + aclQueryType query, + const char *kernel, + void *data_ptr, + size_t *ptr_size) ACL_API_0_8; + +acl_error ACL_API_ENTRY +if_aclDbgAddArgument(aclCompiler *cl, + aclBinary *bin, + const char *kernel, + const char *name, + bool byVal) ACL_API_0_8; + +acl_error ACL_API_ENTRY +if_aclDbgRemoveArgument(aclCompiler *cl, + aclBinary *bin, + const char* kernel, + const char* name) ACL_API_0_8; + +#endif // _IF_ACL_0_8_H_ diff --git a/rocclr/compiler/lib/complibdefs b/rocclr/compiler/lib/complibdefs new file mode 100644 index 0000000000..1e464b6b55 --- /dev/null +++ b/rocclr/compiler/lib/complibdefs @@ -0,0 +1,84 @@ +include $(OPENCL_DEPTH)/opencldefs +COMPLIB_DEPTH = $(OPENCL_DEPTH)/compiler/lib + +ifdef GONE_TO_BUILD_DIR +ifeq ($(BUILD_HSA_TARGET),yes) +override BUILD_CPU_DEVICE = no +override BUILD_GPU_DEVICE = no +endif +endif + +ifneq ($(BUILD_GPU_DEVICE),no) +GCPPFLAGS += $(DEFSWITCH) WITH_TARGET_AMDIL +endif + +ifneq ($(BUILD_HSA_DEVICE),no) +GCPPFLAGS += $(DEFSWITCH) WITH_TARGET_HSAIL +ifneq ($(BUILD_GPU_DEVICE),no) +GCPPFLAGS += $(DEFSWITCH) DYNAMIC_HSAIL +endif +endif + +ifneq ($(BUILD_CPU_DEVICE),no) +ifdef ATI_ARCH_ARM +GCPPFLAGS += $(DEFSWITCH) WITH_TARGET_ARM +endif + +ifdef ATI_ARCH_X86 +ifneq ($(BUILD_CPU_DEVICE),no) +GCPPFLAGS += $(DEFSWITCH) WITH_TARGET_X86 +endif +endif +endif + +GCPPFLAGS += $(INCSWITCH) "$(DEPTH)/drivers" +GCPPFLAGS += $(INCSWITCH) "$(DEPTH)/drivers/inc/asic_reg" +GCPPFLAGS += $(INCSWITCH) "$(COMPLIB_DEPTH)" +GCPPFLAGS += $(INCSWITCH) "$(COMPLIB_DEPTH)/promotions/oclutils" +GCPPFLAGS += $(INCSWITCH) "$(COMPLIB_DEPTH)/utils" + +# Do we build the 0.9 version? +ifeq ($(BUILD_VERSION_0_9), 1) +GCPPFLAGS += $(DEFSWITCH) WITH_VERSION_0_9 +CLVER_SUBDIR = v0_9 +else +# Otherwise we build v0.8 +GCPPFLAGS += $(DEFSWITCH) WITH_VERSION_0_8 +CLVER_SUBDIR = v0_8 +endif + +GCPPFLAGS += $(INCSWITCH) "$(COMPLIB_DEPTH)/utils/$(CLVER_SUBDIR)" +GCPPFLAGS += $(INCSWITCH) "$(COMPLIB_DEPTH)/include/$(CLVER_SUBDIR)" +GCPPFLAGS += $(DEFSWITCH) "BREAK_ON_LOG_WARNING=0" +GCPPFLAGS += $(DEFSWITCH) "BREAK_ON_LOG_ERROR=0" +GCPPFLAGS += $(DEFSWITCH) "LOG_LEVEL=2" + +export CAL_INCLUDES = $(OPENCL_DEPTH)/runtime/device/gpu/gslbe/src/include/cal +GCPPFLAGS += $(INCSWITCH) "$(CAL_INCLUDES)" +GCPPFLAGS += $(INCSWITCH) "$(CAL_INCLUDES)/private" +GCPPFLAGS += $(INCSWITCH) "$(UGL_DEPTH)/gl/gs/hwl/pele/include/asic" +GCPPFLAGS += $(INCSWITCH) "$(UGL_DEPTH)/gl/gs/hwl/evergreen/include/asic" +GCPPFLAGS += $(INCSWITCH) "$(UGL_DEPTH)/gl/gs/hwl/si/include/asic" +export BRIG_ENABLE=1 + +ifdef ATI_OS_WINDOWS + GCPPFLAGS += $(INCSWITCH) "$(DK_ROOT)/$(DK_DX_SDK)/inc" + + ifeq ($(BUILD_TYPE),dbg) + GCXXOPTS += /Ob1 + endif + + GCXXOPTS += -wd4985 -wd4355 -wd4800 + + GCPPFLAGS += $(DEFSWITCH) _SCL_SECURE_NO_WARNINGS + + GCXXOPTS += /GR- # Disable Run-Time Type Information +endif + +ifdef ATI_OS_LINUX + GCPPFLAGS += $(DEFSWITCH) __STDC_LIMIT_MACROS + GCPPFLAGS += $(DEFSWITCH) __STDC_CONSTANT_MACROS + + GCXXOPTS += -fno-rtti +endif + diff --git a/rocclr/compiler/lib/complibrules b/rocclr/compiler/lib/complibrules new file mode 100644 index 0000000000..5c438132ad --- /dev/null +++ b/rocclr/compiler/lib/complibrules @@ -0,0 +1,16 @@ +COMPLIB_DEPTH = $(OPENCL_DEPTH)/compiler/lib +ifdef GONE_TO_BUILD_DIR +ifdef ATI_OS_LINUX +*$(OBJ_EXT): $(COMPLIB_DEPTH)/complibdefs $(COMPLIB_DEPTH)/complibrules +endif +endif + +%.v08: + @$(MAKENOISE) "Setting BUILD_VERSION_0_8=1" + @$(MAKE) BUILD_VERSION_0_8=1 $* + +%.v09: + @$(MAKENOISE) "Setting BUILD_VERSION_0_9=1" + @$(MAKE) BUILD_VERSION_0_9=1 $* + +include $(OPENCL_DEPTH)/openclrules diff --git a/rocclr/compiler/lib/include/acl.h b/rocclr/compiler/lib/include/acl.h new file mode 100644 index 0000000000..1bfd20192f --- /dev/null +++ b/rocclr/compiler/lib/include/acl.h @@ -0,0 +1,17 @@ +// +// Copyright (c) 2012 Advanced Micro Devices, Inc. All rights reserved. +// +// This is a compatibility header file. Either define the version +// of the compiler library that is to be used or include the +// header file for that version directly. +#ifndef ACL_H_ +#define ACL_H_ +#if WITH_VERSION_0_8 +#include "v0_8/acl.h" +#elif WITH_VERSION_0_9 +#include "v0_9/acl.h" +#else +#error "The compiler library version was not defined." +#include "v0_8/acl.h" +#endif +#endif // ACL_H_ diff --git a/rocclr/compiler/lib/include/aclDefs.h b/rocclr/compiler/lib/include/aclDefs.h new file mode 100644 index 0000000000..987a06f1a8 --- /dev/null +++ b/rocclr/compiler/lib/include/aclDefs.h @@ -0,0 +1,16 @@ +// +// Copyright (c) 2011 Advanced Micro Devices, Inc. All rights reserved. +// +// This is a compatibility header file. Either define the version +// of the compiler library that is to be used or include the +// header file for that version directly. +#ifndef ACL_DEFS_H_ +#define ACL_DEFS_H_ +#if WITH_VERSION_0_8 +#include "v0_8/aclDefs.h" +#elif WITH_VERSION_0_9 +#else +#error "The compiler library version was not defined." +#include "v0_8/aclDefs.h" +#endif +#endif // ACL_DEFS_H_ diff --git a/rocclr/compiler/lib/include/aclEnums.h b/rocclr/compiler/lib/include/aclEnums.h new file mode 100644 index 0000000000..9e1e2f94c3 --- /dev/null +++ b/rocclr/compiler/lib/include/aclEnums.h @@ -0,0 +1,16 @@ +// +// Copyright (c) 2012 Advanced Micro Devices, Inc. All rights reserved. +// +// This is a compatibility header file. Either define the version +// of the compiler library that is to be used or include the +// header file for that version directly. +#ifndef ACL_ENUMS_H_ +#define ACL_ENUMS_H_ +#if WITH_VERSION_0_8 +#include "v0_8/aclEnums.h" +#elif WITH_VERSION_0_9 +#else +#error "The compiler library version was not defined." +#include "v0_8/aclEnums.h" +#endif +#endif // ACL_ENUMS_H_ diff --git a/rocclr/compiler/lib/include/aclFunctors.h b/rocclr/compiler/lib/include/aclFunctors.h new file mode 100644 index 0000000000..b1f2309984 --- /dev/null +++ b/rocclr/compiler/lib/include/aclFunctors.h @@ -0,0 +1,16 @@ +// +// Copyright (c) 2012 Advanced Micro Devices, Inc. All rights reserved. +// +// This is a compatibility header file. Either define the version +// of the compiler library that is to be used or include the +// header file for that version directly. +#ifndef ACL_FUNCTORS_H_ +#define ACL_FUNCTORS_H_ +#if WITH_VERSION_0_8 +#include "v0_8/aclFunctors.h" +#elif WITH_VERSION_0_9 +#else +#error "The compiler library version was not defined." +#include "v0_8/aclFunctors.h" +#endif +#endif // ACL_FUNCTORS_H_ diff --git a/rocclr/compiler/lib/include/aclStructs.h b/rocclr/compiler/lib/include/aclStructs.h new file mode 100644 index 0000000000..6d8f5b97a3 --- /dev/null +++ b/rocclr/compiler/lib/include/aclStructs.h @@ -0,0 +1,16 @@ +// +// Copyright (c) 2012 Advanced Micro Devices, Inc. All rights reserved. +// +// This is a compatibility header file. Either define the version +// of the compiler library that is to be used or include the +// header file for that version directly. +#ifndef ACL_STRUCTS_H_ +#define ACL_STRUCTS_H_ +#if WITH_VERSION_0_8 +#include "v0_8/aclStructs.h" +#elif WITH_VERSION_0_9 +#else +#error "The compiler library version was not defined." +#include "v0_8/aclStructs.h" +#endif +#endif // ACL_STRUCTS_H_ diff --git a/rocclr/compiler/lib/include/aclTypes.h b/rocclr/compiler/lib/include/aclTypes.h new file mode 100644 index 0000000000..7ef826507d --- /dev/null +++ b/rocclr/compiler/lib/include/aclTypes.h @@ -0,0 +1,17 @@ +// +// Copyright (c) 2012 Advanced Micro Devices, Inc. All rights reserved. +// +// This is a compatibility header file. Either define the version +// of the compiler library that is to be used or include the +// header file for that version directly. +#ifndef ACL_TYPES_H_ +#define ACL_TYPES_H_ +#if WITH_VERSION_0_8 +#include "v0_8/aclTypes.h" +#elif WITH_VERSION_0_9 +#include "v0_9/aclTypes.h" +#else +#error "The compiler library version was not defined." +#include "v0_8/aclTypes.h" +#endif +#endif // ACL_TYPES_H_ diff --git a/rocclr/compiler/lib/include/v0_8/acl.h b/rocclr/compiler/lib/include/v0_8/acl.h new file mode 100644 index 0000000000..340bfee11d --- /dev/null +++ b/rocclr/compiler/lib/include/v0_8/acl.h @@ -0,0 +1,250 @@ +// +// Copyright (c) 2012 Advanced Micro Devices, Inc. All rights reserved. +// +#ifndef _ACL_0_8_H_ +#define _ACL_0_8_H_ +#ifdef __cplusplus +extern "C" { +#endif +#include "aclTypes.h" + +//!--------------------------------------------------------------------------!// +// Functions that deal with aclCompiler objects. +//!--------------------------------------------------------------------------!// +aclCompiler* ACL_API_ENTRY +aclCompilerInit(aclCompilerOptions *opts, acl_error *error_code) ACL_API_0_8; +acl_error ACL_API_ENTRY + aclCompilerFini(aclCompiler *cl) ACL_API_0_8; +aclCLVersion ACL_API_ENTRY + aclCompilerVersion(aclCompiler *cl, acl_error *error_code) ACL_API_0_8; +uint32_t ACL_API_ENTRY + aclVersionSize(aclCLVersion num, acl_error *error_code) ACL_API_0_8; +const char* ACL_API_ENTRY + aclGetErrorString(acl_error error_code) ACL_API_0_8; + +//!--------------------------------------------------------------------------!// +// Functions that deal with target specific information. +//!--------------------------------------------------------------------------!// +//! Returns in the names argument, if non-NULL, a pointer to each of the arch +// names that the compiler supports. If names is NULL and arch_size is +// non-NULL, returns the number of arch entries that are required. +acl_error ACL_API_ENTRY + aclGetArchInfo(const char** arch_names, + size_t *arch_size) ACL_API_0_8; + +//! Returns in the arch argument, if non-NULL, a pointer to each device +// name that the compiler supports. If device_size is non-NULL, +// returns the number of device entries that are used. +acl_error ACL_API_ENTRY + aclGetDeviceInfo(const char* arch, + const char **names, + size_t *device_size) ACL_API_0_8; + +//! Function that returns a correctly filled out aclTargetInfo structure based +// on the information passed into the kernel. +aclTargetInfo ACL_API_ENTRY +aclGetTargetInfo(const char *arch, + const char *device, + acl_error *error_code) ACL_API_0_8; + +//! Function that returns a correctly filled out aclTargetInfo structure based +// on the information passed into the kernel. +aclTargetInfo ACL_API_ENTRY +aclGetTargetInfoFromChipID(const char *arch, + const uint32_t chip_id, + acl_error *error_code) ACL_API_0_8; + +//! Function that returns a string representation of the target architecture. +const char* ACL_API_ENTRY + aclGetArchitecture(const aclTargetInfo &target) ACL_API_0_8; + +//! Function that returns a string representation of the target chip options. +const uint64_t ACL_API_ENTRY + aclGetChipOptions(const aclTargetInfo &target) ACL_API_0_8; + +//! Function that returns a string representation of the target family. +const char* ACL_API_ENTRY + aclGetFamily(const aclTargetInfo &target) ACL_API_0_8; + +//! Function that returns a string representation of the target chip. +const char* ACL_API_ENTRY + aclGetChip(const aclTargetInfo &target) ACL_API_0_8; + +//!--------------------------------------------------------------------------!// +// Functions that deal with aclBinary objects. +//!--------------------------------------------------------------------------!// +aclBinary* ACL_API_ENTRY + aclBinaryInit( + size_t struct_version, + const aclTargetInfo *target, + const aclBinaryOptions *options, + acl_error *error_code) ACL_API_0_8; + +acl_error ACL_API_ENTRY + aclBinaryFini(aclBinary *bin) ACL_API_0_8; + +aclBinary* ACL_API_ENTRY + aclReadFromFile(const char *str, + acl_error *error_code) ACL_API_0_8; + +aclBinary* ACL_API_ENTRY + aclReadFromMem(const void *mem, + size_t size, acl_error *error_code) ACL_API_0_8; + +acl_error ACL_API_ENTRY + aclWriteToFile(aclBinary *bin, + const char *str) ACL_API_0_8; + +acl_error ACL_API_ENTRY + aclWriteToMem(aclBinary *bin, + void **mem, size_t *size) ACL_API_0_8; + +aclBinary* ACL_API_ENTRY + aclCreateFromBinary(const aclBinary *binary, + aclBIFVersion version) ACL_API_0_8; + +aclBIFVersion ACL_API_ENTRY + aclBinaryVersion(const aclBinary *binary) ACL_API_0_8; + +acl_error ACL_API_ENTRY + aclInsertSection(aclCompiler *cl, + aclBinary *binary, + const void *data, + size_t data_size, + aclSections id) ACL_API_0_8; + +acl_error ACL_API_ENTRY + aclInsertSymbol(aclCompiler *cl, + aclBinary *binary, + const void *data, + size_t data_size, + aclSections id, + const char *symbol) ACL_API_0_8; + +const void* ACL_API_ENTRY + aclExtractSection(aclCompiler *cl, + const aclBinary *binary, + size_t *size, + aclSections id, + acl_error *error_code) ACL_API_0_8; + +const void* ACL_API_ENTRY + aclExtractSymbol(aclCompiler *cl, + const aclBinary *binary, + size_t *size, + aclSections id, + const char *symbol, + acl_error *error_code) ACL_API_0_8; + +acl_error ACL_API_ENTRY + aclRemoveSection(aclCompiler *cl, + aclBinary *binary, + aclSections id) ACL_API_0_8; + +acl_error ACL_API_ENTRY + aclRemoveSymbol(aclCompiler *cl, + aclBinary *binary, + aclSections id, + const char *symbol) ACL_API_0_8; + +//!--------------------------------------------------------------------------!// +// Functions that deal with debug/metdata. +//!--------------------------------------------------------------------------!// +acl_error ACL_API_ENTRY + aclQueryInfo(aclCompiler *cl, + const aclBinary *binary, + aclQueryType query, + const char *kernel, + void *data_ptr, + size_t *ptr_size) ACL_API_0_8; + +acl_error ACL_API_ENTRY + aclDbgAddArgument(aclCompiler *cl, + aclBinary *binary, + const char* kernel, + const char* name, + bool byVal) + ACL_API_0_8; + +acl_error ACL_API_ENTRY + aclDbgRemoveArgument(aclCompiler *cl, + aclBinary *binary, + const char* kernel, + const char* name) + ACL_API_0_8; + +//!--------------------------------------------------------------------------!// +// Functions that deal with various compilation phases. +//!--------------------------------------------------------------------------!// +acl_error ACL_API_ENTRY + aclCompile(aclCompiler *cl, + aclBinary *bin, + const char *options, + aclType from, + aclType to, + aclLogFunction compile_callback) ACL_API_0_8; + +acl_error ACL_API_ENTRY + aclLink(aclCompiler *cl, + aclBinary *src_bin, + unsigned int num_libs, + aclBinary **libs, + aclType link_mode, + const char *options, + aclLogFunction link_callback) ACL_API_0_8; + +const char* ACL_API_ENTRY + aclGetCompilerLog(aclCompiler *cl) ACL_API_0_8; + +const void* ACL_API_ENTRY + aclRetrieveType(aclCompiler *cl, + const aclBinary *bin, + const char *name, + size_t *data_size, + aclType type, + acl_error *error_code) ACL_API_0_8; + +acl_error ACL_API_ENTRY + aclSetType(aclCompiler *cl, + aclBinary *bin, + const char *name, + aclType type, + const void *data, + size_t size) ACL_API_0_8; + +acl_error ACL_API_ENTRY + aclConvertType(aclCompiler *cl, + aclBinary *bin, + const char *name, + aclType type) ACL_API_0_8; + +acl_error ACL_API_ENTRY + aclDisassemble(aclCompiler *cl, + aclBinary *bin, + const char *kernel, + aclLogFunction disasm_callback) ACL_API_0_8; + +const void* ACL_API_ENTRY + aclGetDeviceBinary(aclCompiler *cl, + const aclBinary *bin, + const char *kernel, + size_t *size, + acl_error *error_code) ACL_API_0_8; + +//!--------------------------------------------------------------------------!// +// Debug functionality +//!--------------------------------------------------------------------------!// +void aclDumpBinary(const aclBinary *bin); + + +//!--------------------------------------------------------------------------!// +// Functions that deal with memory. +// Free memory allocated by aclWriteToMem +//!--------------------------------------------------------------------------!// +acl_error ACL_API_ENTRY +aclFreeMem(aclBinary *bin, + void *mem); +#ifdef __cplusplus +} +#endif +#endif // _ACL_0_8_H_ diff --git a/rocclr/compiler/lib/include/v0_8/aclDefs.h b/rocclr/compiler/lib/include/v0_8/aclDefs.h new file mode 100644 index 0000000000..f39756b9d4 --- /dev/null +++ b/rocclr/compiler/lib/include/v0_8/aclDefs.h @@ -0,0 +1,35 @@ +// +// Copyright (c) 2011 Advanced Micro Devices, Inc. All rights reserved. +// +#ifndef _ACL_DEFS_0_8_H_ +#define _ACL_DEFS_0_8_H_ + +#ifndef ACL_API_ENTRY +#if defined(_WIN32) || defined(__CYGWIN__) +#define ACL_API_ENTRY __stdcall +#else +#define ACL_API_ENTRY +#endif +#endif + +#ifndef ACL_API_0_8 +#define ACL_API_0_8 +#endif + +#ifndef AOC_API_0_7 +#define AOC_API_0_7 +#endif + +#ifndef BIF_API_2_0 +#define BIF_API_2_0 +#endif + +#ifndef BIF_API_2_1 +#define BIF_API_2_1 +#endif + +#ifndef BIF_API_3_0 +#define BIF_API_3_0 +#endif + +#endif // _ACL_DEFS_0_8_H_ diff --git a/rocclr/compiler/lib/include/v0_8/aclEnums.h b/rocclr/compiler/lib/include/v0_8/aclEnums.h new file mode 100644 index 0000000000..1f6432b6e7 --- /dev/null +++ b/rocclr/compiler/lib/include/v0_8/aclEnums.h @@ -0,0 +1,318 @@ +// +// Copyright (c) 2012 Advanced Micro Devices, Inc. All rights reserved. +// +#ifndef _ACL_ENUMS_0_8_H_ +#define _ACL_ENUMS_0_8_H_ + +typedef enum _acl_error_enum_0_8 { + ACL_SUCCESS = 0, + ACL_ERROR = 1, + ACL_INVALID_ARG = 2, + ACL_OUT_OF_MEM = 3, + ACL_SYS_ERROR = 4, + ACL_UNSUPPORTED = 5, + ACL_ELF_ERROR = 6, + ACL_INVALID_FILE = 7, + ACL_INVALID_COMPILER= 8, + ACL_INVALID_TARGET = 9, + ACL_INVALID_BINARY = 10, + ACL_INVALID_OPTION = 11, + ACL_INVALID_TYPE = 12, + ACL_INVALID_SECTION = 13, + ACL_INVALID_SYMBOL = 14, + ACL_INVALID_QUERY = 15, + ACL_FRONTEND_FAILURE= 16, + ACL_INVALID_BITCODE = 17, + ACL_LINKER_ERROR = 18, + ACL_OPTIMIZER_ERROR = 19, + ACL_CODEGEN_ERROR = 20, + ACL_ISAGEN_ERROR = 21, + ACL_INVALID_SOURCE = 22, + ACL_LIBRARY_ERROR = 23, + ACL_INVALID_SPIR = 24, + ACL_LWVERIFY_FAIL = 25, + ACL_HWVERIFY_FAIL = 26, + ACL_LAST_ERROR = 27 +} acl_error_0_8; + +typedef enum _comp_device_caps_enum_0_8 { + capError = 0, + capFMA = 1, + capImageSupport = 2, + capSaveSOURCE = 3, // input source + capSaveLLVMIR = 4, // output LLVMIR from frontend + capSaveCG = 5, // output from LLVM-BE + capSaveEXE = 6, // output executable + capSaveAMDIL = 7, // Save per-kernel AMDIL + capSaveHSAIL = 8, // Save per-kernel HSAIL + capEncrypted = 9, + capSaveDISASM = 10, + capSaveAS = 11, + capSaveSPIR = 12, + capDumpLast = 13 +} compDeviceCaps_0_8; + +typedef enum _comp_opt_settings_enum_0_8 { + optO0 = 0, // No optimization setting. + optO1 = 1, + optO2 = 2, + optO3 = 3, + optO4 = 4, + optOs = 5, + optError = 6, // Invalid optimization set + optLast = 7 +} compOptSettings_0_8; + +#define FLAG_SHIFT_VALUE 5 +#define FLAG_MASK_VALUE ((1 << capDumpLast) - 1) +#define FLAG_BITLOC(A) (1 << ((A) & FLAG_MASK_VALUE)) +#define FLAG_ARRAY_SIZE 4 + +//! An enumeration that defines the possible valid device types that +// can be compiled for. +typedef enum _acl_dev_type_enum_0_8 { + aclError = 0, // aclDevType of 0 is an error. + aclX86 = 1, // Targeting a 32bit X86 CPU device. + aclAMDIL = 2, // Targeting an AMDIL GPU device. + aclHSAIL = 3, // Targeting an HSAIL GPU device. + aclX64 = 4, // Targeting a 64bit X86 CPU device. + aclHSAIL64= 5, // Targeting a 64bit HSAIL GPU device. + aclAMDIL64= 6, // Targeting a 64bit AMDIL GPU device + aclLast = 7 +} aclDevType_0_8; + +//! Enum that represents the versions of the compiler +typedef enum _acl_cl_version_enum_0_8 { + ACL_VERSION_ERROR = 0, + ACL_VERSION_0_7 = 1, + ACL_VERSION_0_8 = 2, + ACL_VERSION_0_8_1 = 3, + ACL_VERSION_0_9 = 4, + ACL_VERSION_1_0 = 5, + ACL_VERSION_LAST = 6 +} aclCLVersion_0_8; + +//! Enum of the various aclTypes that are supported +typedef enum _acl_type_enum_0_8 { + ACL_TYPE_DEFAULT = 0, + ACL_TYPE_OPENCL = 1, + ACL_TYPE_LLVMIR_TEXT = 2, + ACL_TYPE_LLVMIR_BINARY = 3, + ACL_TYPE_SPIR_TEXT = 4, + ACL_TYPE_SPIR_BINARY = 5, + ACL_TYPE_AMDIL_TEXT = 6, + ACL_TYPE_AMDIL_BINARY = 7, + ACL_TYPE_HSAIL_TEXT = 8, + ACL_TYPE_HSAIL_BINARY = 9, + ACL_TYPE_X86_TEXT = 10, + ACL_TYPE_X86_BINARY = 11, + ACL_TYPE_CG = 12, + ACL_TYPE_SOURCE = 13, + ACL_TYPE_ISA = 14, + ACL_TYPE_HEADER = 15, + ACL_TYPE_RSLLVMIR_BINARY = 16, + ACL_TYPE_LAST = 17 +} aclType_0_8; + +//! Enum of the various loader types that are supported. +typedef enum _acl_loader_type_enum_0_8 { + ACL_LOADER_COMPLIB = 0, + ACL_LOADER_FRONTEND = 1, + ACL_LOADER_LINKER = 2, + ACL_LOADER_OPTIMIZER= 3, + ACL_LOADER_CODEGEN = 4, + ACL_LOADER_BACKEND = 5, + ACL_LOADER_SC = 6, + ACL_LOADER_LAST = 7 +} aclLoaderType_0_8; + +// Enumeration for the various acl versions +typedef enum _bif_version_enum_0_8 { + aclBIFVersionError = 0, // Error + aclBIFVersion20 = 1, // Version 2.0 of the OpenCL BIF + aclBIFVersion21 = 2, // Version 2.1 of the OpenCL BIF + aclBIFVersion30 = 3, // Version 3.0 of the OpenCL BIF + aclBIFVersionLatest = aclBIFVersion30, // Most recent version of the BIF + aclBIFVersionCAL = 4, + aclBIFVersionLast = 5 +} aclBIFVersion_0_8; + +// Enumeration for the various platform types +typedef enum _bif_platform_enum_0_8 { + aclPlatformCAL = 0, // For BIF 2.0 backward compatibility + aclPlatformCPU = 1, // For BIF 2.0 backward compatibility + aclPlatformCompLib = 2, + aclPlatformLast = 3 +} aclPlatform_0_8; + +// Enumeration for the various bif sections +typedef enum _bif_sections_enum_0_8 { + aclLLVMIR = 0, + aclSOURCE = 1, + aclILTEXT = 2, // For BIF 2.0 backward compatibility + aclASTEXT = 3, // For BIF 2.0 backward compatibility + aclCAL = 4, // For BIF 2.0 backward compatibility + aclDLL = 5, // For BIF 2.0 backward compatibility + aclSTRTAB = 6, + aclSYMTAB = 7, + aclRODATA = 8, + aclSHSTRTAB = 9, + aclNOTES = 10, + aclCOMMENT = 11, + aclILDEBUG = 12, // For BIF 2.0 backward compatibility + aclDEBUG_INFO = 13, + aclDEBUG_ABBREV = 14, + aclDEBUG_LINE = 15, + aclDEBUG_PUBNAMES = 16, + aclDEBUG_PUBTYPES = 17, + aclDEBUG_LOC = 18, + aclDEBUG_ARANGES = 19, + aclDEBUG_RANGES = 20, + aclDEBUG_MACINFO = 21, + aclDEBUG_STR = 22, + aclDEBUG_FRAME = 23, + aclJITBINARY = 24, // For BIF 2.0 backward compatibility + aclCODEGEN = 25, + aclTEXT = 26, + aclINTERNAL = 27, + aclSPIR = 28, + aclHEADER = 29, + aclBRIGcode = 30, + aclBRIGdirs = 31, + aclBRIGoprs = 32, + aclBRIGstrs = 33, + aclHSADEBUG = 34, + aclLAST = 35 +} aclSections_0_8; + +//! An enumeration that defines what are valid queries for aclQueryInfo. +typedef enum _rt_query_types_enum_0_8 { + RT_ABI_VERSION = 0, + RT_DEVICE_NAME = 1, + RT_MEM_SIZES = 2, + RT_GPU_FUNC_CAPS = 3, + RT_GPU_FUNC_ID = 4, + RT_GPU_DEFAULT_ID = 5, + RT_WORK_GROUP_SIZE = 6, + RT_WORK_REGION_SIZE = 7, + RT_ARGUMENT_ARRAY = 8, + RT_GPU_PRINTF_ARRAY = 9, + RT_CPU_BARRIER_NAMES= 10, + RT_DEVICE_ENQUEUE = 11, + RT_KERNEL_INDEX = 12, + RT_LAST_TYPE = 13 +} aclQueryType_0_8; + +//! An enumeration for the various GPU capabilities +typedef enum _rt_gpu_caps_enum_0_8 { + RT_COMPILER_WRITE = 1 << 0, + RT_DATA_SECTION = 1 << 1, + RT_WGS = 1 << 2, + RT_LIMIT_WGS = 1 << 3, + RT_PACKED_REGS = 1 << 4, + RT_64BIT_ABI = 1 << 5, + RT_PRINTF = 1 << 6, + RT_ARENA_UAV = 1 << 7, + RT_LRP_MEM = 1 << 8, // Local/Region/Private Memory + RT_INDEX_TEMPS = 1 << 9, + RT_WRS = 1 << 10, + RT_GWS = 1 << 11, + RT_SWGWS = 1 << 12, + RT_GPU_CAPS_MASK = 0xFFF +} aclGPUCaps_0_8; + +//! An enumeration for the various CPU capabilities. +typedef enum _rt_cpu_caps_enum_0_8 { + RT_KERNEL_BARRIER = 1 << 0, + RT_PROGRAM_BARRIER = 1 << 1, + RT_CPU_CAPS_MASK = 0x3 +} aclCPUCaps_0_8; + +//! An enumeration that maps Resource type to index values +typedef enum _rt_gpu_resource_enum_0_8 { + RT_RES_UAV = 0, // UAV resources + RT_RES_PRI = 1, // Private resources + RT_RES_LDS = 2, // LDS resources + RT_RES_GDS = 3, // GDS resources + RT_RES_CON = 4, // Constant resources + RT_RES_LAST = 5 +} aclGPUResource_0_8; + +//! An enumeration that maps memory types to index values +typedef enum _rt_gpu_mem_sizes_enum_0_8 { + RT_MEM_HW_LOCAL = 0, + RT_MEM_SW_LOCAL = 1, + RT_MEM_HW_PRIVATE = 2, + RT_MEM_SW_PRIVATE = 3, + RT_MEM_HW_REGION = 4, + RT_MEM_SW_REGION = 5, + RT_MEM_LAST = 6 +} aclGPUMemSizes_0_8; + +// Enumerations for the various argument types. +typedef enum _acl_arg_type_enum_0_8 { + ARG_TYPE_ERROR = 0, + ARG_TYPE_SAMPLER = 1, + ARG_TYPE_IMAGE = 2, + ARG_TYPE_COUNTER = 3, + ARG_TYPE_VALUE = 4, + ARG_TYPE_POINTER = 5, + ARG_TYPE_SEMAPHORE = 6, + ARG_TYPE_LAST = 7 +} aclArgType_0_8; + +// Enumerations of the valid data types for pass by value and +// pass by pointer kernel arguments. +typedef enum _acl_data_type_enum_0_8 { + DATATYPE_ERROR = 0, + DATATYPE_i1 = 1, + DATATYPE_i8 = 2, + DATATYPE_i16 = 3, + DATATYPE_i32 = 4, + DATATYPE_i64 = 5, + DATATYPE_u8 = 6, + DATATYPE_u16 = 7, + DATATYPE_u32 = 8, + DATATYPE_u64 = 9, + DATATYPE_f16 = 10, + DATATYPE_f32 = 11, + DATATYPE_f64 = 12, + DATATYPE_f80 = 13, + DATATYPE_f128 = 14, + DATATYPE_struct = 15, + DATATYPE_union = 16, + DATATYPE_event = 17, + DATATYPE_opaque = 18, + DATATYPE_unknown = 19, + DATATYPE_LAST = 20 +} aclArgDataType_0_8; + +// Enumerations of the valid memory types for pass by pointer +// kernel arguments +typedef enum _acl_memory_type_enum_0_8 { + PTR_MT_ERROR = 0, // Error + PTR_MT_GLOBAL = 1, // global buffer + PTR_MT_SCRATCH_EMU = 2, // SW emulated private memory + PTR_MT_LDS_EMU = 3, // SW emulated local memory + PTR_MT_UAV = 4, // uniformed access vector memory + PTR_MT_CONSTANT_EMU = 5, // SW emulated constant memory + PTR_MT_GDS_EMU = 6, // SW emulated region memory + PTR_MT_LDS = 7, // HW local memory + PTR_MT_SCRATCH = 8, // HW private memory + PTR_MT_CONSTANT = 9, // HW constant memory + PTR_MT_GDS = 10, // HW region memory + PTR_MT_UAV_SCRATCH = 11, // SI and later HW private memory + PTR_MT_UAV_CONSTANT = 12, // SI and later HW constant memory + PTR_MT_LAST = 13 +} aclMemoryType_0_8; + +// Enumeration that specifies the various access types for a pointer/image. +typedef enum _acl_access_type_enum_0_8 { + ACCESS_TYPE_ERROR = 0, + ACCESS_TYPE_RO = 1, + ACCESS_TYPE_WO = 2, + ACCESS_TYPE_RW = 3, + ACCESS_TYPE_LAST = 4 +} aclAccessType_0_8; + +#endif // _ACL_ENUMS_0_8_H_ diff --git a/rocclr/compiler/lib/include/v0_8/aclFunctors.h b/rocclr/compiler/lib/include/v0_8/aclFunctors.h new file mode 100644 index 0000000000..43a19a91a5 --- /dev/null +++ b/rocclr/compiler/lib/include/v0_8/aclFunctors.h @@ -0,0 +1,182 @@ +// +// Copyright (c) 2012 Advanced Micro Devices, Inc. All rights reserved. +// +#ifndef _ACL_FUNCTORS_0_8_H_ +#define _ACL_FUNCTORS_0_8_H_ + +//! Callback for the log function function pointer that many +// API calls take to have the calling application receive +// information on what errors occur. +typedef void (*aclLogFunction_0_8)(const char *msg, size_t size); + +typedef acl_error +(ACL_API_ENTRY *InsertSec_0_8)(aclCompiler *cl, + aclBinary *binary, + const void *data, + size_t data_size, + aclSections id) ACL_API_0_8; + +typedef acl_error +(ACL_API_ENTRY *InsertSym_0_8)(aclCompiler *cl, + aclBinary *binary, + const void *data, + size_t data_size, + aclSections id, + const char *symbol) ACL_API_0_8; + +typedef const void * +(ACL_API_ENTRY *ExtractSec_0_8)(aclCompiler *cl, + const aclBinary *binary, + size_t *size, + aclSections id, + acl_error *error_code) ACL_API_0_8; + +typedef const void * +(ACL_API_ENTRY *ExtractSym_0_8)(aclCompiler *cl, + const aclBinary *binary, + size_t *size, + aclSections id, + const char *symbol, + acl_error *error_code) ACL_API_0_8; + +typedef acl_error +(ACL_API_ENTRY *RemoveSec_0_8)(aclCompiler *cl, + aclBinary *binary, + aclSections id) ACL_API_0_8; + +typedef acl_error +(ACL_API_ENTRY *RemoveSym_0_8)(aclCompiler *cl, + aclBinary *binary, + aclSections id, + const char *symbol) ACL_API_0_8; + +typedef acl_error +(ACL_API_ENTRY *QueryInfo_0_8)(aclCompiler *cl, + const aclBinary *binary, + aclQueryType query, + const char *kernel, + void *data_ptr, + size_t *ptr_size) ACL_API_0_8; + +typedef acl_error +(ACL_API_ENTRY *AddDbgArg_0_8)(aclCompiler *cl, + aclBinary *bin, + const char *kernel, + const char *name, + bool byVal) ACL_API_0_8; + +typedef acl_error +(ACL_API_ENTRY *RemoveDbgArg_0_8)(aclCompiler *cl, + aclBinary *bin, + const char *kernel, + const char *name) ACL_API_0_8; + +typedef acl_error +(ACL_API_ENTRY *Compile_0_8)(aclCompiler *cl, + aclBinary *bin, + const char *options, + aclType from, + aclType to, + aclLogFunction_0_8 compile_callback) ACL_API_0_8; + +typedef acl_error +(ACL_API_ENTRY *Link_0_8)(aclCompiler *cl, + aclBinary *src_bin, + unsigned int num_libs, + aclBinary **libs, + aclType link_mode, + const char *options, + aclLogFunction_0_8 link_callback) ACL_API_0_8; + +typedef const char * +(ACL_API_ENTRY *CompLog_0_8)(aclCompiler *cl) ACL_API_0_8; + +typedef const void * +(ACL_API_ENTRY *RetrieveType_0_8)(aclCompiler *cl, + const aclBinary *bin, + const char *name, + size_t *data_size, + aclType type, + acl_error *error_code) ACL_API_0_8; + +typedef acl_error +(ACL_API_ENTRY *SetType_0_8)(aclCompiler *cl, + aclBinary *bin, + const char *name, + aclType type, + const void *data, + size_t size) ACL_API_0_8; + +typedef acl_error +(ACL_API_ENTRY *ConvertType_0_8)(aclCompiler *cl, + aclBinary *bin, + const char *name, + aclType type) ACL_API_0_8; + +typedef acl_error +(ACL_API_ENTRY *Disassemble_0_8)(aclCompiler *cl, + aclBinary *bin, + const char *kernel, + aclLogFunction_0_8 disasm_callback) ACL_API_0_8; + +typedef const void * +(ACL_API_ENTRY *GetDevBinary_0_8)(aclCompiler *cl, + const aclBinary *bin, + const char *kernel, + size_t *size, + acl_error *error_code) ACL_API_0_8; + +typedef aclLoaderData * +(ACL_API_ENTRY *LoaderInit_0_8)(aclCompiler *cl, + aclBinary *bin, + aclLogFunction_0_8 callback, + acl_error *error); + +typedef acl_error +(ACL_API_ENTRY *LoaderFini_0_8)(aclLoaderData *data); + +typedef aclModule * +(ACL_API_ENTRY *FEToIR_0_8)(aclLoaderData *ald, + const char *source, + size_t data_size, + aclContext *ctx, + acl_error *error) ACL_API_0_8; + +typedef acl_error +(ACL_API_ENTRY *SourceToISA_0_8)(aclLoaderData *ald, + const char *source, + size_t data_size) ACL_API_0_8; + +typedef aclModule * +(ACL_API_ENTRY *IRPhase_0_8)(aclLoaderData *data, + aclModule *ir, + aclContext *ctx, + acl_error *error) ACL_API_0_8; + +typedef aclModule * +(ACL_API_ENTRY *LinkPhase_0_8)(aclLoaderData *data, + aclModule *ir, + unsigned int num_libs, + aclModule **libs, + aclContext *ctx, + acl_error *error) ACL_API_0_8; + +typedef const void * +(ACL_API_ENTRY *CGPhase_0_8)(aclLoaderData *data, + aclModule *ir, + aclContext *ctx, + acl_error *error) ACL_API_0_8; + +typedef acl_error +(ACL_API_ENTRY *DisasmISA_0_8)(aclLoaderData *data, + const char *kernel, + const void *isa_code, + size_t isa_size) ACL_API_0_8; + +typedef void* +(*AllocFunc_0_8)(size_t size) ACL_API_0_8; + +typedef void +(*FreeFunc_0_8)(void *ptr) ACL_API_0_8; + +#endif // _ACL_FUNCTORS_0_8_H_ diff --git a/rocclr/compiler/lib/include/v0_8/aclStructs.h b/rocclr/compiler/lib/include/v0_8/aclStructs.h new file mode 100644 index 0000000000..6fdcc792bf --- /dev/null +++ b/rocclr/compiler/lib/include/v0_8/aclStructs.h @@ -0,0 +1,305 @@ +// +// Copyright (c) 2012 Advanced Micro Devices, Inc. All rights reserved. +// +#ifndef _ACL_STRUCTS_0_8_H_ +#define _ACL_STRUCTS_0_8_H_ +#define ACL_STRUCT_HEADER \ + size_t struct_size +//! A structure that holds information on the various types of arguments +// The format in memory of this structure is +// ------------- +// | aclArgData | +// ------------- +// |->argStr | +// ------------- +// |->typeStr | +// ------------- +typedef struct _acl_md_arg_type_0_8 { + ACL_STRUCT_HEADER; + size_t argNameSize; + size_t typeStrSize; + const char *argStr; + const char *typeStr; + union { + struct { // Struct for sampler arguments + unsigned ID; + unsigned isKernelDefined; + unsigned value; + } sampler; + struct { // Struct for image arguments + unsigned resID; + unsigned cbNum; + unsigned cbOffset; + aclAccessType type; + bool is2D; + bool is1D; + bool isArray; + bool isBuffer; + } image; + struct { // struct for atomic counter arguments + unsigned is32bit; + unsigned resID; + unsigned cbNum; + unsigned cbOffset; + } counter; + struct { // struct for semaphore arguments + unsigned resID; + unsigned cbNum; + unsigned cbOffset; + } sema; + struct { // struct for pass by value arguments + unsigned numElements; + unsigned cbNum; + unsigned cbOffset; + aclArgDataType data; + } value; + struct { // struct for pass by pointer arguments + unsigned numElements; + unsigned cbNum; + unsigned cbOffset; + unsigned bufNum; + unsigned align; + aclArgDataType data; + aclMemoryType memory; + aclAccessType type; + bool isVolatile; + bool isRestrict; + bool isPipe; + } pointer; + } arg; + aclArgType type; + bool isConst; +} aclArgData_0_8; + +//! A structure that holds information for printf +// The format in memory of this structure is +// -------------- +// | aclPrintfFmt| +// -------------- +// |->argSizes | +// -------------- +// |->fmrStr | +// -------------- + +typedef struct _acl_md_printf_fmt_0_8 { + ACL_STRUCT_HEADER; + unsigned ID; + size_t numSizes; + size_t fmtStrSize; + uint32_t *argSizes; + const char *fmtStr; +} aclPrintfFmt_0_8; + +//! A structure that holds the metadata in the RODATA section. +typedef struct _acl_metadata_0_8 { + ACL_STRUCT_HEADER; // This holds the size of the structure itself for versioning. + size_t data_size; // This holds the size of all the memory allocated for this structure. + uint32_t major, minor, revision, gpuCaps, funcID; + uint32_t gpuRes[5]; + size_t wgs[3]; + uint32_t wrs[3]; + size_t kernelNameSize; + size_t deviceNameSize; + size_t mem[6]; + size_t numArgs; + size_t numPrintf; + + aclArgData_0_8 *args; + aclPrintfFmt_0_8 *printf; + const char *kernelName; + const char *deviceName; + bool enqueue_kernel; + uint32_t kernel_index; +} aclMetadata_0_8; + +//! An structure that holds information on the capabilities of the bif device. +typedef struct _acl_device_caps_rec_0_8 { + ACL_STRUCT_HEADER; + uint32_t flags[4]; + uint32_t encryptCode; +} aclDevCaps_0_8; + +//! Structure that holds information on the target that the source is +// being compiled for. +typedef struct _acl_target_info_rec_0_8 { + ACL_STRUCT_HEADER; + aclDevType arch_id; // An identifier for the architecture. + uint32_t chip_id; // A identifier for the chip. +} aclTargetInfo_0_8; + +// Structure for the version 0.8 of the structure. +typedef struct _acl_binary_opts_rec_0_8 { + ACL_STRUCT_HEADER; + uint32_t elfclass; + uint32_t bitness; + const char *temp_file; + uint32_t kernelArgAlign; +} aclBinaryOptions_0_8; + +// Structure for the version 0.8.1 of the structure. +// This versions addes in alloc/dealloc functions. +typedef struct _acl_binary_opts_rec_0_8_1 { + ACL_STRUCT_HEADER; + uint32_t elfclass; + uint32_t bitness; + const char *temp_file; + uint32_t kernelArgAlign; + AllocFunc_0_8 alloc; + FreeFunc_0_8 dealloc; +} aclBinaryOptions_0_8_1; + +//! Structure that holds the OpenCL binary information. +typedef struct _acl_bif_rec_0_8 { + ACL_STRUCT_HEADER; + aclTargetInfo_0_8 target; // Information about the target device. + aclBIF* bin; // Pointer to the acl. + aclOptions* options; // Pointer to acl options. + aclBinaryOptions_0_8 binOpts; // Pointer to the binary options. + aclDevCaps_0_8 caps; // Capabilities of the BIF. +} aclBinary_0_8; + +//! Version of the aclBinary that uses the 0_8_1 version of the aclBinaryOptions. +typedef struct _acl_bif_rec_0_8_1 { + ACL_STRUCT_HEADER; + aclTargetInfo_0_8 target; // Information about the target device. + aclBIF* bin; // Pointer to the acl. + aclOptions* options; // Pointer to acl options. + aclBinaryOptions_0_8_1 binOpts; // Pointer to the binary options. + aclDevCaps_0_8 caps; // Capabilities of the BIF. +} aclBinary_0_8_1; + +#define ACL_LOADER_COMMON\ + ACL_STRUCT_HEADER; \ +bool isBuiltin; \ +const char *libName; \ +void *handle; \ +LoaderInit init; \ +LoaderFini fini; + +// Struct that maps to the common structure between all loaders. +typedef struct _acl_common_loader_rec_0_8 { + ACL_LOADER_COMMON; +} aclCommonLoader_0_8; + +typedef struct _acl_cl_loader_rec_0_8 { + ACL_LOADER_COMMON; + Compile compile; + Link link; + CompLog getLog; + RetrieveType_0_8 retrieveType; + SetType_0_8 setType; + ConvertType_0_8 convertType; + Disassemble disassemble; + GetDevBinary_0_8 devBinary; + InsertSec insSec; + ExtractSec extSec; + RemoveSec remSec; + InsertSym insSym; + ExtractSym extSym; + RemoveSym remSym; + QueryInfo getInfo; + AddDbgArg addDbg; + RemoveDbgArg removeDbg; +} aclCLLoader_0_8; + +//! Structure that holds the required functions +// that sc exports for the SCDLL infrastructure. +typedef struct _acl_sc_loader_rec_0_8 { + ACL_LOADER_COMMON; + uint32_t /*SC_UINT32*/ sc_interface_version; + void /**SC_EXPORT_FUNCTIONS**/ *scef; + // Any version specific fields go here. +} aclSCLoader_0_8; + +typedef struct _acl_fe_loader_rec_0_8 { + ACL_LOADER_COMMON; + FEToIR toIR; // Used for Source to aclModule containing LLVMIR + FEToIR toModule; // Used to convert raw SPIR/LLVM-IR to aclModule + SourceToISA toISA; // Used for Source to ISA +} aclFELoader_0_8; + +typedef struct _acl_opt_loader_rec_0_8 { + ACL_LOADER_COMMON; + IRPhase optimize; // Used for IR to IR transformation +} aclOptLoader_0_8; + +typedef struct _acl_link_loader_rec_0_8 { + ACL_LOADER_COMMON; + LinkPhase link; // Used for Linking in IR modules + IRPhase toLLVMIR; // Used for converting SPIR to LLVMIR + IRPhase toSPIR; // Used for converting LLVMIR to SPIR +} aclLinkLoader_0_8; + +typedef struct _acl_cg_loader_rec_0_8 { + ACL_LOADER_COMMON; + CGPhase codegen; // Used for converting from LLVMIR to target ASM. +} aclCGLoader_0_8; + +typedef struct _acl_be_loader_rec_0_8 { + ACL_LOADER_COMMON; + SourceToISA finalize; // Used for converting from target source to target ISA. + SourceToISA assemble; // Used for converting from target text to target binary. + DisasmISA disassemble; // Used for converting from target binary to target ISA. +} aclBELoader_0_8; + +typedef struct _acl_compiler_opts_rec_0_8 { + ACL_STRUCT_HEADER; // Size of the structure for version checking. + const char *clLib; + const char *feLib; + const char *optLib; + const char *linkLib; + const char *cgLib; + const char *beLib; + const char *scLib; +} aclCompilerOptions_0_8; + +typedef struct _acl_compiler_opts_rec_0_8_1 { + ACL_STRUCT_HEADER; // Size of the structure for version checking. + const char* clLib; + const char *feLib; + const char *optLib; + const char *linkLib; + const char *cgLib; + const char *beLib; + const char *scLib; + AllocFunc alloc; + FreeFunc dealloc; +} aclCompilerOptions_0_8_1; + +//! Structure that holds the OpenCL compiler and various loaders. +typedef struct _acl_compiler_rec_0_8 { + ACL_STRUCT_HEADER; // Size of structure for version checking. + aclCLLoader clAPI; // Pointer to the compiler API. + aclFELoader feAPI; // Pointer to the FE Loader API. + aclOptLoader optAPI; // Pointer to the Opt Loader API. + aclLinkLoader linkAPI; // Pointer to the Link Loader API. + aclCGLoader cgAPI; // Pointer to the CG Loader API. + aclBELoader beAPI; // Pointer to the BE Loader API. + aclSCLoader scAPI; // Pointer to the SC Loader API. + aclCompilerOptions *opts; // The options structure for the compiler. + void *llvm_shutdown; // Pointer to the llvm shutdown object. + char *buildLog; // Pointer to the current build log. + unsigned logSize; // Size of the current build log. + aclLoaderData *apiData; // pointer to data store for the compiler API loader. +} aclCompilerHandle_0_8; + +//! Structure that holds the OpenCL compiler and various loaders. +typedef struct _acl_compiler_rec_0_8_1 { + ACL_STRUCT_HEADER; + aclCLLoader clAPI; // Pointer to the compiler API. + aclFELoader feAPI; // Pointer to the FE Loader API. + aclOptLoader optAPI; // Pointer to the Opt Loader API. + aclLinkLoader linkAPI; // Pointer to the Link Loader API. + aclCGLoader cgAPI; // Pointer to the CG Loader API. + aclBELoader beAPI; // Pointer to the BE Loader API. + aclSCLoader scAPI; // Pointer to the SC Loader API. + AllocFunc alloc; + FreeFunc dealloc; + aclCompilerOptions *opts; // The options structure for the compiler. + void *llvm_shutdown; // Pointer to the llvm shutdown object. + char *buildLog; // Pointer to the current build log. + unsigned logSize; // Size of the current build log. + aclLoaderData *apiData; // pointer to data store for the compiler API loader. +} aclCompilerHandle_0_8_1; + +#endif // _ACL_STRUCTS_0_8_H_ diff --git a/rocclr/compiler/lib/include/v0_8/aclTypes.h b/rocclr/compiler/lib/include/v0_8/aclTypes.h new file mode 100644 index 0000000000..cba640591b --- /dev/null +++ b/rocclr/compiler/lib/include/v0_8/aclTypes.h @@ -0,0 +1,88 @@ +// +// Copyright (c) 2012 Advanced Micro Devices, Inc. All rights reserved. +// +#ifndef _ACL_API_TYPES_0_8_H_ +#define _ACL_API_TYPES_0_8_H_ +#include "aclDefs.h" +#include +#include + +// Typedefs that always point to the most recent versions of the objects. +typedef struct _acl_md_arg_type_0_8 aclArgData; +typedef struct _acl_md_printf_fmt_0_8 aclPrintfFmt; +typedef struct _acl_metadata_0_8 aclMetadata; +typedef struct _acl_device_caps_rec_0_8 aclDevCaps; +typedef struct _acl_target_info_rec_0_8 aclTargetInfo; +typedef struct _acl_bif_rec_0_8_1 aclBinary; +typedef struct _acl_binary_opts_rec_0_8_1 aclBinaryOptions; +typedef struct _acl_compiler_rec_0_8_1 aclCompiler; +typedef struct _acl_compiler_opts_rec_0_8_1 aclCompilerOptions; +typedef struct _acl_options_0_8* aclOptions; // Opaque pointer to amd::Options +typedef struct _acl_binary_0_8* aclBIF; // Opaque pointer to bifbase +typedef struct _acl_common_loader_rec_0_8 aclCommonLoader; +typedef struct _acl_cl_loader_rec_0_8 aclCLLoader; +typedef struct _acl_sc_loader_rec_0_8 aclSCLoader; +typedef struct _acl_fe_loader_rec_0_8 aclFELoader; +typedef struct _acl_link_loader_rec_0_8 aclLinkLoader; +typedef struct _acl_opt_loader_rec_0_8 aclOptLoader; +typedef struct _acl_cg_loader_rec_0_8 aclCGLoader; +typedef struct _acl_be_loader_rec_0_8 aclBELoader; +typedef struct _acl_llvm_module_0_8* aclModule; // Opaque pointer to llvm::Module +typedef struct _acl_llvm_context_0_8* aclContext; // Opaque pointer to llvm::Context +typedef struct _acl_loader_data_0_8* aclLoaderData; // Opaque pointer to loader data + +#include "aclEnums.h" +// Typedefs for enumerations +typedef enum _acl_error_enum_0_8 acl_error; +typedef enum _comp_device_caps_enum_0_8 compDeviceCaps; +typedef enum _comp_opt_settings_enum_0_8 compOptSettings; +typedef enum _acl_dev_type_enum_0_8 aclDevType; +typedef enum _acl_cl_version_enum_0_8 aclCLVersion; +typedef enum _acl_type_enum_0_8 aclType; +typedef enum _rt_query_types_enum_0_8 aclQueryType; +typedef enum _rt_gpu_caps_enum_0_8 aclGPUCaps; +typedef enum _rt_gpu_resource_enum_0_8 aclGPUResource; +typedef enum _rt_gpu_mem_sizes_enum_0_8 aclGPUMemSizes; +typedef enum _acl_arg_type_enum_0_8 aclArgType; +typedef enum _acl_data_type_enum_0_8 aclArgDataType; +typedef enum _acl_memory_type_enum_0_8 aclMemoryType; +typedef enum _acl_access_type_enum_0_8 aclAccessType; +typedef enum _bif_version_enum_0_8 aclBIFVersion; +typedef enum _bif_platform_enum_0_8 aclPlatform; +typedef enum _bif_sections_enum_0_8 aclSections; +typedef enum _acl_loader_type_enum_0_8 aclLoaderType; + +#include "aclFunctors.h" +// Typedefs for function pointers +typedef aclLogFunction_0_8 aclLogFunction; +typedef InsertSec_0_8 InsertSec; +typedef RemoveSec_0_8 RemoveSec; +typedef ExtractSec_0_8 ExtractSec; +typedef InsertSym_0_8 InsertSym; +typedef RemoveSym_0_8 RemoveSym; +typedef ExtractSym_0_8 ExtractSym; +typedef QueryInfo_0_8 QueryInfo; +typedef Compile_0_8 Compile; +typedef Link_0_8 Link; +typedef AddDbgArg_0_8 AddDbgArg; +typedef RemoveDbgArg_0_8 RemoveDbgArg; +typedef CompLog_0_8 CompLog; +typedef RetrieveType_0_8 RetrieveType; +typedef SetType_0_8 SetType; +typedef ConvertType_0_8 ConvertType; +typedef Disassemble_0_8 Disassemble; +typedef GetDevBinary_0_8 GetDevBinary; +typedef LoaderInit_0_8 LoaderInit; +typedef LoaderFini_0_8 LoaderFini; +typedef FEToIR_0_8 FEToIR; +typedef SourceToISA_0_8 SourceToISA; +typedef IRPhase_0_8 IRPhase; +typedef LinkPhase_0_8 LinkPhase; +typedef CGPhase_0_8 CGPhase; +typedef DisasmISA_0_8 DisasmISA; +typedef AllocFunc_0_8 AllocFunc; +typedef FreeFunc_0_8 FreeFunc; + +#include "aclStructs.h" + +#endif // _CL_API_TYPES_0_8_H_ diff --git a/rocclr/compiler/lib/loaders/elf/elf.cpp b/rocclr/compiler/lib/loaders/elf/elf.cpp new file mode 100644 index 0000000000..7580d876fb --- /dev/null +++ b/rocclr/compiler/lib/loaders/elf/elf.cpp @@ -0,0 +1,1548 @@ +// +// Copyright (c) 2010 Advanced Micro Devices, Inc. All rights reserved. +// +#include "elf.hpp" + +#include +#include +#include + +#if defined(__linux__) +#include +#endif + +#include "os/os.hpp" +#include "_libelf.h" +namespace amd { + +using namespace oclelfutils; + +#if !defined(ELFMAG) +#define ELFMAG "\177ELF" +#define SELFMAG 4 +#endif + +/* + Opague data type definition. +*/ +struct symbol_handle { + union { + Elf64_Sym sym64; + Elf32_Sym sym32; + } u; +}; + +typedef struct { + OclElf::oclElfSections id; + const char *name; + Elf_Type d_type; + uint64_t d_align; // section alignment in bytes + Elf32_Word sh_type; // section type + Elf32_Word sh_flags; // section flags + const char *desc; +} OclElfSectionsDesc; + +namespace { + // Objects that are visible only within this module + + const OclElfSectionsDesc oclElfSecDesc[] = + { + { OclElf::LLVMIR, ".llvmir", ELF_T_BYTE, 1, SHT_PROGBITS, 0, + "ASIC-independent LLVM IR" }, + { OclElf::SOURCE, ".source", ELF_T_BYTE, 1, SHT_PROGBITS, 0, + "OpenCL source" }, + { OclElf::ILTEXT, ".amdil", ELF_T_BYTE, 1, SHT_PROGBITS, 0, + "AMD IL text" }, + { OclElf::ASTEXT, ".astext", ELF_T_BYTE, 1, SHT_PROGBITS, 0, + "X86 assembly text" }, + { OclElf::CAL, ".text", ELF_T_BYTE, 1, SHT_PROGBITS, SHF_ALLOC | SHF_EXECINSTR, + "AMD CalImage" }, + { OclElf::DLL, ".text", ELF_T_BYTE, 1, SHT_PROGBITS, SHF_ALLOC | SHF_EXECINSTR, + "x86 dll" }, + { OclElf::STRTAB, ".strtab", ELF_T_BYTE, 1, SHT_STRTAB, SHF_STRINGS, + "String table" }, + { OclElf::SYMTAB, ".symtab", ELF_T_SYM, sizeof(Elf64_Xword), SHT_SYMTAB, 0, + "Symbol table" }, + { OclElf::RODATA, ".rodata", ELF_T_BYTE, 1, SHT_PROGBITS, SHF_ALLOC, + "Read-only data" }, + { OclElf::SHSTRTAB, ".shstrtab", ELF_T_BYTE, 1, SHT_STRTAB, SHF_STRINGS, + "Section names" }, + { OclElf::NOTES, ".note", ELF_T_NOTE, 1, SHT_NOTE, 0, + "used by loader for notes" }, + { OclElf::COMMENT, ".comment", ELF_T_BYTE, 1, SHT_PROGBITS, 0, + "Version string" }, + { OclElf::ILDEBUG, ".debugil", ELF_T_BYTE, 1, SHT_PROGBITS, 0, + "AMD Debug IL" }, + { OclElf::DEBUG_INFO, ".debug_info", ELF_T_BYTE, 1, SHT_PROGBITS, 0, + "Dwarf debug info" }, + { OclElf::DEBUG_ABBREV, ".debug_abbrev", ELF_T_BYTE, 1, SHT_PROGBITS, 0, + "Dwarf debug abbrev" }, + { OclElf::DEBUG_LINE, ".debug_line", ELF_T_BYTE, 1, SHT_PROGBITS, 0, + "Dwarf debug line" }, + { OclElf::DEBUG_PUBNAMES, ".debug_pubnames", ELF_T_BYTE, 1, SHT_PROGBITS, 0, + "Dwarf debug pubnames" }, + { OclElf::DEBUG_PUBTYPES, ".debug_pubtypes", ELF_T_BYTE, 1, SHT_PROGBITS, 0, + "Dwarf debug pubtypes" }, + { OclElf::DEBUG_LOC, ".debug_loc", ELF_T_BYTE, 1, SHT_PROGBITS, 0, + "Dwarf debug loc" }, + { OclElf::DEBUG_ARANGES, ".debug_aranges", ELF_T_BYTE, 1, SHT_PROGBITS, 0, + "Dwarf debug aranges" }, + { OclElf::DEBUG_RANGES, ".debug_ranges", ELF_T_BYTE, 1, SHT_PROGBITS, 0, + "Dwarf debug ranges" }, + { OclElf::DEBUG_MACINFO, ".debug_macinfo", ELF_T_BYTE, 1, SHT_PROGBITS, 0, + "Dwarf debug macinfo" }, + { OclElf::DEBUG_STR, ".debug_str", ELF_T_BYTE, 1, SHT_PROGBITS, 0, + "Dwarf debug str" }, + { OclElf::DEBUG_FRAME, ".debug_frame", ELF_T_BYTE, 1, SHT_PROGBITS, 0, + "Dwarf debug frame" }, + { OclElf::JITBINARY, ".text", ELF_T_BYTE, 1, SHT_PROGBITS, SHF_ALLOC | SHF_EXECINSTR, + "x86 JIT Binary" }, + { OclElf::CODEGEN, ".cg", ELF_T_BYTE, 1, SHT_PROGBITS, 0, + "Target dependent IL" }, + { OclElf::TEXT, ".text", ELF_T_BYTE, 1, SHT_PROGBITS, SHF_ALLOC | SHF_EXECINSTR, + "Device specific ISA" }, + { OclElf::INTERNAL, ".internal", ELF_T_BYTE, 1, SHT_PROGBITS, 0, + "Internal usage" }, + { OclElf::SPIR, ".spir", ELF_T_BYTE, 1, SHT_PROGBITS, 0, + "Vendor/Device-independent LLVM IR" }, + }; + + // index 0 is reserved and must be there (NULL section) + const char shstrtab[] = { + /* index 0 */ '\0', + /* index 1 */ '.', 's', 'h', 's', 't', 'r', 't', 'a', 'b', '\0', + /* index 11 */ '.', 's', 't', 'r', 't', 'a', 'b', '\0' + }; + +#define SHSTRTAB_NAME_NDX 1 +#define STRTAB_NAME_NDX 11 + + // index 0 is reserved and must be there (NULL name) + const char strtab[] = { + /* index 0 */ '\0' + }; + +} + + bool +isElfMagic(const char* p) +{ + if (p==NULL || strncmp(p, ELFMAG, SELFMAG) != 0) { + return false; + } + return true; +} + +// + bool +isElfHeader(const char* p, signed char ec) +{ + if (!isElfMagic(p)) { + return false; + } + signed char libVersion = elf_version(EV_CURRENT); + signed char fileVersion = p[EI_VERSION]; + signed char elfClass = p[EI_CLASS]; + if( fileVersion > libVersion) { + return false; + } + + // class check: + if ( elfClass != ec) { + return false; + } + + return true; +} + + bool +isCALTarget(const char* p, signed char ec) +{ + if (!isElfMagic(p)) { + return false; + } + + Elf64_Half machine; + if (ec == ELFCLASS32) { + machine = ((Elf32_Ehdr*)p)->e_machine; + + } + else { + machine = ((Elf64_Ehdr*)p)->e_machine; + } + + if ( (machine >= OclElf::CAL_FIRST) && (machine <= OclElf::CAL_LAST) ) { + return true; + } + + return false; +} + + +/////////////////////////////////////////////////////////////// +////////////////////// elf initializers /////////////////////// +/////////////////////////////////////////////////////////////// + +OclElf::OclElf ( + unsigned char eclass, + const char* rawElfBytes, + uint64_t rawElfSize, + const char* elfFileName, + Elf_Cmd elfcmd + ) +: _fd (-1), + _fname (elfFileName), + _isTempFile(false), + _e (0), + _err (), + _eclass (eclass), + _rawElfBytes (rawElfBytes), + _rawElfSize (rawElfSize), + _elfCmd (elfcmd), + _elfMemory(), + _shstrtab_ndx (0), + _strtab_ndx (0) +{ + if (rawElfBytes != NULL) { + /* + In general, 'eclass' should be the same as rawElfBytes's. 'eclass' is what the runtime + will use for generating an ELF, and therefore it expects the input ELF to have this 'eclass'. + However, GPU needs to accept both 32-bit and 64-bit ELF for compatibility (we used to + generate 64-bit ELF, which is the bad design in the first place). Here we just uses eclass + from rawElfBytes, and overrides the input 'eclass'. + */ + _eclass = (unsigned char)rawElfBytes[EI_CLASS]; + } + (void)Init(); +} + +OclElf::~OclElf() +{ +#if 0 + Elf_Cmd c = (_errCmd == ELF_C_READ) ? ELF_C_NULL : _errCmd; + if (elf_update(_e, c < 0) { + _err.xfail("OclElf::Fini() : elf_update() failed: %s", elf_errmsg(-1); + return; + } +#endif + _err.Fini(); + + elf_end(_e); + _e = 0; + + if (_fd != -1) { + xclose(_err, _fname, _fd); + char* tname= const_cast(_fname); + if (_isTempFile) { + unlink(tname); + _isTempFile = false; + } + free(tname); + _fd = -1; + _fname = NULL; + + } + + elfMemoryRelease(); +} + + bool +OclElf::Clear() +{ + if (_e) { + elf_end(_e); + _e = NULL; + } + + if (_fd != -1) { + if (xclose(_err, _fname, _fd) < 0) { + return false; + } + _fd = -1; + } + + elfMemoryRelease(); + + _err.Fini(); + + // Re-initialize the object + Init(); + + return !hasError(); +} + + +/* + Initialize OclElf object + */ + bool +OclElf::Init() +{ + _err.Init(); + + // Create a temporary file if it is needed + if (_elfCmd != ELF_C_READ) { + std::string tempFileName; + size_t sz; + if (_fname == NULL) { + tempFileName = amd::Os::getTempFileName(); + _fname = tempFileName.c_str(); + sz = tempFileName.size() + 1; + _isTempFile = true; + } + else { + sz = strlen(_fname) + 1; + } + + char* tname = (char*)xmalloc(_err, sz); + if (tname == 0) { + _err.xfail("OclElf::Init() failed to malloc()"); + return false; + } + strcpy(tname, _fname); + _fname = static_cast(tname); + } + + if (elf_version(EV_CURRENT) == EV_NONE) { + _err.xfail("OclElf::Init(): Application expects CURRENT elf version"); + return false; + } + int oflag, pmode; +#if defined(_MSC_VER) + if (_elfCmd == ELF_C_READ) { + oflag = _O_RDONLY | _O_BINARY; + } + else { + oflag = _O_CREAT | _O_RDWR | _O_TRUNC | _O_BINARY; + } + pmode = _S_IREAD | _S_IWRITE; +#else + if (_elfCmd == ELF_C_READ) { + oflag = O_RDONLY; + } + else { + oflag = O_CREAT | O_RDWR | O_TRUNC; + } + pmode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH; // 0644 +#endif + if ((_fd == -1) && (_rawElfBytes == NULL)) { + // case 1: elf object is in file '_fname' + + assert ((_fname != NULL) && "ELF file name should be provided"); + + _fd = xopen(_err, _fname, oflag, pmode); + if (_fd < 0) { + _err.xfail("OclElf::Init(): Cannot Open File %s!", _fname); + return false; + } + + _e = elf_begin(_fd, _elfCmd, NULL, NULL); + if (_e == NULL) { + _err.xfail ("OclElf::Init(): elf_begin failed"); + return false; + } + } + else if (_fd == -1) { + // case 2: elf object is in memory + if (_elfCmd == ELF_C_READ) { + assert ((_fname == NULL) && "ELF file name should not be provided for a read only elf."); + } else { + _fd = xopen(_err, _fname, oflag, pmode); + if (_fd < 0) { + _err.xfail("OclElf::Init(): Cannot Open File %s!", _fname); + return false; + } + } + + // const_cast is safe + _e = elf_memory(const_cast(_rawElfBytes), _rawElfSize, NULL); + if ( _e == NULL) { + _err.xfail("OclElf::Init(): elf_memory failed: %s", + elf_errmsg(-1)); + return false; + } + // If _fd != -1, then we are a read/write and not just a read, so change accordingly. + if (_fd != -1) { + _e->e_fd = _fd; + _e->e_cmd = _elfCmd; + } + } + else { // _fd != -1 + // case 3: elf object is in a file with file descriptor '_fd' + + _e = elf_begin(_fd, _elfCmd, NULL, NULL); + if (_e == NULL) { + _err.xfail ("OclElf::Init(): elf_begin failed: %s", + elf_errmsg(-1)); + return false; + } + } + + if (!InitElf()) { + return false; + } + + // Success + return true; +} + +/* + Return true: if InitElf() is successful + Return false: if InitElf() failed. + */ + bool +OclElf::InitElf () +{ + assert (_e && "libelf object should have been created already"); + + if (_elfCmd != ELF_C_WRITE) { + // Set up _shstrtab_ndx and _strtab_ndx + GElf_Ehdr gehdr; + if (gelf_getehdr(_e, &gehdr) == NULL) { + _err.xfail("OclElf::InitElf() failed in gelf_getehdr()- %s", + elf_errmsg(-1)); + return false; + } + + _shstrtab_ndx = gehdr.e_shstrndx; + + Elf_Scn* scn; + if (!getSectionDesc(scn, STRTAB)) { + _err.xfail("OclElf::InitElf() failed in getSectionDesc(STRTAB)"); + return false; + } + + // Sanity check. Each ELF binary should have STRTAB ! + if (scn != NULL) { + _strtab_ndx = elf_ndxscn(scn); + } + + return true; + } + + + /*********************************/ + /******** ELF_C_WRITE ************/ + /*********************************/ + + // + // 1. Create ELF header + // + if (_eclass == ELFCLASS32) { + Elf32_Ehdr* ehdr32 = elf32_newehdr(_e); + if (ehdr32 == NULL) { + _err.xfail("OclElf::InitElf() failed in elf32_newehdr: %s.", + elf_errmsg(-1)); + return false; + } + } + else { + Elf64_Ehdr* ehdr64 = elf64_newehdr(_e); + if (ehdr64 == NULL) { + _err.xfail("OclElf::InitElf() failed in elf32_newehdr : %s.", + elf_errmsg(-1)); + return false; + } + } + +#if 0 + if (elf_update(_e, ELF_C_NULL) < 0) { + _err.xfail("elf_update() failed"); + return -1; + } +#endif + + // + // 2. Create ELF shstrtab + // + Elf_Scn* scn_shstrtab = elf_newscn(_e); + if (scn_shstrtab == NULL) { + _err.xfail("Elf::InitElf() failed in elf_newscn : %s", elf_errmsg(-1)); + return false; + } + + /* addng ELF_Data descriptor associated with section scn */ + Elf_Data* data_shstrtab = createElfData(scn_shstrtab, SHSTRTAB, + const_cast(shstrtab), (uint64_t)sizeof(shstrtab), false); + if (data_shstrtab == NULL) { + return false; + } + + if (!createShdr(SHSTRTAB, scn_shstrtab, SHSTRTAB_NAME_NDX)) { + return false; + } + + // Save shstrtab section index + _shstrtab_ndx = elf_ndxscn(scn_shstrtab); +#if defined(BSD_LIBELF) + elf_setshstrndx(_e, _shstrtab_ndx); +#else + elfx_update_shstrndx(_e, _shstrtab_ndx); +#endif + +#if 0 + if (elf_update(_e, ELF_C_NULL) < 0) { + _err.xfail("elf_update() failed"); + return -1; + } +#endif + + // + // 3. Create .strtab section + // + Elf_Scn* scn_strtab = elf_newscn(_e); + if (scn_strtab == NULL) { + _err.xfail("Elf::InitElf() failed in elf_newscn : %s", elf_errmsg(-1)); + return false; + } + + /* addng ELF_Data descriptor associated with section scn */ + Elf_Data* data_strtab = createElfData(scn_strtab, STRTAB, + const_cast(strtab), (uint64_t)sizeof(strtab), false); + if (data_strtab == NULL) { + return false; + } + + if (!createShdr(STRTAB, scn_strtab, STRTAB_NAME_NDX)) { + return false; + } + + // Save strtab section index + _strtab_ndx = elf_ndxscn(scn_strtab); + + // Need to update section header + if (elf_update(_e, ELF_C_NULL) < 0) { + _err.xfail("elf_update() failed"); + return false; + } + + // + // 4. Create the symbol table + // + + // Create the first reserved symbol (undefined symbol) + size_t sym_sz = (_eclass == ELFCLASS32) ? sizeof(Elf32_Sym) : sizeof(Elf64_Sym); + void* sym = oclelf_calloc(sym_sz); + if (sym == NULL) { + _err.xfail("OclElf::InitElf() failed to alloc memory"); + return false; + } + + Elf_Scn* scn_symtab = newSection(SYMTAB, sym, sym_sz, false); + if (scn_symtab == NULL) { + // Use newSection()'s error message. + return false; + } + + return true; +} + +Elf_Data* +OclElf::createElfData( + Elf_Scn*& scn, + oclElfSections id, + void* d_buf, + uint64_t d_size, + bool do_copy + ) +{ + /* addng Elf_Data descriptor associated with section scn */ + Elf_Data* data = elf_newdata(scn); + if (data == NULL) { + _err.xfail("OclElf::createElfData() failed in elf_newdata() - %s", + elf_errmsg(-1)); + return NULL; + } + + void* newbuf; + if (do_copy) { + newbuf = oclelf_allocAndCopy((void*)d_buf, d_size); + } + else { + newbuf = d_buf; + } + + data->d_align = oclElfSecDesc[id].d_align; + data->d_off = 0LL; + data->d_buf = newbuf; + data->d_type = oclElfSecDesc[id].d_type; + data->d_size = d_size; + data->d_version = EV_CURRENT ; + + if (elf_update(_e, ELF_C_NULL) < 0) { + _err.xfail("elf_update() failed"); + return NULL; + } + return data; +} + +bool +OclElf::createShdr ( + oclElfSections id, + Elf_Scn*& scn, + Elf64_Word shname, + Elf64_Word shlink + ) +{ + if (_eclass == ELFCLASS32) { + Elf32_Shdr* shdr32 = elf32_getshdr(scn); + if (shdr32 == NULL) { + _err.xfail("Elf::createShdr() failed in elf32_getshdr(): %s.", elf_errmsg(-1)); + return false; + } + + shdr32->sh_name = (Elf32_Word)shname; + shdr32->sh_type = (Elf32_Word)oclElfSecDesc[id].sh_type; + shdr32->sh_flags = (Elf32_Word)oclElfSecDesc[id].sh_flags; + + shdr32->sh_link = (Elf32_Word)shlink; + } + else { + Elf64_Shdr* shdr64 = elf64_getshdr(scn); + if (shdr64 == NULL) { + _err.xfail("Elf::InitElf() failed in elf64_getshdr(): %s.", elf_errmsg(-1)); + return false; + } + + shdr64->sh_name = (Elf64_Word)shname; + shdr64->sh_type = (Elf64_Word)oclElfSecDesc[id].sh_type; + shdr64->sh_flags = (Elf64_Xword)oclElfSecDesc[id].sh_flags; + + shdr64->sh_link = (Elf64_Word)shlink; + } + return true; +} + + + bool +OclElf::getTarget(uint16_t& machine, oclElfPlatform& platform) +{ + assert(_e != 0); + + GElf_Ehdr ehdrO; + GElf_Ehdr *ehdr = gelf_getehdr(_e, &ehdrO); + if (ehdr == NULL) { + return false; + } + + Elf64_Half mach = ehdr->e_machine; + if ((mach >= CPU_FIRST) && (mach <= CPU_LAST)) { + platform = CPU_PLATFORM; + machine = mach - CPU_BASE; + } + else if ( (mach >= CAL_FIRST) && (mach <= CAL_LAST)) { + platform = CAL_PLATFORM; + machine = mach - CAL_BASE; + } else if (mach == EM_386 + || mach == EM_HSAIL + || mach == EM_HSAIL_64 + || mach == EM_AMDIL + || mach == EM_AMDIL_64 + || mach == EM_X86_64) { + platform = COMPLIB_PLATFORM; + machine = mach; + } else { + // Invalid machine + return false; + } + + return true; +} + + bool +OclElf::setTarget(uint16_t machine, oclElfPlatform platform) +{ + assert(_e != 0); + + Elf64_Half mach; + if (platform == CPU_PLATFORM) + mach = machine + CPU_BASE; + else if (platform == CAL_PLATFORM) + mach = machine + CAL_BASE; + else + mach = machine; + + if (_eclass == ELFCLASS32) { + Elf32_Ehdr* ehdr32 = elf32_getehdr(_e); + + if (ehdr32 == NULL) { + _err.xfail("setTarget() : failed in elf32_getehdr()- %s.", elf_errmsg(-1)); + return false; + } + + ehdr32->e_ident[EI_DATA] = ELFDATA2LSB; + ehdr32->e_type = ET_NONE; + ehdr32->e_machine = (Elf32_Half)mach; + } + else { + Elf64_Ehdr* ehdr64 = elf64_getehdr(_e); + + if (ehdr64 == NULL) { + _err.xfail("setTarget() : failed in elf64_getehdr()- %s.", elf_errmsg(-1)); + return false; + } + + ehdr64->e_ident[EI_DATA] = ELFDATA2LSB; + ehdr64->e_type = ET_NONE; + ehdr64->e_machine = mach; + } + + return true; +} + +bool +OclElf::getType(uint16_t &type) { + assert(_e != 0); + + if (_eclass == ELFCLASS32) { + Elf32_Ehdr* ehdr32 = elf32_getehdr(_e); + + if (ehdr32 == NULL) { + _err.xfail("setTarget() : failed in elf32_getehdr()- %s.", elf_errmsg(-1)); + return false; + } + + type = ehdr32->e_type; + } + else { + Elf64_Ehdr* ehdr64 = elf64_getehdr(_e); + + if (ehdr64 == NULL) { + _err.xfail("setTarget() : failed in elf64_getehdr()- %s.", elf_errmsg(-1)); + return false; + } + + type = ehdr64->e_type; + } + + return true; +} + +bool +OclElf::setType(uint16_t type) { + assert(_e != 0); + + if (_eclass == ELFCLASS32) { + Elf32_Ehdr* ehdr32 = elf32_getehdr(_e); + + if (ehdr32 == NULL) { + _err.xfail("setTarget() : failed in elf32_getehdr()- %s.", elf_errmsg(-1)); + return false; + } + + ehdr32->e_type = type; + } + else { + Elf64_Ehdr* ehdr64 = elf64_getehdr(_e); + + if (ehdr64 == NULL) { + _err.xfail("setTarget() : failed in elf64_getehdr()- %s.", elf_errmsg(-1)); + return false; + } + + ehdr64->e_type = type; + } + + return true; +} + +bool +OclElf::getFlags(uint32_t &flag) { + assert(_e != 0); + + if (_eclass == ELFCLASS32) { + Elf32_Ehdr* ehdr32 = elf32_getehdr(_e); + + if (ehdr32 == NULL) { + _err.xfail("setTarget() : failed in elf32_getehdr()- %s.", elf_errmsg(-1)); + return false; + } + + flag = ehdr32->e_flags; + } + else { + Elf64_Ehdr* ehdr64 = elf64_getehdr(_e); + + if (ehdr64 == NULL) { + _err.xfail("setTarget() : failed in elf64_getehdr()- %s.", elf_errmsg(-1)); + return false; + } + + flag = ehdr64->e_flags; + } + + return true; +} + +bool +OclElf::setFlags(uint32_t flag) { + assert(_e != 0); + + if (_eclass == ELFCLASS32) { + Elf32_Ehdr* ehdr32 = elf32_getehdr(_e); + + if (ehdr32 == NULL) { + _err.xfail("setTarget() : failed in elf32_getehdr()- %s.", elf_errmsg(-1)); + return false; + } + + ehdr32->e_flags = flag; + } + else { + Elf64_Ehdr* ehdr64 = elf64_getehdr(_e); + + if (ehdr64 == NULL) { + _err.xfail("setTarget() : failed in elf64_getehdr()- %s.", elf_errmsg(-1)); + return false; + } + + ehdr64->e_flags = flag; + } + + return true; +} + +/* + returns true if success; return false if fail. + scn will return scn for 'id'. + */ + bool +OclElf::getSectionDesc(Elf_Scn*& scn, OclElf::oclElfSections id) const +{ + if ( ((id == SHSTRTAB) && (_shstrtab_ndx != 0)) || + ((id == STRTAB) && (_strtab_ndx != 0)) ) { + // Special (efficient) processing of SHSTRTAB/STRTAB + size_t idx = (id == SHSTRTAB) ? _shstrtab_ndx : _strtab_ndx; + if ((scn = elf_getscn(_e, idx)) == NULL) { + _err.xfail("OclElf::addSectionDesc(): elf_getscn() failed - %s", + elf_errmsg(-1)); + return false; + } + } + else { + /* Search sections */ + const char* sname = oclElfSecDesc[id].name; + for (scn = elf_nextscn(_e, 0); + scn != NULL; + scn = elf_nextscn(_e, scn)) + { + size_t idx = elf_ndxscn(scn); + if ( ((idx == _shstrtab_ndx) && (_shstrtab_ndx != 0)) || + ((idx == _strtab_ndx) && (_strtab_ndx != 0)) ) { + continue; + } + + GElf_Shdr shdr; + if (gelf_getshdr(scn, &shdr) != &shdr) { + _err.xfail("OclElf::getSectionDesc() : failed in gelf_getshdr()- %s.", + elf_errmsg(-1)); + return false; + } + + /* Convert an index (to the shdr string table) to a char pointer */ + char *nm = elf_strptr(_e, _shstrtab_ndx, shdr.sh_name); + if (strcmp(sname, nm ? nm : "") == 0) { + // Found ! + break; + } + } + } + return true; +} + +/* + Return true if success; return false if fail. + data will return Elf_Data. + */ + bool +OclElf::getSectionData(Elf_Data*& data, OclElf::oclElfSections id) const +{ + assert(_e != 0); + + data = NULL; + Elf_Scn* scn; + if (!getSectionDesc(scn, id)) { + return false; + } + if (scn != NULL) { + // There is only one data descriptor (we are reading!) + data = elf_getdata(scn, 0); + } + return true; +} + +/* + Get the whole section, assuming that there is only one data descriptor + */ + bool +OclElf::getSection(OclElf::oclElfSections id, char** dst, size_t* sz) const +{ + assert((oclElfSecDesc[id].id == id) && + "oclElfSecDesc[] should be in the same order as enum oclElfSections"); + + Elf_Data* data; + if (!getSectionData(data, id)) { + _err.xfail("OclElf::getSection() failed in getSectionData()"); + return false; + } + if (data == NULL) { + *dst = NULL; + *sz = 0; + } + else { + *sz = (size_t)data->d_size; + *dst = (char*)data->d_buf; + } + + return true; +} + + +/* + API routines for manipulating symbols + */ + Sym_Handle +OclElf::nextSymbol(Sym_Handle symHandle) const +{ + size_t sz; + char* beg, *end; + + if (!getSection(SYMTAB, &beg, &sz)) { + _err.xfail("OclElf::nextSymbol() failed in getSection()"); + return NULL; + } + + if ( (beg == 0) || (sz == 0) ) { + return NULL; + } + + end = beg + sz; + if (_eclass == ELFCLASS64) { + // Skip the first dummy symbol (STT_NOTYPE) + beg += sizeof(Elf64_Sym); + + if (beg == end) { // No valid symbols in the table + return NULL; + } + + if (symHandle == NULL) { + // Return the first symbol + return reinterpret_cast(beg); + } + + // Return the next symbol + Elf64_Sym* sym64 = reinterpret_cast(symHandle); + sym64++; + if (reinterpret_cast(sym64) == end) { + return NULL; + } + return reinterpret_cast(sym64); + } + else { + // Skip the first dummy symbol (STT_NOTYPE) + beg += sizeof(Elf32_Sym); + + if (beg == end) { // No valid symbols in the table + return NULL; + } + + if (symHandle == NULL) { + // Return the first symbol + return reinterpret_cast(beg); + } + + Elf32_Sym* sym32 = reinterpret_cast(symHandle); + sym32++; + if (reinterpret_cast(sym32) == end) { + return NULL; + } + return reinterpret_cast(sym32); + } + + // UNREACHABLE + return NULL; +} + +/* + Given a symbol handle, return info for this symbol + */ + bool +OclElf::getSymbolInfo(Sym_Handle symHandle, SymbolInfo* symInfo) const +{ + assert(_e != 0); + + Elf_Scn *scn; + char* sym_name; + Elf64_Addr st_value; /* visibility */ + Elf64_Xword st_size; /* index of related section */ + + if (_eclass == ELFCLASS64) { + Elf64_Sym* sym64 = reinterpret_cast(symHandle); + + sym_name = elf_strptr(_e, _strtab_ndx, sym64->st_name); + st_value = (Elf64_Addr)(sym64->st_value); + st_size = (Elf64_Xword)(sym64->st_size); + + // get section + scn = elf_getscn(_e, sym64->st_shndx); + } + else { + Elf32_Sym* sym32 = reinterpret_cast(symHandle); + + sym_name = elf_strptr(_e, _strtab_ndx, sym32->st_name); + st_value = (Elf64_Addr)(sym32->st_value); + st_size = (Elf64_Xword)(sym32->st_size); + + // get section + scn = elf_getscn(_e, sym32->st_shndx); + } + + GElf_Shdr gshdr; + if (gelf_getshdr(scn, &gshdr) == NULL) { + _err.xfail("OclElf::getSymbolInfo() failed in gelf_getshdr() - %s.", + elf_errmsg(-1)); + return false; + } + char* sec_name = elf_strptr(_e, _shstrtab_ndx, gshdr.sh_name); + + // Assume there is only one Elf_Data. For reading, it's always true + Elf_Data* data = elf_getdata(scn, 0); + if (data == NULL) { + symInfo->sec_addr = (char*)NULL; + symInfo->sec_size = 0; + symInfo->address = (char*)NULL; + symInfo->size = (uint64_t)0; + } + else { + symInfo->sec_addr = (char*)data->d_buf; + symInfo->sec_size = data->d_size; + symInfo->address = symInfo->sec_addr + (size_t)st_value; + symInfo->size = (uint64_t)st_size; + } + symInfo->sec_name = sec_name; + symInfo->sym_name = sym_name; + + return true; +} + +/* + AddSectionData() will add data into a section. Return the offset + of the data in this section if success; return -1 if fail. + */ +bool +OclElf::addSectionData ( + Elf64_Xword& outOffset, + oclElfSections id, + const void* buffer, + size_t size, + bool do_copy // true if buffer needs to be copied + ) +{ + outOffset = 0; + const char* secName = oclElfSecDesc[id].name; + GElf_Shdr shdr; + Elf_Scn* scn; + if (!getSectionDesc(scn, id)) { + return false; + } + assert (scn && "Elf_Scn should have been created already"); + + if (gelf_getshdr(scn, &shdr) != &shdr) { + _err.xfail("OclElf::addSectionData(): gelf_getshdr() failed - %s", + elf_errmsg(-1)); + return false; + } + outOffset = (Elf64_Xword)shdr.sh_size; + + /* addng Elf_Data descriptor associated with section scn */ + Elf_Data* data = createElfData(scn, id, const_cast(buffer), + (uint64_t)size, do_copy); + if (data == NULL) { + return false; + } + + return true; +} + +/* + getShdrNdx() returns an index to the .shstrtab in 'outNdx' for "name" if it + is in .shstrtab (outNdx == 0 means it is not in .shstrtab). It return true if + it is successful; return false if en error occured. + */ + bool +OclElf::getShstrtabNdx(Elf64_Word& outNdx, const char* name) +{ + outNdx = 0; + + // .shstrtab must be created already + Elf_Scn* scn = elf_getscn(_e, _shstrtab_ndx); + if (scn == NULL) { + _err.xfail("OclElf::getShdrNdx() failed in elf_getscn for section .shstrtab - %s", + elf_errmsg(-1)); + return false; + } + + Elf_Data* data = elf_getdata(scn, NULL); + if (data == NULL) { + _err.xfail("Elf::getShdrNdx() failed in elf_getdata for section .shstrtab - %s", + elf_errmsg(-1)); + return false; + } + + size_t name_sz = strlen(name); + uint64_t data_offset = 0; + do { + if (data->d_size > name_sz) { + char* base = (char*)data->d_buf; + char* end = base + (size_t)data->d_size; + char* b = base; + char* e; + + while ( b != end) { + e = b; + + // find the next 0 char + while ( (e != end) && (*e != 0) ) { + e++; + } + + if ((e != end) && ((size_t)(e - b) == name_sz) && + (strcmp(b, name) == 0)) { + outNdx = (Elf64_Word)((b - base) + data_offset); + return true; + } + b = e+1; + } + } + data_offset += data->d_size; + } while ((data = elf_getdata(scn, data)) != NULL); + + return true; +} + +/* + newSection() assumes that .shstrtab and .strtab have been created already. + Return the pointer to the new section if success; return 0 if fail. + */ +Elf_Scn* +OclElf::newSection ( + OclElf::oclElfSections id, + const void* d_buf, + size_t d_size, + bool do_copy + ) +{ + Elf64_Word sh_name; + if (!getShstrtabNdx(sh_name, oclElfSecDesc[id].name)) { + _err.xfail("OclElf::newSection() failed in getShstrtabNdx() for section %s", + oclElfSecDesc[id].name); + return NULL; + } + + if (sh_name == 0) { // Need to create a new entry for this section name + Elf64_Xword offset; + if (!addSectionData(offset, SHSTRTAB, oclElfSecDesc[id].name, + strlen(oclElfSecDesc[id].name) + 1, false)) { + _err.xfail("OclElf::newSection() failed in getSectionData() for section %s", + oclElfSecDesc[id].name); + return NULL; + } + sh_name = (Elf64_Word)offset; + } + + // Create a new section + Elf_Scn* scn = elf_newscn(_e); + if (scn == NULL) { + _err.xfail("OclElf::newSection() failed in elf_newscn() - %s.", + elf_errmsg(-1)); + return NULL; + } + + // If there is no data, skip creating Elf_Data + if ((d_buf != NULL) && (d_size != 0)) { + Elf_Data* data = createElfData(scn, id, + const_cast(d_buf), (uint64_t)d_size, do_copy); + if (data == NULL) { + return NULL; + } + } + + if (!createShdr(id, scn, sh_name, (id == SYMTAB) ? _strtab_ndx : 0)) { + return NULL; + } + + if (elf_update(_e, ELF_C_NULL) < 0) { + _err.xfail("OclElf::newSection(): elf_update() failed"); + return NULL; + } + + return scn; +} + +/* + Return true: success +false: fail +*/ +bool +OclElf::addSection ( + oclElfSections id, + const void* d_buf, + size_t d_size, + bool do_copy + ) +{ + assert(oclElfSecDesc[id].id == id && + "struct oclElfSecDesc should be ordered by id same as enum Elf::oclElfSections"); + + /* If section is already in elf object, simply return its address */ + Elf_Scn* scn; + if (!getSectionDesc(scn, id)) { + // Failed + return false; + } + + if (scn != NULL) { + Elf64_Xword sec_offset; + if (!addSectionData(sec_offset, id, d_buf, d_size, do_copy)) { + _err.xfail("OclElf::addSection() failed in addSectionData() for section name %s.", + oclElfSecDesc[id].name); + return false; + } + } + else { + scn = newSection(id, d_buf, d_size, do_copy); + if (scn == NULL) { + _err.xfail("OclElf::addSection() failed in newSection() for section name %s.", + oclElfSecDesc[id].name); + return false; + } + } + return true; +} + +bool +OclElf::addSymbol( + oclElfSections id, + const char* symbolName, + const void* buffer, + size_t size, + bool do_copy + ) +{ + assert(oclElfSecDesc[id].id == id && + "The order of oclElfSecDesc[] and Elf::oclElfSections mismatches."); + + const char* sectionName = oclElfSecDesc[id].name; + + bool isFunction = ((id == OclElf::CAL) || (id == OclElf::DLL) || (id == OclElf::JITBINARY)) ? true : false; + + // Get section index + Elf_Scn* scn; + if (!getSectionDesc(scn, id)) { + _err.xfail("OclElf::addSymbol() failed in getSectionDesc"); + return false; + } + if (scn == NULL) { + // Create a new section. + if ((scn = newSection(id, NULL, 0, false)) == NULL) { + _err.xfail("OclElf::addSymbol() failed in newSection"); + return false; + } + } + size_t sec_ndx = elf_ndxscn(scn); + if (sec_ndx == SHN_UNDEF) { + _err.xfail("OclElf::addSymbol() failed in elf_ndxscn() - %s.", + elf_errmsg(-1)); + return false; + } + + // Put symbolName into .strtab section + Elf64_Xword strtab_offset; + if (!addSectionData(strtab_offset, STRTAB, (void*)symbolName, + strlen(symbolName)+1, true)) { + _err.xfail("OclElf::addSymbol() failed in addSectionData(.strtab)"); + return false; + } + + // Put buffer into section + Elf64_Xword sec_offset = 0; + if ( (buffer != NULL) && (size != 0) ) { + if (!addSectionData(sec_offset, id, buffer, size, do_copy)) { + _err.xfail("OclElf::addSymbol() failed in addSectionData(%s)", sectionName); + return false; + } + } + + bool retvalue; + Elf64_Xword symtab_offset; + if (_eclass == ELFCLASS64) { + Elf64_Sym* sym64 = (Elf64_Sym*)oclelf_calloc(sizeof(Elf64_Sym)); + + sym64->st_name = (Elf64_Word)strtab_offset; + sym64->st_value = (Elf64_Addr)sec_offset; + sym64->st_size = (Elf64_Xword)size; + sym64->st_info = (isFunction)? STT_FUNC : STT_OBJECT; + sym64->st_shndx = (Elf64_Section)sec_ndx; + + retvalue = addSectionData(symtab_offset, SYMTAB, sym64, sizeof(Elf64_Sym), false); + } + else { // _eclass == ELFCLASS32 + Elf32_Sym* sym32 = (Elf32_Sym*)oclelf_calloc(sizeof(Elf32_Sym)); + + sym32->st_name = (Elf32_Word)strtab_offset; + sym32->st_value = (Elf32_Addr)sec_offset; + sym32->st_size = (Elf32_Word)size; + sym32->st_info = (isFunction)? STT_FUNC : STT_OBJECT; + sym32->st_shndx = (Elf32_Section)sec_ndx; + + retvalue = addSectionData(symtab_offset, SYMTAB, sym32, sizeof(Elf32_Sym), false); + } + + if (!retvalue) { + _err.xfail("OclElf::addSymbol() failed in addSectionData(.symtab)"); + return false; + } + + if (elf_update(_e, ELF_C_NULL) < 0) { + _err.xfail("OclElf::addSymbol() : elf_update() failed"); + return false; + } + + return true; +} + +bool +OclElf::getSymbol( + oclElfSections id, + const char* symbolName, + char** buffer, + size_t* size + ) const +{ + assert(oclElfSecDesc[id].id == id && + "The order of oclElfSecDesc[] and Elf::oclElfSections mismatches."); + if (!size || !buffer || !symbolName) { + return false; + } + // Initialize the size and buffer to invalid data points. + (*size) = 0; + (*buffer) = NULL; + for (amd::Sym_Handle s = nextSymbol(NULL); s; s = nextSymbol(s)) { + amd::OclElf::SymbolInfo si; + // Continue if symbol information is not retrieved. + if (!getSymbolInfo(s, &si)) { + continue; + } + // Continue if the symbol is in the wrong section. + if (strcmp(oclElfSecDesc[id].name, si.sec_name)) { + continue; + } + // Continue if the symbol name doesn't match. + if (strcmp(symbolName, si.sym_name)) { + continue; + } + // Set the size and the address and return true. + (*size) = si.size; + (*buffer) = si.address; + return true; + } + return false; +} + +bool +OclElf::addNote( + const char* noteName, + const char* noteDesc, + size_t nameSize, + size_t descSize + ) +{ + if ((nameSize == 0 && descSize == 0) + || (nameSize != 0 && noteName == NULL) + || (descSize != 0 && noteDesc == NULL)) { + _err.xfail("OclElf::addNote() empty note"); + return false; + } + + const oclElfSections sid = NOTES; + assert(oclElfSecDesc[sid].id == sid && + "The order of oclElfSecDesc[] and Elf::oclElfSections mismatches."); + + // Get section + Elf_Scn* scn; + if (!getSectionDesc(scn, sid)) { + _err.xfail("OclElf::addNote() failed in getSectionDesc"); + return false; + } + if (scn == NULL) { + // Create a new section. + if ((scn = newSection(sid, NULL, 0, false)) == NULL) { + _err.xfail("OclElf::addNote() failed in newSection"); + return false; + } + } + + // Put note into section + Elf64_Xword sec_offset = 0; + size_t bufsize = sizeof(Elf_Note) + nameSize + descSize; + char* buffer = (char*)oclelf_calloc(bufsize); + if (buffer == NULL) return false; + + Elf_Note* en = reinterpret_cast(buffer); + en->n_namesz = nameSize; + en->n_descsz = descSize; + en->n_type = 0; + memcpy(buffer+sizeof(Elf_Note), noteName, nameSize); + memcpy(buffer+sizeof(Elf_Note)+nameSize, noteDesc, descSize); + if (!addSectionData(sec_offset, sid, buffer, bufsize, false/*not copy*/)) { + const char* sectionName = oclElfSecDesc[sid].name; + _err.xfail("OclElf::addNote() failed in addSectionData(%s)", sectionName); + return false; + } + + if (elf_update(_e, ELF_C_NULL) < 0) { + _err.xfail("OclElf::addNote() : elf_update() failed"); + return false; + } + + return true; +} + +bool +OclElf::getNote( + const char* noteName, + char** noteDesc, + size_t *descSize + ) +{ + if (!descSize || !noteDesc || !noteName) { + return false; + } + + const oclElfSections sid = NOTES; + assert(oclElfSecDesc[sid].id == sid && + "The order of oclElfSecDesc[] and Elf::oclElfSections mismatches."); + + // Get section + Elf_Scn* scn; + if (!getSectionDesc(scn, sid)) { + _err.xfail("OclElf::getNote() failed in getSectionDesc"); + return false; + } + if (scn == NULL) { + _err.xfail("OclElf::getNote() failed: .note section not found"); + return false; + } + + // read the whole .note section + Elf_Data* data = elf_getdata(scn, 0); + + // Initialize the size and buffer to invalid data points. + *descSize = 0; + *noteDesc = NULL; + + // look for the specified note + char* ptr = (char*)data->d_buf; + while (ptr < (char*)data->d_buf + data->d_size) { + Elf_Note* note = reinterpret_cast(ptr); + + // Continue if the note name doesn't match. + if (strlen(noteName) != note->n_namesz + || strncmp(noteName, ptr+sizeof(Elf_Note), note->n_namesz) != 0) { + ptr += sizeof(Elf_Note) + note->n_namesz + note->n_descsz; + continue; + } + // Set the size and the address and return true. + *descSize = note->n_descsz; + *noteDesc = ptr + sizeof(Elf_Note) + note->n_namesz; + return true; + } + return false; +} + + bool +OclElf::dumpImage(char** buff, size_t* len) +{ + if (buff == NULL || len == NULL ) { + return false; + } + + assert ((_fd > 0) && "_fd in Elf::dumpImage should be defined"); + + // Now, write the ELF into the file + if (elf_update(_e, ELF_C_WRITE) < 0) { + _err.xfail("OclElf::dumpImage() : elf_update() failed - %s", + elf_errmsg(-1)); + return false; + } + + int buff_sz = xlseek(_err, _fname, _fd, 0, SEEK_END); + if (buff_sz == -1) { + return false; + } + + /* + The memory is owned by caller, and caller assumes that the memory is new'ed. + So, use new instead of malloc + */ + *buff = new char[buff_sz]; + if (*buff == NULL) { + _err.xfail("OclElf::dumpImage() : new char[sz] failed"); + return false; + } + + if (xlseek(_err, _fname, _fd, 0, SEEK_SET) == -1) { + _err.xfail("OclElf::dumpImage() failed in xlseek()"); + delete [] *buff; + return false; + } + + if (xread(_err, _fname, _fd, *buff, buff_sz) != buff_sz) { + _err.xfail("OclElf::dumpImage() failed in xread()"); + delete [] *buff; + *buff = 0; + return false; + } + + *len = buff_sz; + return true; +} + + void* +OclElf::oclelf_allocAndCopy(void* p, size_t sz) +{ + if (p == 0 || sz == 0) return p; + + void* buf = xmalloc(_err, sz); + if (buf == 0) { + _err.xfail("OclElf::oclelf_allocAndCopy() failed"); + return 0; + } + + memcpy(buf, p, sz); + _elfMemory.insert( std::make_pair(buf, sz)); + return buf; +} + + void* +OclElf::oclelf_calloc(size_t sz) +{ + void* buf = xmalloc(_err, sz); + if (buf == 0) { + _err.xfail("OclElf::oclelf_calloc() failed"); + return 0; + } + _elfMemory.insert( std::make_pair(buf, sz)); + return buf; +} + + void +OclElf::elfMemoryRelease() +{ + for(EMemory::iterator it = _elfMemory.begin(); it != _elfMemory.end(); it++) { + free(it->first); + } + _elfMemory.clear(); +} + +} // namespace amd diff --git a/rocclr/compiler/lib/loaders/elf/elf.hpp b/rocclr/compiler/lib/loaders/elf/elf.hpp new file mode 100644 index 0000000000..34bf608595 --- /dev/null +++ b/rocclr/compiler/lib/loaders/elf/elf.hpp @@ -0,0 +1,399 @@ +// +// Copyright (c) 2008 Advanced Micro Devices, Inc. All rights reserved. +// +#ifndef ELF_HPP_ +#define ELF_HPP_ + +#include + +#include "top.hpp" +#include "elf_utils.hpp" +#include "cal.h" // using CALtargetEnum + +#include "libelf.h" +#include "gelf.h" + +// Not sure where to put these in the libelf +#define AMD_BIF2 2 // AMD BIF Version 2.0 +#define AMD_BIF3 3 // AMD BIF Version 3.0 + +// These two definitions need to stay in sync with +// the definitions elfdefinitions.h until they get +// properly upstreamed to gcc/libelf. +#ifndef EM_HSAIL +#define EM_HSAIL 0xAF5A +#endif +#ifndef EM_AMDIL +#define EM_AMDIL 0x4154 +#endif +#ifndef EM_ATI_CALIMAGE_BINARY +#define EM_ATI_CALIMAGE_BINARY 125 +#endif +#ifndef ELFOSABI_AMD_OPENCL +#define ELFOSABI_AMD_OPENCL 201 +#endif +#ifndef ELFOSABI_HSAIL +#define ELFOSABI_HSAIL 202 +#endif +#ifndef ELFOSABI_AMDIL +#define ELFOSABI_AMDIL 203 +#endif +#ifndef ELFOSABI_CALIMAGE +#define ELFOSABI_CALIMAGE 100 +#endif +namespace amd { + +// Test: is it ELF file (with a given bitness) ? +bool isElfHeader(const char* p, signed char ec); +bool isElfMagic(const char* p); + +// Test: is it ELF for CAL ? +bool isCALTarget(const char* p, signed char ec); + +// Symbol handle +typedef struct symbol_handle *Sym_Handle; + +class OclElf +{ +public: + enum { + CAL_BASE = 1001, // A number that is not dependent on libelf.h + CPU_BASE = 2001, + CPU_FEATURES_FIRST = 0, // Never generated, but keep it for simplicity. + CPU_FEATURES_LAST = 0xF // This should be consistent with cpudevice.hpp + } oclElfBase; + + typedef enum { + // NOTE!!! Never remove an entry or change the order. + + // All CAL targets are within [CAL_FIRST, CAL_LAST]. + CAL_FIRST = CAL_TARGET_600 + CAL_BASE, + CAL_LAST = CAL_TARGET_LAST + CAL_BASE, + + // All CPU targets are within [CPU_FIRST, CPU_LAST] + CPU_FIRST = CPU_FEATURES_FIRST + CPU_BASE, + CPU_LAST = CPU_FEATURES_LAST + CPU_BASE, + OCL_TARGETS_LAST, + } oclElfTargets; + + typedef enum { + CAL_PLATFORM = 0, + CPU_PLATFORM = 1, + COMPLIB_PLATFORM = 2, + LAST_PLATFORM = 3 + } oclElfPlatform; + + typedef enum { + LLVMIR = 0, + SOURCE, + ILTEXT, + ASTEXT, + CAL, + DLL, + STRTAB, + SYMTAB, + RODATA, + SHSTRTAB, + NOTES, + COMMENT, + ILDEBUG, + DEBUG_INFO, + DEBUG_ABBREV, + DEBUG_LINE, + DEBUG_PUBNAMES, + DEBUG_PUBTYPES, + DEBUG_LOC, + DEBUG_ARANGES, + DEBUG_RANGES, + DEBUG_MACINFO, + DEBUG_STR, + DEBUG_FRAME, + JITBINARY, + CODEGEN, + TEXT, + INTERNAL, + SPIR, + OCL_ELF_SECTIONS_LAST + } oclElfSections; + + typedef struct { + char* sec_name; //! section name + char* sec_addr; //! section address + uint64_t sec_size; //! section size + char* sym_name; //! symbol name + char* address; //! address of corresponding to symbol data + uint64_t size; //! size of data corresponding to symbol + } SymbolInfo; + +private: + + // file descriptor + int _fd; + + // file name + const char* _fname; + + // Is the _fname temporary ? + bool _isTempFile; + + // pointer to libelf structure + ::Elf* _e; + + // Error Object + mutable OclElfErr _err; + + // Bitness of the Elf object. + unsigned char _eclass; + + // Raw ELF bytes in memory from which Elf object is initialized + // The memory is owned by the client, not this OclElf object ! + const char* _rawElfBytes; + uint64_t _rawElfSize; + + // Read, write, or read and write for this Elf object + const Elf_Cmd _elfCmd; + + // Memory management + typedef std::map EMemory; + EMemory _elfMemory; + + // Indexes of .shstrtab and .strtab (for convenience) + Elf64_Word _shstrtab_ndx; + Elf64_Word _strtab_ndx; + +public: + + /* + OclElf object can be created for reading or writing (it could be created for + both reading and writing, which is not supported yet at this time). Currently, + it has two forms: + + 1) OclElf(eclass, rawElfBytes, rawElfSize, 0, ELF_C_READ) + + To load ELF from raw bytes in memory and generate OclElf object. And this + object is for reading only. + + 2) OclElf(eclass, NULL, 0, elfFileName|NULL, ELF_C_WRITE) + + To create an ELF for writing and save it into a file 'elfFileName' (if it + is NULL, the OclElf will create a temporary file and set it to 'elfFileName'. + + Since we need to read the ELF into memory, this file 'elfFileName' is created + with both read and write, so that the runtime can use dumpImage() to get ELF + raw bytes by reading this file. + + 'eclass' is ELF's bitness and it must be the same as the eclass of ELF to + be loaded (for example, rawElfBytes). + + + Return values of all public APIs with bool return type + true : on success; + false : on error. + */ + OclElf ( + unsigned char eclass, // eclass for this ELF + const char* rawElfBytes, // raw ELF bytes to be loaded + uint64_t rawElfSize, // size of the ELF raw bytes + const char* elfFileName, // File to save this ELF. + Elf_Cmd elfcmd // ELF_C_READ/ELF_C_WRITE + ); + + ~OclElf (); + + /* + dumpImage() will finalize the ELF and write it into the file. It then reads + it into the memory; and returns it via . + + The memory pointed by buff is owned by OclElf object. + */ + bool dumpImage(char** buff, size_t* len); + + /* + addSection() is used to create a single ELF section with data . If + do_copy is true, the OclElf object will make a copy of d_buf and uses that copy to + create an ELF section. + + When setting do_copy = false, the caller should make sure that will + be unchanged and available during the lifetime of this OclElf object; ie before + calling dumpImage(). + */ + bool addSection ( + oclElfSections id, + const void* d_buf, + size_t d_size, + bool do_copy = true + ); + + /* + getSection() will return the whole section in . + + The memory pointed by is owned by the OclElf object. + */ + bool getSection(oclElfSections id, char** dst, size_t* sz) const; + + + /* + addSymbol() adds a symbol with name 'symbolName' and data + into the ELF. 'id' indicates which section will go + into. The meaning of 'do_copy' is the same as addSection(). + */ + bool addSymbol( + oclElfSections id, // Section in which symbol is added + const char* symbolName, // Name of symbol + const void* buffer, // Symbol's data + size_t size, // Symbol's size + bool do_copy = true // If true, add a copy of buffer into the section + ); + + /* + * getSymbol() will return the data associated with + * the symbol from the Elf. + * + * The memory pointed by is owned by the OclElf object + */ + bool getSymbol( + oclElfSections id, // Section in which symbol is in + const char* symbolName, // Name of the symbol to retrieve + char** buffer, // Symbol's data + size_t* size // Symbol's size + ) const; + + /* + nextSymbol() and getSymbolInfo() use the symbol handle to access symbols + + For example: + for( Sym_Handle s = nextSymbol(NULL); s ; s = nextSymbol(s)) { + SymbolInfo si; + if (!getSymbolInfo(s, &si)) { + Error; + } + use si + } + + where nextSymbol(NULL) will return the first symbol. + + Note that memory space pointed to by si is owned by OclElf. + */ + bool getSymbolInfo(Sym_Handle sym, SymbolInfo* symInfo) const; + Sym_Handle nextSymbol(Sym_Handle symhandle) const; + + /* + Adds a note with name 'noteName' and description "noteDesc" + into the .note section of ELF. Length of note name is 'nameSize'. + Length of note description is "descSize'. + */ + bool addNote(const char* noteName, const char* noteDesc, + size_t nameSize, size_t descSize); + + /* + Returns the description of a note whose name is 'noteName' + in 'noteDesc'. + Returns the length of the description in 'descSize'. + */ + bool getNote(const char* noteName, char** noteDesc, size_t *descSize); + + + /* + Get/set machine and platform (target) for which elf is built. + */ + bool getTarget(uint16_t& machine, oclElfPlatform& platform); + bool setTarget(uint16_t machine, oclElfPlatform platform); + + /* + Get/set elf type field from header + */ + bool getType(uint16_t &type); + bool setType(uint16_t type); + + /* + Get/set elf flag field from header. + */ + bool getFlags(uint32_t &flag); + bool setFlags(uint32_t flag); + + /* + Clear() will return the status of OclElf to just after ctor() is invoked. + However, it will not regenerate a temporary file name like ctor() does. + + It is useful when the ELF content needs to be discarded for some reason. + */ + bool Clear(); + + bool hasError() { return (_err.getOclElfError())[0] != 0; } + const char* getErrMsg() { return _err.getOclElfError(); } + unsigned char getELFClass() { return _eclass; } + +private: + + /* Initialization */ + bool Init(); + + /* + Initialize ELF object by creating ELF header and key sections such as + .shstrtab, .strtab, and .symtab. + */ + bool InitElf (); + + // Wraper for creating a section header and Elf_Data + bool createShdr ( + oclElfSections id, + Elf_Scn*& scn, + Elf64_Word shname, + Elf64_Word shlink = 0 + ); + + Elf_Data* createElfData( + Elf_Scn*& scn, + oclElfSections id, + void* d_buf, + uint64_t d_size, + bool do_copy + ); + + + /* + Create a new section (id) with data . If do_copy is true, + make a copy of d_buf and create a new section with that copy. + + Return the valid Elf_Scn on success; return NULL on error. + + Note that newSection() uses Section Header's size, so make sure elf_update() + is invoked properly before invoking newSection(). + */ + Elf_Scn* newSection ( + oclElfSections id, + const void* d_buf, + size_t d_size, + bool do_copy = true // if true, add a copy of d_buf + ); + + /* + Add a new data into a section by creating a new data descriptor. + And the new data's offset is returned in 'outOffset'. + */ + bool addSectionData( + Elf64_Xword& outOffset, + oclElfSections id, + const void* buffer, + size_t size, + bool do_copy=true // if true, add a copy of buffer + ); + + // Return Elf_Data for this section 'id' + bool getSectionData(Elf_Data*& data, oclElfSections id) const; + + // Return Elf_Scn for this section 'id' + bool getSectionDesc(Elf_Scn*& scn, oclElfSections id) const; + + // + bool getShstrtabNdx(Elf64_Word& outNdx, const char*); + + void* oclelf_allocAndCopy(void* p, size_t sz); + void* oclelf_calloc(size_t sz); + + void elfMemoryRelease(); +}; + +} // namespace amd + +#endif diff --git a/rocclr/compiler/lib/loaders/elf/elf_utils.cpp b/rocclr/compiler/lib/loaders/elf/elf_utils.cpp new file mode 100644 index 0000000000..d3ca6ce344 --- /dev/null +++ b/rocclr/compiler/lib/loaders/elf/elf_utils.cpp @@ -0,0 +1,302 @@ +// +// Copyright (c) 2010 Advanced Micro Devices, Inc. All rights reserved. +// + +#include "elf_utils.hpp" + +#include +#include +#include +#include + +#if defined(__GNUC__) +#include +#else +#include +#endif + + +/* + See elf_utils.hpp for descriptions about each functions + */ + +namespace amd { + +#if defined(_MSC_VER) + +#define ELF_OPEN _open +#define ELF_READ(f, b, l) _read((f), (b), (unsigned int)(l)) +#define ELF_WRITE _write +#define ELF_CLOSE _close +#define ELF_LSEEK _lseek + +#else + +#define ELF_OPEN open +#define ELF_READ(f, b, l) read((f), (b), (size_t)(l)) +#define ELF_WRITE write +#define ELF_CLOSE close +#define ELF_LSEEK lseek + +#endif + +/* + Save the error string in _lastErrMsg. If it is built without NDEBUG, the program + will terminate immediately with exit(1). + */ +void OclElfErr::xfail(const char *fmt, ...) +{ + va_list ap; + va_start(ap, fmt); + vsnprintf(&_lastErrMsg[0], (size_t)MAX_ERROR_MESSAGE_LENGTH, fmt, ap); + va_end(ap); + +#ifndef NDEBUG + printf("%s\n", _lastErrMsg); + exit(1); +#endif +} + +namespace oclelfutils { + +/* + Wrap malloc() with xfail(), so this returns newly-allocated memory or 0. + The memory is guaranteed to be initialized to zero. + */ +void* xmalloc(OclElfErr& err, const size_t len) +{ + void *retval = calloc(1, len); + if (retval == NULL) { + err.xfail("xmalloc failed: out of memory"); + return NULL; + } + return retval; +} + + +/* + Return file descriptor on success; return -1 on error and invoke xfail() + to record the error. + */ +int xopen(OclElfErr& err, const char *fname, const int in_flags, const int perms) +{ + const int retval = ELF_OPEN(fname, in_flags, perms); + if (retval < 0) { + err.xfail("Failed to open '%s': %s", fname, strerror(errno)); + return -1; + } + return retval; +} + +/* + Return 0 on success; return -1 on error. + */ +int xclose(OclElfErr& err, const char *fname, const int fd) +{ + int rc; + while ( ((rc = :: ELF_CLOSE(fd)) == -1) && (errno == EINTR) ) { ;/* spin. */ } + if (rc == -1) { + err.xfail("Failed to close '%s': %s", fname, strerror(errno)); + return -1; + } + return rc; +} + +/* + Return the file offset location on success; return -1 on error. + */ +off_t xlseek( + OclElfErr& err, + const char* fname, + const int fd, + const off_t offset, + const int whence) +{ + // For really big file _lseeki64/lseek64 are needed. For now, + // lseek/_lseek is enough. + off_t res = ELF_LSEEK(fd, offset, whence); + if (res == -1) { + err.xfail("Failed to seek in '%s': %s", fname, strerror(errno)); + return -1; + } + return res; +} + +/* + Return the number of bytes that are read on success; return -1 on error. + */ +ssize_t xread( + OclElfErr& err, + const char* fname, + const int fd, + void* buf, + const size_t buf_len + ) +{ + ssize_t rc; + while (((rc = ELF_READ(fd, buf, buf_len)) == -1) && (errno == EINTR)) { ;/* spin */ } + if (rc < 0) { + err.xfail("Failed to read '%s': %s", fname, strerror(errno)); + return -1; + } + return rc; +} + +#if 0 + + +/* + Return the number of bytes that have been written on success; return -1 on error. + */ +ssize_t xwrite(OclElfErr& err, + const char* fname, + const int fd, + const void* buf, + const size_t len) +{ + ssize_t rc; + while (((rc = ELF_WRITE(fd, buf, len)) == -1) && (errno == EINTR)) { ;/* spin */ } + if ( (rc == -1) || (rc != (ssize_t)len) ) { + err.xfail("Failed to write '%s': %s", fname, strerror(errno)); + return -1; + } + return rc; +} + +/* + Allocate a copy of (str), invoke xfail() on failure. + Returns NULL on error, or address of the allocated copy + */ +char* xstrdup(OclElfErr& err, const char *str) +{ + char* retval = (char*)xmalloc(err, strlen(str) + 1); + if (retval == NULL) { + err.xfail("xstrdup failed: cannot allocate new char string"); + return NULL; + } + strcpy(retval, str); + return retval; +} + + +/* + get the length of an open file in bytes. return -1 on error. + */ +uint64_t xget_file_size(OclElfErr& err, const char *fname, const int fd) +{ + struct stat statbuf; + if (fstat(fd, &statbuf) == -1) { + err.xfail("Failed to fstat '%s': %s", fname, strerror(errno)); + return -1; + } + return (uint64_t) statbuf.st_size; +} + + +/* + Copy file 'infd' to file 'outfd'. + Return the total number of bytes copied on success; return -1 on error. + */ +int64_t xcopyfile( + OclElfErr& err, + const char* in, + const int infd, + const char* out, + const int outfd + ) +{ + uint64_t retval = 0; + ssize_t rc = 0; + off_t res = xlseek(err, in, infd, 0, SEEK_SET); + if (res == -1) { + err.xfail("xcopyfile failed in xlseek : in %s, out %s", in, out); + return -1; + } + + uint8_t* copybuf = err._copyBuffer; + if (copybuf == NULL) { + copybuf = (uint8_t*)xmalloc(err, IO_BUF_SIZE); + err._copyBuffer = copybuf; + } + + while ( (rc = xread(err, in, infd, copybuf, IO_BUF_SIZE)) > 0 ) { + retval += (uint64_t) rc; + int ret = xwrite(err, out, outfd, copybuf, rc); + if (ret == -1) { + err.xfail("xcopyfile failed in xwrite: in %s, out %s", in, out); + return -1; + } + } + if (rc == -1) { + err.xfail("xcopyfile failed in xread: in %s, out %s", in, out); + return -1; + } + return retval; +} + + +/* + Copy file from 'infd' to current offset in 'outfd', for 'size' bytes. + Return 'size' on success; return -1 on error. + */ +int64_t +xcopyfile_range( + OclElfErr& err, const char *in, const int infd, + const char *out, const int outfd, + const uint64_t offset, const uint64_t size + ) +{ + uint8_t* copybuf = err._copyBuffer; + if (copybuf == NULL) { + copybuf = (uint8_t*)xmalloc(err, IO_BUF_SIZE); + err._copyBuffer = copybuf; + } + + ssize_t rc = xlseek(err, in, infd, (off_t) offset, SEEK_SET); + if (rc == -1) { + err.xfail("xcopyfile_range: xlseek() failed: %s", in); + return -1; + } + + uint64_t remaining = size; + while (remaining >= IO_BUF_SIZE) { + rc = xread(err, in, infd, copybuf, IO_BUF_SIZE); + if ((rc == -1) || (rc != IO_BUF_SIZE)) { + err.xfail("xcopyfile_range: xread() failed %s", in); + return -1; + } + rc = xwrite(err, out, outfd, copybuf, IO_BUF_SIZE); + if (rc == -1) { + err.xfail("xcopyfile_range: xwrite() failed: %s", out); + } + + remaining -= (uint64_t) IO_BUF_SIZE; + } + + if (remaining > 0) { + rc = xread(err, in, infd, copybuf, IO_BUF_SIZE); + if ((rc == -1) || (rc != (ssize_t)remaining)) { + err.xfail("xcopyfile_range: xread() failed %s", in); + return -1; + } + rc = xwrite(err, out, outfd, copybuf, rc); + if (rc == -1) { + err.xfail("xcopyfile_range: xwrite() failed: %s", out); + } + } + return size; +} + + +uint64_t +align_to_page(const uint64_t offset) +{ + // TODO_jugu don't use hardcoded pagesize. + return (offset + ((1LL << 12) -1)) & ((uint64_t)(-(1LL << 12))); +} + +#endif + +} // namespace elfutils + +} // namespace amd diff --git a/rocclr/compiler/lib/loaders/elf/elf_utils.hpp b/rocclr/compiler/lib/loaders/elf/elf_utils.hpp new file mode 100644 index 0000000000..fc0be2da1c --- /dev/null +++ b/rocclr/compiler/lib/loaders/elf/elf_utils.hpp @@ -0,0 +1,145 @@ +// +// Copyright (c) 2008 Advanced Micro Devices, Inc. All rights reserved. +// +#ifndef _ELF_UTILS_HPP +#define _ELF_UTILS_HPP + + +#include +#include +#include +#include + +#include "top.hpp" + +namespace amd { + +#define MAX_ERROR_MESSAGE_LENGTH 1024 +#define IO_BUF_SIZE 16 * 1024 + +class OclElfErr +{ +public: + // Temperary buffer for copying from file to file + uint8_t* _copyBuffer; // Initialized first time it is used + +private: + // Keep the last error message. + char _lastErrMsg[MAX_ERROR_MESSAGE_LENGTH]; + +public: + + OclElfErr() : _copyBuffer(NULL) { _lastErrMsg[0] = 0; } + ~OclElfErr() { + if (_copyBuffer) { + free(_copyBuffer); + } + } + + void Init() { _lastErrMsg[0] = 0; } + + void Fini() { + _lastErrMsg[0] = 0; + if (_copyBuffer) { + free(_copyBuffer); + } + _copyBuffer = NULL; + } + + // Return the last error message. + const char* getOclElfError() const { return _lastErrMsg; } + + // + // Save the error string in ErrorMessage. If it is built without NDEBUG, the program + // will terminate immediately with exit(1). + // + void xfail(const char *fmt, ...); + +}; + +namespace oclelfutils { + +/* + Wrap malloc() with xfail(), so this returns newly-allocated memory or 0. + The memory is guaranteed to be initialized to zero. + */ +void* xmalloc(OclElfErr& err, const size_t len); + +/* + Return file descriptor on success; return -1 on error and invoke xfail() + to record the error. + */ +int xopen(OclElfErr& err, const char *fname, const int flags, const int perms); + +/* + Return 0 on success; return -1 on error. + */ +int xclose(OclElfErr& err, const char *fname, const int fd); + +/* + Return the file offset location on success; return -1 on error. + */ +off_t xlseek(OclElfErr& err, const char *fname, const int fd, + const off_t o, const int whence); + +/* + Return the number of bytes that are read on success; return -1 on error. + */ +ssize_t xread( + OclElfErr& err, + const char* fname, // File name for file descriptor 'fd' + const int fd, // File descriptor + void* buf, // buffer for reading + const size_t buf_len // capacity of buffer in bytes + ); + +#if 0 + +/* + Return the number of bytes that have been written on success; return -1 on error. + */ +ssize_t xwrite( + OclElfErr& err, + const char* fname, // File name for file descriptor 'fd' + const int fd, // File descriptor + const void* buf, // data buffer to be written out + const size_t buf_len // the size of data in bytes + ); + +/* + Allocate a copy of (str), invoke xfail() on failure. + Returns 0 on error, or address of the allocated copy + */ +char* xstrdup(OclElfErr& err, const char *str); + +/* + get the length of an open file in bytes. return -1 if error. + */ +uint64_t xget_file_size(OclElfErr& err, const char *fname, const int fd); + +/* + Copy file 'infd' to file 'outfd'. + Return the total number of bytes copied on success; return -1 on error. + */ +int64_t xcopyfile(OclElfErr& err, const char *in, const int infd, + const char *out, const int outfd); + +/* + Copy file from 'infd' to current offset in 'outfd', for 'size' bytes. + Return 'size' on success; return -1 on error. + */ +int64_t xcopyfile_range(OclElfErr& err, const char *in, const int infd, + const char *out, const int outfd, + const uint64_t offset, const uint64_t size); + + +// Align a value to the page size. +uint64_t align_to_page(const uint64_t offset); + +#endif + +} // namespace elfutils + +} // namespace amd + +#endif diff --git a/rocclr/compiler/lib/loaders/elf/utils/common/_elftc.h b/rocclr/compiler/lib/loaders/elf/utils/common/_elftc.h new file mode 100644 index 0000000000..18631b0d4d --- /dev/null +++ b/rocclr/compiler/lib/loaders/elf/utils/common/_elftc.h @@ -0,0 +1,542 @@ +/*- + * Copyright (c) 2009 Joseph Koshy + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * $Id: _elftc.h 2064 2011-10-26 15:12:32Z jkoshy $ + */ + +/** + ** Miscellanous definitions needed by multiple components. + **/ + +#ifndef _ELFTC_H +#define _ELFTC_H + +#ifndef NULL +#define NULL ((void *) 0) +#endif + +#ifndef offsetof +// change from (int) to (char *) for x64 gcc +#define offsetof(T, M) ((char *) &((T*) 0) -> M) +#endif + +/* + * Supply macros missing from + */ + +#ifndef STAILQ_FOREACH_SAFE +#define STAILQ_FOREACH_SAFE(var, head, field, tvar) \ + for ((var) = STAILQ_FIRST((head)); \ + (var) && ((tvar) = STAILQ_NEXT((var), field), 1); \ + (var) = (tvar)) +#endif + +#ifndef STAILQ_LAST +#define STAILQ_LAST(head, type, field) \ + (STAILQ_EMPTY((head)) ? \ + NULL : \ + ((struct type *)(void *) \ + ((char *)((head)->stqh_last) - offsetof(struct type, field)))) +#endif + +#ifndef TAILQ_FOREACH_SAFE +#define TAILQ_FOREACH_SAFE(var, head, field, tvar) \ + for ((var) = TAILQ_FIRST((head)); \ + (var) && ((tvar) = TAILQ_NEXT((var), field), 1); \ + (var) = (tvar)) +#endif + +/* + * Symbols that are sometimes missing in system headers. + */ + +#ifndef DT_DEPRECATED_SPARC_REGISTER +#define DT_DEPRECATED_SPARC_REGISTER 0x70000001 +#endif + +#ifndef DT_GNU_PRELINKED +#define DT_GNU_PRELINKED 0x6FFFFDF5U +#endif + +#ifndef DT_GNU_CONFLICTSZ +#define DT_GNU_CONFLICTSZ 0x6FFFFDF6U +#endif + +#ifndef DT_GNU_LIBLISTSZ +#define DT_GNU_LIBLISTSZ 0x6FFFFDF7U +#endif + +#ifndef DT_GNU_HASH +#define DT_GNU_HASH 0x6FFFFEF5U +#endif + +#ifndef DT_GNU_CONFLICT +#define DT_GNU_CONFLICT 0x6FFFFEF8U +#endif + +#ifndef DT_GNU_LIBLIST +#define DT_GNU_LIBLIST 0x6FFFFEF9U +#endif + +#ifndef DT_MAXPOSTAGS +#define DT_MAXPOSTAGS 34 +#endif + +#ifndef DT_SUNW_AUXILIARY +#define DT_SUNW_AUXILIARY 0x6000000D +#endif + +#ifndef DT_SUNW_CAP +#define DT_SUNW_CAP 0x60000010 +#endif + +#ifndef DT_SUNW_FILTER +#define DT_SUNW_FILTER 0x6000000F +#endif + +#ifndef DT_SUNW_RTLDINF +#define DT_SUNW_RTLDINF 0x6000000E +#endif + +#ifndef DT_USED +#define DT_USED 0x7FFFFFFE +#endif + +#ifndef ELFOSABI_86OPEN +#define ELFOSABI_86OPEN 5 +#endif + +#ifndef ELFOSABI_AIX +#define ELFOSABI_AIX 7 +#endif + +#ifndef ELFOSABI_HURD +#define ELFOSABI_HURD 4 +#endif + +#ifndef ELFOSABI_NONE +#define ELFOSABI_NONE 0 +#endif + +#ifndef ELFOSABI_NSK +#define ELFOSABI_NSK 14 +#endif + +#ifndef ELFOSABI_OPENVMS +#define ELFOSABI_OPENVMS 13 +#endif + +/* + * Supply missing EM_XXX definitions. + */ +#ifndef EM_68HC05 +#define EM_68HC05 72 +#endif + +#ifndef EM_68HC08 +#define EM_68HC08 71 +#endif + +#ifndef EM_68HC11 +#define EM_68HC11 70 +#endif + +#ifndef EM_68HC16 +#define EM_68HC16 69 +#endif + +#ifndef EM_ARCA +#define EM_ARCA 109 +#endif + +#ifndef EM_ARC_A5 +#define EM_ARC_A5 93 +#endif + +#ifndef EM_AVR +#define EM_AVR 83 +#endif + +#ifndef EM_BLACKFIN +#define EM_BLACKFIN 106 +#endif + +#ifndef EM_CR +#define EM_CR 103 +#endif + +#ifndef EM_CRIS +#define EM_CRIS 76 +#endif + +#ifndef EM_D10V +#define EM_D10V 85 +#endif + +#ifndef EM_D30V +#define EM_D30V 86 +#endif + +#ifndef EM_F2MC16 +#define EM_F2MC16 104 +#endif + +#ifndef EM_FIREPATH +#define EM_FIREPATH 78 +#endif + +#ifndef EM_FR30 +#define EM_FR30 84 +#endif + +#ifndef EM_FX66 +#define EM_FX66 66 +#endif + +#ifndef EM_HUANY +#define EM_HUANY 81 +#endif + +#ifndef EM_IP2K +#define EM_IP2K 101 +#endif + +#ifndef EM_JAVELIN +#define EM_JAVELIN 77 +#endif + +#ifndef EM_M32R +#define EM_M32R 88 +#endif + +#ifndef EM_MAX +#define EM_MAX 102 +#endif + +#ifndef EM_MMIX +#define EM_MMIX 80 +#endif + +#ifndef EM_MN10200 +#define EM_MN10200 90 +#endif + +#ifndef EM_MN10300 +#define EM_MN10300 89 +#endif + +#ifndef EM_MSP430 +#define EM_MSP430 105 +#endif + +#ifndef EM_NS32K +#define EM_NS32K 97 +#endif + +#ifndef EM_OPENRISC +#define EM_OPENRISC 92 +#endif + +#ifndef EM_PDSP +#define EM_PDSP 63 +#endif + +#ifndef EM_PJ +#define EM_PJ 91 +#endif + +#ifndef EM_PRISM +#define EM_PRISM 82 +#endif + +#ifndef EM_SEP +#define EM_SEP 108 +#endif + +#ifndef EM_SE_C33 +#define EM_SE_C33 107 +#endif + +#ifndef EM_SNP1K +#define EM_SNP1K 99 +#endif + +#ifndef EM_ST19 +#define EM_ST19 74 +#endif + +#ifndef EM_ST200 +#define EM_ST200 100 +#endif + +#ifndef EM_ST7 +#define EM_ST7 68 +#endif + +#ifndef EM_ST9PLUS +#define EM_ST9PLUS 67 +#endif + +#ifndef EM_SVX +#define EM_SVX 73 +#endif + +#ifndef EM_TMM_GPP +#define EM_TMM_GPP 96 +#endif + +#ifndef EM_TPC +#define EM_TPC 98 +#endif + +#ifndef EM_UNICORE +#define EM_UNICORE 110 +#endif + +#ifndef EM_V850 +#define EM_V850 87 +#endif + +#ifndef EM_VAX +#define EM_VAX 75 +#endif + +#ifndef EM_VIDEOCORE +#define EM_VIDEOCORE 95 +#endif + +#ifndef EM_XTENSA +#define EM_XTENSA 94 +#endif + +#ifndef EM_ZSP +#define EM_ZSP 79 +#endif + +#ifndef PN_XNUM +#define PN_XNUM 0xFFFFU +#endif + +#ifndef R_IA_64_DIR32LSB +#define R_IA_64_DIR32LSB 0x25 +#endif + +#ifndef R_IA_64_DIR64LSB +#define R_IA_64_DIR64LSB 0x27 +#endif + +#ifndef R_IA_64_SECREL32LSB +#define R_IA_64_SECREL32LSB 0x65 +#endif + +#ifndef R_MIPS_32 +#define R_MIPS_32 0x2 +#endif + +#ifndef R_PPC_ADDR32 +#define R_PPC_ADDR32 0x1 +#endif + +#ifndef R_SPARC_UA32 +#define R_SPARC_UA32 23 +#endif + +#ifndef R_SPARC_UA64 +#define R_SPARC_UA64 54 +#endif + +#ifndef R_X86_64_32 +#define R_X86_64_32 10 +#endif + +#ifndef R_X86_64_64 +#define R_X86_64_64 1 +#endif + +#ifndef SHT_AMD64_UNWIND +#define SHT_AMD64_UNWIND 0x70000001 +#endif + +#ifndef SHT_SUNW_ANNOTATE +#define SHT_SUNW_ANNOTATE 0X6FFFFFF7 +#endif + +#ifndef SHT_SUNW_DEBUGSTR +#define SHT_SUNW_DEBUGSTR 0X6FFFFFF8 +#endif + +#ifndef SHT_SUNW_DEBUG +#define SHT_SUNW_DEBUG 0X6FFFFFF9 +#endif + +#ifndef SHT_SUNW_cap +#define SHT_SUNW_cap 0x6FFFFFF5 +#endif + +#ifndef SHT_SUNW_dof +#define SHT_SUNW_dof 0x6FFFFFF4 +#endif + +#ifndef SHT_SUNW_verdef +#define SHT_SUNW_verdef 0x6FFFFFFD +#endif + +#ifndef SHT_SUNW_verneed +#define SHT_SUNW_verneed 0x6FFFFFFE +#endif + +#ifndef SHT_SUNW_versym +#define SHT_SUNW_versym 0x6FFFFFFF +#endif + +#ifndef SHN_XINDEX +#define SHN_XINDEX 0xFFFFU +#endif + +#ifndef SHT_GNU_ATTRIBUTES +#define SHT_GNU_ATTRIBUTES 0x6FFFFFF5U +#endif + +#ifndef SHT_GNU_HASH +#define SHT_GNU_HASH 0x6FFFFFF6U +#endif + +#ifndef SHT_GNU_LIBLIST +#define SHT_GNU_LIBLIST 0x6FFFFFF7U +#endif + +/* + * VCS Ids. + */ + +#ifndef ELFTC_VCSID + +#if defined(__FreeBSD__) +#define ELFTC_VCSID(ID) __FBSDID(ID) +#endif + +#if defined(__linux__) || defined(__GNU__) || defined(__GLIBC__) +#if defined(__GNUC__) +#define ELFTC_VCSID(ID) __asm__(".ident\t\"" ID "\"") +#else +#define ELFTC_VCSID(ID) /**/ +#endif +#endif + +#if defined(__NetBSD__) +#define ELFTC_VCSID(ID) __RCSID(ID) +#endif + +#endif /* ELFTC_VCSID */ + +/* + * Provide an equivalent for getprogname(3). + */ + +#ifndef ELFTC_GETPROGNAME + +#if defined(__FreeBSD__) || defined(__NetBSD__) + +#include + +#define ELFTC_GETPROGNAME() getprogname() + +#endif /* defined(__FreeBSD__) || defined(__NetBSD__) */ + + +#if defined(__linux__) + +#ifdef __cplusplus +extern "C" { +#endif +/* + * GLIBC based systems have a global 'char *' pointer referencing + * the executable's name. + */ +extern /*const*/ char *program_invocation_short_name; +#ifdef __cplusplus +} +#endif + +#define ELFTC_GETPROGNAME() program_invocation_short_name + +#endif /* __linux__ */ + +#endif /* ELFTC_GETPROGNAME */ + +/** + ** Per-OS configuration. + **/ + +#if defined(__linux__) + +#include + +#define ELFTC_BYTE_ORDER __BYTE_ORDER +#define ELFTC_BYTE_ORDER_LITTLE_ENDIAN __LITTLE_ENDIAN +#define ELFTC_BYTE_ORDER_BIG_ENDIAN __BIG_ENDIAN + +/* + * Debian GNU/Linux is missing strmode(3). + */ +#define ELFTC_HAVE_STRMODE 0 + +/* Whether we need to supply {be,le}32dec. */ +#define ELFTC_NEED_BYTEORDER_EXTENSIONS 1 + +#define roundup2 roundup + +#endif /* __linux__ */ + + +#if defined(__FreeBSD__) + +#include +#include + +#define ELFTC_BYTE_ORDER _BYTE_ORDER +#define ELFTC_BYTE_ORDER_LITTLE_ENDIAN _LITTLE_ENDIAN +#define ELFTC_BYTE_ORDER_BIG_ENDIAN _BIG_ENDIAN + +#define ELFTC_HAVE_STRMODE 1 +#if __FreeBSD_version <= 900000 +#define ELFTC_BROKEN_YY_NO_INPUT 1 +#endif +#endif /* __FreeBSD__ */ + + +#if defined(__NetBSD__) + +#include + +#define ELFTC_BYTE_ORDER _BYTE_ORDER +#define ELFTC_BYTE_ORDER_LITTLE_ENDIAN _LITTLE_ENDIAN +#define ELFTC_BYTE_ORDER_BIG_ENDIAN _BIG_ENDIAN + +#define ELFTC_HAVE_STRMODE 1 +#define ELFTC_BROKEN_YY_NO_INPUT 1 +#endif /* __NetBSD __ */ + +#endif /* _ELFTC_H */ diff --git a/rocclr/compiler/lib/loaders/elf/utils/common/elfdefinitions.h b/rocclr/compiler/lib/loaders/elf/utils/common/elfdefinitions.h new file mode 100644 index 0000000000..10051dbcc3 --- /dev/null +++ b/rocclr/compiler/lib/loaders/elf/utils/common/elfdefinitions.h @@ -0,0 +1,2618 @@ +/*- + * Copyright (c) 2010 Joseph Koshy + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * $Id: elfdefinitions.h 2064 2011-10-26 15:12:32Z jkoshy $ + */ + +/* + * These definitions are based on: + * - The public specification of the ELF format as defined in the + * October 2009 draft of System V ABI. + * See: http://www.sco.com/developers/gabi/latest/ch4.intro.html + * - The May 1998 (version 1.5) draft of "The ELF-64 object format". + * - Processor-specific ELF ABI definitions for sparc, i386, amd64, mips, + * ia64, and powerpc processors. + * - The "Linkers and Libraries Guide", from Sun Microsystems. + */ + +#ifndef _ELFDEFINITIONS_H_ +#define _ELFDEFINITIONS_H_ + +#ifdef WIN32 +#include "compat.h" +#else +#include +#endif + +/* + * Types of capabilities. + */ + +#define _ELF_DEFINE_CAPABILITIES() \ +_ELF_DEFINE_CA(CA_SUNW_NULL, 0, "ignored") \ +_ELF_DEFINE_CA(CA_SUNW_HW_1, 1, "hardware capability") \ +_ELF_DEFINE_CA(CA_SUNW_SW_1, 2, "software capability") + +#undef _ELF_DEFINE_CA +#define _ELF_DEFINE_CA(N, V, DESCR) N = V , +enum { + _ELF_DEFINE_CAPABILITIES() + CA__LAST__ +}; + +/* + * Flags used with dynamic linking entries. + */ + +#define _ELF_DEFINE_DYN_FLAGS() \ +_ELF_DEFINE_DF(DF_ORIGIN, 0x1, \ + "object being loaded may refer to $ORIGIN") \ +_ELF_DEFINE_DF(DF_SYMBOLIC, 0x2, \ + "search library for references before executable") \ +_ELF_DEFINE_DF(DF_TEXTREL, 0x4, \ + "relocation entries may modify text segment") \ +_ELF_DEFINE_DF(DF_BIND_NOW, 0x8, \ + "process relocation entries at load time") \ +_ELF_DEFINE_DF(DF_STATIC_TLS, 0x10, \ + "uses static thread-local storage") +#undef _ELF_DEFINE_DF +#define _ELF_DEFINE_DF(N, V, DESCR) N = V , +enum { + _ELF_DEFINE_DYN_FLAGS() + DF__LAST__ +}; + + +/* + * Dynamic linking entry types. + */ + +#define _ELF_DEFINE_DYN_TYPES() \ +_ELF_DEFINE_DT(DT_NULL, 0, "end of array") \ +_ELF_DEFINE_DT(DT_NEEDED, 1, "names a needed library") \ +_ELF_DEFINE_DT(DT_PLTRELSZ, 2, \ + "size in bytes of associated relocation entries") \ +_ELF_DEFINE_DT(DT_PLTGOT, 3, \ + "address associated with the procedure linkage table") \ +_ELF_DEFINE_DT(DT_HASH, 4, \ + "address of the symbol hash table") \ +_ELF_DEFINE_DT(DT_STRTAB, 5, \ + "address of the string table") \ +_ELF_DEFINE_DT(DT_SYMTAB, 6, \ + "address of the symbol table") \ +_ELF_DEFINE_DT(DT_RELA, 7, \ + "address of the relocation table") \ +_ELF_DEFINE_DT(DT_RELASZ, 8, "size of the DT_RELA table") \ +_ELF_DEFINE_DT(DT_RELAENT, 9, "size of each DT_RELA entry") \ +_ELF_DEFINE_DT(DT_STRSZ, 10, "size of the string table") \ +_ELF_DEFINE_DT(DT_SYMENT, 11, \ + "size of a symbol table entry") \ +_ELF_DEFINE_DT(DT_INIT, 12, \ + "address of the initialization function") \ +_ELF_DEFINE_DT(DT_FINI, 13, \ + "address of the finalization function") \ +_ELF_DEFINE_DT(DT_SONAME, 14, "names the shared object") \ +_ELF_DEFINE_DT(DT_RPATH, 15, \ + "runtime library search path") \ +_ELF_DEFINE_DT(DT_SYMBOLIC, 16, \ + "alter symbol resolution algorithm") \ +_ELF_DEFINE_DT(DT_REL, 17, \ + "address of the DT_REL table") \ +_ELF_DEFINE_DT(DT_RELSZ, 18, "size of the DT_REL table") \ +_ELF_DEFINE_DT(DT_RELENT, 19, "size of each DT_REL entry") \ +_ELF_DEFINE_DT(DT_PLTREL, 20, \ + "type of relocation entry in the procedure linkage table") \ +_ELF_DEFINE_DT(DT_DEBUG, 21, "used for debugging") \ +_ELF_DEFINE_DT(DT_TEXTREL, 22, \ + "text segment may be written to during relocation") \ +_ELF_DEFINE_DT(DT_JMPREL, 23, \ + "address of relocation entries associated with the procedure linkage table") \ +_ELF_DEFINE_DT(DT_BIND_NOW, 24, \ + "bind symbols at loading time") \ +_ELF_DEFINE_DT(DT_INIT_ARRAY, 25, \ + "pointers to initialization functions") \ +_ELF_DEFINE_DT(DT_FINI_ARRAY, 26, \ + "pointers to termination functions") \ +_ELF_DEFINE_DT(DT_INIT_ARRAYSZ, 27, "size of the DT_INIT_ARRAY") \ +_ELF_DEFINE_DT(DT_FINI_ARRAYSZ, 28, "size of the DT_FINI_ARRAY") \ +_ELF_DEFINE_DT(DT_RUNPATH, 29, \ + "index of library search path string") \ +_ELF_DEFINE_DT(DT_FLAGS, 30, \ + "flags specific to the object being loaded") \ +_ELF_DEFINE_DT(DT_ENCODING, 32, "standard semantics") \ +_ELF_DEFINE_DT(DT_PREINIT_ARRAY, 32, \ + "pointers to pre-initialization functions") \ +_ELF_DEFINE_DT(DT_PREINIT_ARRAYSZ, 33, \ + "size of pre-initialization array") \ +_ELF_DEFINE_DT(DT_MAXPOSTAGS, 34, \ + "the number of positive tags") \ +_ELF_DEFINE_DT(DT_LOOS, 0x6000000DUL, \ + "start of OS-specific types") \ +_ELF_DEFINE_DT(DT_SUNW_AUXILIARY, 0x6000000DUL, \ + "offset of string naming auxiliary filtees") \ +_ELF_DEFINE_DT(DT_SUNW_RTLDINF, 0x6000000EUL, "rtld internal use") \ +_ELF_DEFINE_DT(DT_SUNW_FILTER, 0x6000000FUL, \ + "offset of string naming standard filtees") \ +_ELF_DEFINE_DT(DT_SUNW_CAP, 0x60000010UL, \ + "address of hardware capabilities section") \ +_ELF_DEFINE_DT(DT_HIOS, 0x6FFFF000UL, \ + "end of OS-specific types") \ +_ELF_DEFINE_DT(DT_VALRNGLO, 0x6FFFFD00UL, \ + "start of range using the d_val field") \ +_ELF_DEFINE_DT(DT_GNU_PRELINKED, 0x6FFFFDF5UL, \ + "prelinking timestamp") \ +_ELF_DEFINE_DT(DT_GNU_CONFLICTSZ, 0x6FFFFDF6UL, \ + "size of conflict section") \ +_ELF_DEFINE_DT(DT_GNU_LIBLISTSZ, 0x6FFFFDF7UL, \ + "size of library list") \ +_ELF_DEFINE_DT(DT_CHECKSUM, 0x6FFFFDF8UL, \ + "checksum for the object") \ +_ELF_DEFINE_DT(DT_PLTPADSZ, 0x6FFFFDF9UL, \ + "size of PLT padding") \ +_ELF_DEFINE_DT(DT_MOVEENT, 0x6FFFFDFAUL, \ + "size of DT_MOVETAB entries") \ +_ELF_DEFINE_DT(DT_MOVESZ, 0x6FFFFDFBUL, \ + "total size of the MOVETAB table") \ +_ELF_DEFINE_DT(DT_FEATURE_1, 0x6FFFFDFCUL, "feature values") \ +_ELF_DEFINE_DT(DT_POSFLAG_1, 0x6FFFFDFDUL, \ + "dynamic position flags") \ +_ELF_DEFINE_DT(DT_SYMINSZ, 0x6FFFFDFEUL, \ + "size of the DT_SYMINFO table") \ +_ELF_DEFINE_DT(DT_SYMINENT, 0x6FFFFDFFUL, \ + "size of a DT_SYMINFO entry") \ +_ELF_DEFINE_DT(DT_VALRNGHI, 0x6FFFFDFFUL, \ + "end of range using the d_val field") \ +_ELF_DEFINE_DT(DT_ADDRRNGLO, 0x6FFFFE00UL, \ + "start of range using the d_ptr field") \ +_ELF_DEFINE_DT(DT_GNU_HASH, 0x6FFFFEF5UL, \ + "GNU style hash tables") \ +_ELF_DEFINE_DT(DT_GNU_CONFLICT, 0x6FFFFEF8UL, \ + "address of conflict section") \ +_ELF_DEFINE_DT(DT_GNU_LIBLIST, 0x6FFFFEF9UL, \ + "address of conflict section") \ +_ELF_DEFINE_DT(DT_CONFIG, 0x6FFFFEFAUL, \ + "configuration file") \ +_ELF_DEFINE_DT(DT_DEPAUDIT, 0x6FFFFEFBUL, \ + "string defining audit libraries") \ +_ELF_DEFINE_DT(DT_AUDIT, 0x6FFFFEFCUL, \ + "string defining audit libraries") \ +_ELF_DEFINE_DT(DT_PLTPAD, 0x6FFFFEFDUL, "PLT padding") \ +_ELF_DEFINE_DT(DT_MOVETAB, 0x6FFFFEFEUL, \ + "address of a move table") \ +_ELF_DEFINE_DT(DT_SYMINFO, 0x6FFFFEFFUL, \ + "address of the symbol information table") \ +_ELF_DEFINE_DT(DT_ADDRRNGHI, 0x6FFFFEFFUL, \ + "end of range using the d_ptr field") \ +_ELF_DEFINE_DT(DT_VERSYM, 0x6FFFFFF0UL, \ + "address of the version section") \ +_ELF_DEFINE_DT(DT_RELACOUNT, 0x6FFFFFF9UL, \ + "count of RELA relocations") \ +_ELF_DEFINE_DT(DT_RELCOUNT, 0x6FFFFFFAUL, \ + "count of REL relocations") \ +_ELF_DEFINE_DT(DT_FLAGS_1, 0x6FFFFFFBUL, "flag values") \ +_ELF_DEFINE_DT(DT_VERDEF, 0x6FFFFFFCUL, \ + "address of the version definition segment") \ +_ELF_DEFINE_DT(DT_VERDEFNUM, 0x6FFFFFFDUL, \ + "the number of version definition entries") \ +_ELF_DEFINE_DT(DT_VERNEED, 0x6FFFFFFEUL, \ + "address of section with needed versions") \ +_ELF_DEFINE_DT(DT_VERNEEDNUM, 0x6FFFFFFFUL, \ + "the number of version needed entries") \ +_ELF_DEFINE_DT(DT_LOPROC, 0x70000000UL, \ + "start of processor-specific types") \ +_ELF_DEFINE_DT(DT_ARM_SYMTABSZ, 0x70000001UL, \ + "number of entries in the dynamic symbol table") \ +_ELF_DEFINE_DT(DT_SPARC_REGISTER, 0x70000001UL, \ + "index of an STT_SPARC_REGISTER symbol") \ +_ELF_DEFINE_DT(DT_ARM_PREEMPTMAP, 0x70000002UL, \ + "address of the preemption map") \ +_ELF_DEFINE_DT(DT_MIPS_RLD_VERSION, 0x70000001UL, \ + "version ID for runtime linker interface") \ +_ELF_DEFINE_DT(DT_MIPS_TIME_STAMP, 0x70000002UL, \ + "timestamp") \ +_ELF_DEFINE_DT(DT_MIPS_ICHECKSUM, 0x70000003UL, \ + "checksum of all external strings and common sizes") \ +_ELF_DEFINE_DT(DT_MIPS_IVERSION, 0x70000004UL, \ + "string table index of a version string") \ +_ELF_DEFINE_DT(DT_MIPS_FLAGS, 0x70000005UL, \ + "MIPS-specific flags") \ +_ELF_DEFINE_DT(DT_MIPS_BASE_ADDRESS, 0x70000006UL, \ + "base address for the executable/DSO") \ +_ELF_DEFINE_DT(DT_MIPS_CONFLICT, 0x70000008UL, \ + "address of .conflict section") \ +_ELF_DEFINE_DT(DT_MIPS_LIBLIST, 0x70000009UL, \ + "address of .liblist section") \ +_ELF_DEFINE_DT(DT_MIPS_LOCAL_GOTNO, 0x7000000AUL, \ + "number of local GOT entries") \ +_ELF_DEFINE_DT(DT_MIPS_CONFLICTNO, 0x7000000BUL, \ + "number of entries in the .conflict section") \ +_ELF_DEFINE_DT(DT_MIPS_LIBLISTNO, 0x70000010UL, \ + "number of entries in the .liblist section") \ +_ELF_DEFINE_DT(DT_MIPS_SYMTABNO, 0x70000011UL, \ + "number of entries in the .dynsym section") \ +_ELF_DEFINE_DT(DT_MIPS_UNREFEXTNO, 0x70000012UL, \ + "index of first external dynamic symbol not ref'ed locally") \ +_ELF_DEFINE_DT(DT_MIPS_GOTSYM, 0x70000013UL, \ + "index of first dynamic symbol corresponds to a GOT entry") \ +_ELF_DEFINE_DT(DT_MIPS_HIPAGENO, 0x70000014UL, \ + "number of page table entries in GOT") \ +_ELF_DEFINE_DT(DT_MIPS_RLD_MAP, 0x70000016UL, \ + "address of runtime linker map") \ +_ELF_DEFINE_DT(DT_MIPS_DELTA_CLASS, 0x70000017UL, \ + "Delta C++ class definition") \ +_ELF_DEFINE_DT(DT_MIPS_DELTA_CLASS_NO, 0x70000018UL, \ + "number of entries in DT_MIPS_DELTA_CLASS") \ +_ELF_DEFINE_DT(DT_MIPS_DELTA_INSTANCE, 0x70000019UL, \ + "Delta C++ class instances") \ +_ELF_DEFINE_DT(DT_MIPS_DELTA_INSTANCE_NO, 0x7000001AUL, \ + "number of entries in DT_MIPS_DELTA_INSTANCE") \ +_ELF_DEFINE_DT(DT_MIPS_DELTA_RELOC, 0x7000001BUL, \ + "Delta relocations") \ +_ELF_DEFINE_DT(DT_MIPS_DELTA_RELOC_NO, 0x7000001CUL, \ + "number of entries in DT_MIPS_DELTA_RELOC") \ +_ELF_DEFINE_DT(DT_MIPS_DELTA_SYM, 0x7000001DUL, \ + "Delta symbols refered by Delta relocations") \ +_ELF_DEFINE_DT(DT_MIPS_DELTA_SYM_NO, 0x7000001EUL, \ + "number of entries in DT_MIPS_DELTA_SYM") \ +_ELF_DEFINE_DT(DT_MIPS_DELTA_CLASSSYM, 0x70000020UL, \ + "Delta symbols for class declarations") \ +_ELF_DEFINE_DT(DT_MIPS_DELTA_CLASSSYM_NO, 0x70000021UL, \ + "number of entries in DT_MIPS_DELTA_CLASSSYM") \ +_ELF_DEFINE_DT(DT_MIPS_CXX_FLAGS, 0x70000022UL, \ + "C++ flavor flags") \ +_ELF_DEFINE_DT(DT_MIPS_PIXIE_INIT, 0x70000023UL, \ + "address of an initialization routine created by pixie") \ +_ELF_DEFINE_DT(DT_MIPS_SYMBOL_LIB, 0x70000024UL, \ + "address of .MIPS.symlib section") \ +_ELF_DEFINE_DT(DT_MIPS_LOCALPAGE_GOTIDX, 0x70000025UL, \ + "GOT index of first page table entry for a segment") \ +_ELF_DEFINE_DT(DT_MIPS_LOCAL_GOTIDX, 0x70000026UL, \ + "GOT index of first page table entry for a local symbol") \ +_ELF_DEFINE_DT(DT_MIPS_HIDDEN_GOTIDX, 0x70000027UL, \ + "GOT index of first page table entry for a hidden symbol") \ +_ELF_DEFINE_DT(DT_MIPS_PROTECTED_GOTIDX, 0x70000028UL, \ + "GOT index of first page table entry for a protected symbol") \ +_ELF_DEFINE_DT(DT_MIPS_OPTIONS, 0x70000029UL, \ + "address of .MIPS.options section") \ +_ELF_DEFINE_DT(DT_MIPS_INTERFACE, 0x7000002AUL, \ + "address of .MIPS.interface section") \ +_ELF_DEFINE_DT(DT_MIPS_DYNSTR_ALIGN, 0x7000002BUL, "???") \ +_ELF_DEFINE_DT(DT_MIPS_INTERFACE_SIZE, 0x7000002CUL, \ + "size of .MIPS.interface section") \ +_ELF_DEFINE_DT(DT_MIPS_RLD_TEXT_RESOLVE_ADDR, 0x7000002DUL, \ + "address of _rld_text_resolve in GOT") \ +_ELF_DEFINE_DT(DT_MIPS_PERF_SUFFIX, 0x7000002EUL, \ + "default suffix of DSO to be appended by dlopen") \ +_ELF_DEFINE_DT(DT_MIPS_COMPACT_SIZE, 0x7000002FUL, \ + "size of a ucode compact relocation record (o32)") \ +_ELF_DEFINE_DT(DT_MIPS_GP_VALUE, 0x70000030UL, \ + "GP value of a specified GP relative range") \ +_ELF_DEFINE_DT(DT_MIPS_AUX_DYNAMIC, 0x70000031UL, \ + "address of an auxiliary dynamic table") \ +_ELF_DEFINE_DT(DT_MIPS_PLTGOT, 0x70000032UL, \ + "address of the PLTGOT") \ +_ELF_DEFINE_DT(DT_MIPS_RLD_OBJ_UPDATE, 0x70000033UL, \ + "object list update callback") \ +_ELF_DEFINE_DT(DT_MIPS_RWPLT, 0x70000034UL, \ + "address of a writable PLT") \ +_ELF_DEFINE_DT(DT_PPC_GOT, 0x70000000UL, \ + "value of _GLOBAL_OFFSET_TABLE_") \ +_ELF_DEFINE_DT(DT_PPC_TLSOPT, 0x70000001UL, \ + "TLS descriptor should be optimized") \ +_ELF_DEFINE_DT(DT_PPC64_GLINK, 0x70000000UL, \ + "address of .glink section") \ +_ELF_DEFINE_DT(DT_PPC64_OPD, 0x70000001UL, \ + "address of .opd section") \ +_ELF_DEFINE_DT(DT_PPC64_OPDSZ, 0x70000002UL, \ + "size of .opd section") \ +_ELF_DEFINE_DT(DT_PPC64_TLSOPT, 0x70000003UL, \ + "TLS descriptor should be optimized") \ +_ELF_DEFINE_DT(DT_AUXILIARY, 0x7FFFFFFDUL, \ + "offset of string naming auxiliary filtees") \ +_ELF_DEFINE_DT(DT_USED, 0x7FFFFFFEUL, "ignored") \ +_ELF_DEFINE_DT(DT_FILTER, 0x7FFFFFFFUL, \ + "index of string naming filtees") \ +_ELF_DEFINE_DT(DT_HIPROC, 0x7FFFFFFFUL, \ + "end of processor-specific types") + +#undef _ELF_DEFINE_DT +#define _ELF_DEFINE_DT(N, V, DESCR) N = V , +enum { + _ELF_DEFINE_DYN_TYPES() + DT__LAST__ = DT_HIPROC +}; +#ifndef DT_DEPRECATED_SPARC_REGISTER +#define DT_DEPRECATED_SPARC_REGISTER DT_SPARC_REGISTER +#endif + +/* + * Flags used in the executable header (field: e_flags). + */ +#define _ELF_DEFINE_EHDR_FLAGS() \ +_ELF_DEFINE_EF(EF_ARM_RELEXEC, 0x00000001UL, \ + "dynamic segment describes only how to relocate segments") \ +_ELF_DEFINE_EF(EF_ARM_HASENTRY, 0x00000002UL, \ + "e_entry contains a program entry point") \ +_ELF_DEFINE_EF(EF_ARM_SYMSARESORTED, 0x00000004UL, \ + "subsection of symbol table is sorted by symbol value") \ +_ELF_DEFINE_EF(EF_ARM_DYNSYMSUSESEGIDX, 0x00000008UL, \ + "dynamic symbol st_shndx = containing segment index + 1") \ +_ELF_DEFINE_EF(EF_ARM_MAPSYMSFIRST, 0x00000010UL, \ + "mapping symbols precede other local symbols in symtab") \ +_ELF_DEFINE_EF(EF_ARM_BE8, 0x00800000UL, \ + "file contains BE-8 code") \ +_ELF_DEFINE_EF(EF_ARM_LE8, 0x00400000UL, \ + "file contains LE-8 code") \ +_ELF_DEFINE_EF(EF_ARM_EABIMASK, 0xFF000000UL, \ + "mask for ARM EABI version number (0 denotes GNU or unknown)") \ +_ELF_DEFINE_EF(EF_ARM_INTERWORK, 0x00000004UL, \ + "GNU EABI extension") \ +_ELF_DEFINE_EF(EF_ARM_APCS_26, 0x00000008UL, \ + "GNU EABI extension") \ +_ELF_DEFINE_EF(EF_ARM_APCS_FLOAT, 0x00000010UL, \ + "GNU EABI extension") \ +_ELF_DEFINE_EF(EF_ARM_PIC, 0x00000020UL, \ + "GNU EABI extension") \ +_ELF_DEFINE_EF(EF_ARM_ALIGN8, 0x00000040UL, \ + "GNU EABI extension") \ +_ELF_DEFINE_EF(EF_ARM_NEW_ABI, 0x00000080UL, \ + "GNU EABI extension") \ +_ELF_DEFINE_EF(EF_ARM_OLD_ABI, 0x00000100UL, \ + "GNU EABI extension") \ +_ELF_DEFINE_EF(EF_ARM_SOFT_FLOAT, 0x00000200UL, \ + "GNU EABI extension") \ +_ELF_DEFINE_EF(EF_ARM_VFP_FLOAT, 0x00000400UL, \ + "GNU EABI extension") \ +_ELF_DEFINE_EF(EF_ARM_MAVERICK_FLOAT, 0x00000800UL, \ + "GNU EABI extension") \ +_ELF_DEFINE_EF(EF_MIPS_NOREORDER, 0x00000001UL, \ + "at least one .noreorder directive appeared in the source") \ +_ELF_DEFINE_EF(EF_MIPS_PIC, 0x00000002UL, \ + "file contains position independent code") \ +_ELF_DEFINE_EF(EF_MIPS_CPIC, 0x00000004UL, \ + "file's code uses standard conventions for calling PIC") \ +_ELF_DEFINE_EF(EF_MIPS_UCODE, 0x00000010UL, \ + "file contains UCODE (obsolete)") \ +_ELF_DEFINE_EF(EF_MIPS_ABI2, 0x00000020UL, \ + "file follows MIPS III 32-bit ABI") \ +_ELF_DEFINE_EF(EF_MIPS_OPTIONS_FIRST, 0x00000080UL, \ + "ld(1) should process .MIPS.options section first") \ +_ELF_DEFINE_EF(EF_MIPS_ARCH_ASE, 0x0F000000UL, \ + "file uses application-specific architectural extensions") \ +_ELF_DEFINE_EF(EF_MIPS_ARCH_ASE_MDMX, 0x08000000UL, \ + "file uses MDMX multimedia extensions") \ +_ELF_DEFINE_EF(EF_MIPS_ARCH_ASE_M16, 0x04000000UL, \ + "file uses MIPS-16 ISA extensions") \ +_ELF_DEFINE_EF(EF_MIPS_ARCH, 0xF0000000UL, \ + "4-bit MIPS architecture field") \ +_ELF_DEFINE_EF(EF_SPARC_EXT_MASK, 0x00ffff00UL, \ + "Vendor Extension mask") \ +_ELF_DEFINE_EF(EF_SPARC_32PLUS, 0x00000100UL, \ + "Generic V8+ features") \ +_ELF_DEFINE_EF(EF_SPARC_SUN_US1, 0x00000200UL, \ + "Sun UltraSPARCTM 1 Extensions") \ +_ELF_DEFINE_EF(EF_SPARC_HAL_R1, 0x00000400UL, "HAL R1 Extensions") \ +_ELF_DEFINE_EF(EF_SPARC_SUN_US3, 0x00000800UL, \ + "Sun UltraSPARC 3 Extensions") \ +_ELF_DEFINE_EF(EF_SPARCV9_MM, 0x00000003UL, \ + "Mask for Memory Model") \ +_ELF_DEFINE_EF(EF_SPARCV9_TSO, 0x00000000UL, \ + "Total Store Ordering") \ +_ELF_DEFINE_EF(EF_SPARCV9_PSO, 0x00000001UL, \ + "Partial Store Ordering") \ +_ELF_DEFINE_EF(EF_SPARCV9_RMO, 0x00000002UL, \ + "Relaxed Memory Ordering") + +#undef _ELF_DEFINE_EF +#define _ELF_DEFINE_EF(N, V, DESCR) N = V , +enum { + _ELF_DEFINE_EHDR_FLAGS() + EF__LAST__ +}; + +/* + * Offsets in the `ei_ident[]` field of an ELF executable header. + */ +#define _ELF_DEFINE_EI_OFFSETS() \ +_ELF_DEFINE_EI(EI_MAG0, 0, "magic number") \ +_ELF_DEFINE_EI(EI_MAG1, 1, "magic number") \ +_ELF_DEFINE_EI(EI_MAG2, 2, "magic number") \ +_ELF_DEFINE_EI(EI_MAG3, 3, "magic number") \ +_ELF_DEFINE_EI(EI_CLASS, 4, "file class") \ +_ELF_DEFINE_EI(EI_DATA, 5, "data encoding") \ +_ELF_DEFINE_EI(EI_VERSION, 6, "file version") \ +_ELF_DEFINE_EI(EI_OSABI, 7, "OS ABI kind") \ +_ELF_DEFINE_EI(EI_ABIVERSION, 8, "OS ABI version") \ +_ELF_DEFINE_EI(EI_PAD, 9, "padding start") \ +_ELF_DEFINE_EI(EI_NIDENT, 16, "total size") + +#undef _ELF_DEFINE_EI +#define _ELF_DEFINE_EI(N, V, DESCR) N = V , +enum { + _ELF_DEFINE_EI_OFFSETS() + EI__LAST__ +}; + +/* + * The ELF class of an object. + */ +#define _ELF_DEFINE_ELFCLASS() \ +_ELF_DEFINE_EC(ELFCLASSNONE, 0, "Unknown ELF class") \ +_ELF_DEFINE_EC(ELFCLASS32, 1, "32 bit objects") \ +_ELF_DEFINE_EC(ELFCLASS64, 2, "64 bit objects") + +#undef _ELF_DEFINE_EC +#define _ELF_DEFINE_EC(N, V, DESCR) N = V , +enum { + _ELF_DEFINE_ELFCLASS() + EC__LAST__ +}; + +/* + * Endianness of data in an ELF object. + */ + +#define _ELF_DEFINE_ELF_DATA_ENDIANNESS() \ +_ELF_DEFINE_ED(ELFDATANONE, 0, "Unknown data endianness") \ +_ELF_DEFINE_ED(ELFDATA2LSB, 1, "little endian") \ +_ELF_DEFINE_ED(ELFDATA2MSB, 2, "big endian") + +#undef _ELF_DEFINE_ED +#define _ELF_DEFINE_ED(N, V, DESCR) N = V , +enum { + _ELF_DEFINE_ELF_DATA_ENDIANNESS() + ED__LAST__ +}; + +/* + * Values of the magic numbers used in identification array. + */ +#define _ELF_DEFINE_ELF_MAGIC() \ +_ELF_DEFINE_EMAG(ELFMAG0, 0x7FU) \ +_ELF_DEFINE_EMAG(ELFMAG1, 'E') \ +_ELF_DEFINE_EMAG(ELFMAG2, 'L') \ +_ELF_DEFINE_EMAG(ELFMAG3, 'F') + +#undef _ELF_DEFINE_EMAG +#define _ELF_DEFINE_EMAG(N, V) N = V , +enum { + _ELF_DEFINE_ELF_MAGIC() + ELFMAG__LAST__ +}; + +/* + * ELF OS ABI field. + */ +#define _ELF_DEFINE_ELF_OSABI() \ +_ELF_DEFINE_EABI(ELFOSABI_NONE, 0, \ + "No extensions or unspecified") \ +_ELF_DEFINE_EABI(ELFOSABI_SYSV, 0, "SYSV") \ +_ELF_DEFINE_EABI(ELFOSABI_HPUX, 1, "Hewlett-Packard HP-UX") \ +_ELF_DEFINE_EABI(ELFOSABI_NETBSD, 2, "NetBSD") \ +_ELF_DEFINE_EABI(ELFOSABI_GNU, 3, "GNU") \ +_ELF_DEFINE_EABI(ELFOSABI_HURD, 4, "GNU/HURD") \ +_ELF_DEFINE_EABI(ELFOSABI_86OPEN, 5, "86Open Common ABI") \ +_ELF_DEFINE_EABI(ELFOSABI_SOLARIS, 6, "Sun Solaris") \ +_ELF_DEFINE_EABI(ELFOSABI_AIX, 7, "AIX") \ +_ELF_DEFINE_EABI(ELFOSABI_IRIX, 8, "IRIX") \ +_ELF_DEFINE_EABI(ELFOSABI_FREEBSD, 9, "FreeBSD") \ +_ELF_DEFINE_EABI(ELFOSABI_TRU64, 10, "Compaq TRU64 UNIX") \ +_ELF_DEFINE_EABI(ELFOSABI_MODESTO, 11, "Novell Modesto") \ +_ELF_DEFINE_EABI(ELFOSABI_OPENBSD, 12, "Open BSD") \ +_ELF_DEFINE_EABI(ELFOSABI_OPENVMS, 13, "Open VMS") \ +_ELF_DEFINE_EABI(ELFOSABI_NSK, 14, \ + "Hewlett-Packard Non-Stop Kernel") \ +_ELF_DEFINE_EABI(ELFOSABI_AROS, 15, "Amiga Research OS") \ +_ELF_DEFINE_EABI(ELFOSABI_FENIXOS, 16, \ + "The FenixOS highly scalable multi-core OS") \ +_ELF_DEFINE_EABI(ELFOSABI_ARM_AEABI, 64, \ + "ARM specific symbol versioning extensions") \ +_ELF_DEFINE_EABI(ELFOSABI_ARM, 97, "ARM ABI") \ +_ELF_DEFINE_EABI(ELFOSABI_AMD_OPENCL, 201, "AMD OpenCL Runtime ABI") \ +_ELF_DEFINE_EABI(ELFOSABI_HSAIL, 202, "AMD HSAIL Runtime ABI") \ +_ELF_DEFINE_EABI(ELFOSABI_AMDIL, 203, "AMD CAL Runtime ABI") \ +_ELF_DEFINE_EABI(ELFOSABI_STANDALONE, 255, \ + "Standalone (embedded) application") + +#undef _ELF_DEFINE_EABI +#define _ELF_DEFINE_EABI(N, V, DESCR) N = V , +enum { + _ELF_DEFINE_ELF_OSABI() + ELFOSABI__LAST__ +}; +#ifndef ELFOSABI_LINUX +#define ELFOSABI_LINUX ELFOSABI_GNU +#endif + +/* + * ELF Machine types: (EM_*). + */ +#define _ELF_DEFINE_ELF_MACHINES() \ +_ELF_DEFINE_EM(EM_NONE, 0, "No machine") \ +_ELF_DEFINE_EM(EM_M32, 1, "AT&T WE 32100") \ +_ELF_DEFINE_EM(EM_SPARC, 2, "SPARC") \ +_ELF_DEFINE_EM(EM_386, 3, "Intel 80386") \ +_ELF_DEFINE_EM(EM_68K, 4, "Motorola 68000") \ +_ELF_DEFINE_EM(EM_88K, 5, "Motorola 88000") \ +_ELF_DEFINE_EM(EM_860, 7, "Intel 80860") \ +_ELF_DEFINE_EM(EM_MIPS, 8, "MIPS I Architecture") \ +_ELF_DEFINE_EM(EM_S370, 9, "IBM System/370 Processor") \ +_ELF_DEFINE_EM(EM_MIPS_RS3_LE, 10, "MIPS RS3000 Little-endian") \ +_ELF_DEFINE_EM(EM_PARISC, 15, "Hewlett-Packard PA-RISC") \ +_ELF_DEFINE_EM(EM_VPP500, 17, "Fujitsu VPP500") \ +_ELF_DEFINE_EM(EM_SPARC32PLUS, 18, \ + "Enhanced instruction set SPARC") \ +_ELF_DEFINE_EM(EM_960, 19, "Intel 80960") \ +_ELF_DEFINE_EM(EM_PPC, 20, "PowerPC") \ +_ELF_DEFINE_EM(EM_PPC64, 21, "64-bit PowerPC") \ +_ELF_DEFINE_EM(EM_S390, 22, "IBM System/390 Processor") \ +_ELF_DEFINE_EM(EM_SPU, 23, "IBM SPU/SPC") \ +_ELF_DEFINE_EM(EM_V800, 36, "NEC V800") \ +_ELF_DEFINE_EM(EM_FR20, 37, "Fujitsu FR20") \ +_ELF_DEFINE_EM(EM_RH32, 38, "TRW RH-32") \ +_ELF_DEFINE_EM(EM_RCE, 39, "Motorola RCE") \ +_ELF_DEFINE_EM(EM_ARM, 40, "Advanced RISC Machines ARM") \ +_ELF_DEFINE_EM(EM_ALPHA, 41, "Digital Alpha") \ +_ELF_DEFINE_EM(EM_SH, 42, "Hitachi SH") \ +_ELF_DEFINE_EM(EM_SPARCV9, 43, "SPARC Version 9") \ +_ELF_DEFINE_EM(EM_TRICORE, 44, \ + "Siemens TriCore embedded processor") \ +_ELF_DEFINE_EM(EM_ARC, 45, \ + "Argonaut RISC Core, Argonaut Technologies Inc.") \ +_ELF_DEFINE_EM(EM_H8_300, 46, "Hitachi H8/300") \ +_ELF_DEFINE_EM(EM_H8_300H, 47, "Hitachi H8/300H") \ +_ELF_DEFINE_EM(EM_H8S, 48, "Hitachi H8S") \ +_ELF_DEFINE_EM(EM_H8_500, 49, "Hitachi H8/500") \ +_ELF_DEFINE_EM(EM_IA_64, 50, \ + "Intel IA-64 processor architecture") \ +_ELF_DEFINE_EM(EM_MIPS_X, 51, "Stanford MIPS-X") \ +_ELF_DEFINE_EM(EM_COLDFIRE, 52, "Motorola ColdFire") \ +_ELF_DEFINE_EM(EM_68HC12, 53, "Motorola M68HC12") \ +_ELF_DEFINE_EM(EM_MMA, 54, \ + "Fujitsu MMA Multimedia Accelerator") \ +_ELF_DEFINE_EM(EM_PCP, 55, "Siemens PCP") \ +_ELF_DEFINE_EM(EM_NCPU, 56, \ + "Sony nCPU embedded RISC processor") \ +_ELF_DEFINE_EM(EM_NDR1, 57, "Denso NDR1 microprocessor") \ +_ELF_DEFINE_EM(EM_STARCORE, 58, "Motorola Star*Core processor") \ +_ELF_DEFINE_EM(EM_ME16, 59, "Toyota ME16 processor") \ +_ELF_DEFINE_EM(EM_ST100, 60, \ + "STMicroelectronics ST100 processor") \ +_ELF_DEFINE_EM(EM_TINYJ, 61, \ + "Advanced Logic Corp. TinyJ embedded processor family") \ +_ELF_DEFINE_EM(EM_X86_64, 62, "AMD x86-64 architecture") \ +_ELF_DEFINE_EM(EM_PDSP, 63, "Sony DSP Processor") \ +_ELF_DEFINE_EM(EM_PDP10, 64, \ + "Digital Equipment Corp. PDP-10") \ +_ELF_DEFINE_EM(EM_PDP11, 65, \ + "Digital Equipment Corp. PDP-11") \ +_ELF_DEFINE_EM(EM_FX66, 66, "Siemens FX66 microcontroller") \ +_ELF_DEFINE_EM(EM_ST9PLUS, 67, \ + "STMicroelectronics ST9+ 8/16 bit microcontroller") \ +_ELF_DEFINE_EM(EM_ST7, 68, \ + "STMicroelectronics ST7 8-bit microcontroller") \ +_ELF_DEFINE_EM(EM_68HC16, 69, \ + "Motorola MC68HC16 Microcontroller") \ +_ELF_DEFINE_EM(EM_68HC11, 70, \ + "Motorola MC68HC11 Microcontroller") \ +_ELF_DEFINE_EM(EM_68HC08, 71, \ + "Motorola MC68HC08 Microcontroller") \ +_ELF_DEFINE_EM(EM_68HC05, 72, \ + "Motorola MC68HC05 Microcontroller") \ +_ELF_DEFINE_EM(EM_SVX, 73, "Silicon Graphics SVx") \ +_ELF_DEFINE_EM(EM_ST19, 74, \ + "STMicroelectronics ST19 8-bit microcontroller") \ +_ELF_DEFINE_EM(EM_VAX, 75, "Digital VAX") \ +_ELF_DEFINE_EM(EM_CRIS, 76, \ + "Axis Communications 32-bit embedded processor") \ +_ELF_DEFINE_EM(EM_JAVELIN, 77, \ + "Infineon Technologies 32-bit embedded processor") \ +_ELF_DEFINE_EM(EM_FIREPATH, 78, \ + "Element 14 64-bit DSP Processor") \ +_ELF_DEFINE_EM(EM_ZSP, 79, \ + "LSI Logic 16-bit DSP Processor") \ +_ELF_DEFINE_EM(EM_MMIX, 80, \ + "Donald Knuth's educational 64-bit processor") \ +_ELF_DEFINE_EM(EM_HUANY, 81, \ + "Harvard University machine-independent object files") \ +_ELF_DEFINE_EM(EM_PRISM, 82, "SiTera Prism") \ +_ELF_DEFINE_EM(EM_AVR, 83, \ + "Atmel AVR 8-bit microcontroller") \ +_ELF_DEFINE_EM(EM_FR30, 84, "Fujitsu FR30") \ +_ELF_DEFINE_EM(EM_D10V, 85, "Mitsubishi D10V") \ +_ELF_DEFINE_EM(EM_D30V, 86, "Mitsubishi D30V") \ +_ELF_DEFINE_EM(EM_V850, 87, "NEC v850") \ +_ELF_DEFINE_EM(EM_M32R, 88, "Mitsubishi M32R") \ +_ELF_DEFINE_EM(EM_MN10300, 89, "Matsushita MN10300") \ +_ELF_DEFINE_EM(EM_MN10200, 90, "Matsushita MN10200") \ +_ELF_DEFINE_EM(EM_PJ, 91, "picoJava") \ +_ELF_DEFINE_EM(EM_OPENRISC, 92, \ + "OpenRISC 32-bit embedded processor") \ +_ELF_DEFINE_EM(EM_ARC_COMPACT, 93, \ + "ARC International ARCompact processor") \ +_ELF_DEFINE_EM(EM_XTENSA, 94, \ + "Tensilica Xtensa Architecture") \ +_ELF_DEFINE_EM(EM_VIDEOCORE, 95, \ + "Alphamosaic VideoCore processor") \ +_ELF_DEFINE_EM(EM_TMM_GPP, 96, \ + "Thompson Multimedia General Purpose Processor") \ +_ELF_DEFINE_EM(EM_NS32K, 97, \ + "National Semiconductor 32000 series") \ +_ELF_DEFINE_EM(EM_TPC, 98, "Tenor Network TPC processor") \ +_ELF_DEFINE_EM(EM_SNP1K, 99, "Trebia SNP 1000 processor") \ +_ELF_DEFINE_EM(EM_ST200, 100, \ + "STMicroelectronics (www.st.com) ST200 microcontroller") \ +_ELF_DEFINE_EM(EM_IP2K, 101, \ + "Ubicom IP2xxx microcontroller family") \ +_ELF_DEFINE_EM(EM_MAX, 102, "MAX Processor") \ +_ELF_DEFINE_EM(EM_CR, 103, \ + "National Semiconductor CompactRISC microprocessor") \ +_ELF_DEFINE_EM(EM_F2MC16, 104, "Fujitsu F2MC16") \ +_ELF_DEFINE_EM(EM_MSP430, 105, \ + "Texas Instruments embedded microcontroller msp430") \ +_ELF_DEFINE_EM(EM_BLACKFIN, 106, \ + "Analog Devices Blackfin (DSP) processor") \ +_ELF_DEFINE_EM(EM_SE_C33, 107, \ + "S1C33 Family of Seiko Epson processors") \ +_ELF_DEFINE_EM(EM_SEP, 108, \ + "Sharp embedded microprocessor") \ +_ELF_DEFINE_EM(EM_ARCA, 109, "Arca RISC Microprocessor") \ +_ELF_DEFINE_EM(EM_UNICORE, 110, \ + "Microprocessor series from PKU-Unity Ltd. and MPRC of Peking University") \ +_ELF_DEFINE_EM(EM_EXCESS, 111, \ + "eXcess: 16/32/64-bit configurable embedded CPU") \ +_ELF_DEFINE_EM(EM_DXP, 112, \ + "Icera Semiconductor Inc. Deep Execution Processor") \ +_ELF_DEFINE_EM(EM_ALTERA_NIOS2, 113, \ + "Altera Nios II soft-core processor") \ +_ELF_DEFINE_EM(EM_CRX, 114, \ + "National Semiconductor CompactRISC CRX microprocessor") \ +_ELF_DEFINE_EM(EM_XGATE, 115, \ + "Motorola XGATE embedded processor") \ +_ELF_DEFINE_EM(EM_C166, 116, \ + "Infineon C16x/XC16x processor") \ +_ELF_DEFINE_EM(EM_M16C, 117, \ + "Renesas M16C series microprocessors") \ +_ELF_DEFINE_EM(EM_DSPIC30F, 118, \ + "Microchip Technology dsPIC30F Digital Signal Controller") \ +_ELF_DEFINE_EM(EM_CE, 119, \ + "Freescale Communication Engine RISC core") \ +_ELF_DEFINE_EM(EM_M32C, 120, \ + "Renesas M32C series microprocessors") \ +_ELF_DEFINE_EM(EM_TSK3000, 131, "Altium TSK3000 core") \ +_ELF_DEFINE_EM(EM_RS08, 132, \ + "Freescale RS08 embedded processor") \ +_ELF_DEFINE_EM(EM_SHARC, 133, \ + "Analog Devices SHARC family of 32-bit DSP processors") \ +_ELF_DEFINE_EM(EM_ECOG2, 134, \ + "Cyan Technology eCOG2 microprocessor") \ +_ELF_DEFINE_EM(EM_SCORE7, 135, \ + "Sunplus S+core7 RISC processor") \ +_ELF_DEFINE_EM(EM_DSP24, 136, \ + "New Japan Radio (NJR) 24-bit DSP Processor") \ +_ELF_DEFINE_EM(EM_VIDEOCORE3, 137, \ + "Broadcom VideoCore III processor") \ +_ELF_DEFINE_EM(EM_LATTICEMICO32, 138, \ + "RISC processor for Lattice FPGA architecture") \ +_ELF_DEFINE_EM(EM_SE_C17, 139, "Seiko Epson C17 family") \ +_ELF_DEFINE_EM(EM_TI_C6000, 140, \ + "The Texas Instruments TMS320C6000 DSP family") \ +_ELF_DEFINE_EM(EM_TI_C2000, 141, \ + "The Texas Instruments TMS320C2000 DSP family") \ +_ELF_DEFINE_EM(EM_TI_C5500, 142, \ + "The Texas Instruments TMS320C55x DSP family") \ +_ELF_DEFINE_EM(EM_MMDSP_PLUS, 160, \ + "STMicroelectronics 64bit VLIW Data Signal Processor") \ +_ELF_DEFINE_EM(EM_CYPRESS_M8C, 161, "Cypress M8C microprocessor") \ +_ELF_DEFINE_EM(EM_R32C, 162, \ + "Renesas R32C series microprocessors") \ +_ELF_DEFINE_EM(EM_TRIMEDIA, 163, \ + "NXP Semiconductors TriMedia architecture family") \ +_ELF_DEFINE_EM(EM_QDSP6, 164, "QUALCOMM DSP6 Processor") \ +_ELF_DEFINE_EM(EM_8051, 165, "Intel 8051 and variants") \ +_ELF_DEFINE_EM(EM_STXP7X, 166, \ + "STMicroelectronics STxP7x family of configurable and extensible RISC processors") \ +_ELF_DEFINE_EM(EM_NDS32, 167, \ + "Andes Technology compact code size embedded RISC processor family") \ +_ELF_DEFINE_EM(EM_ECOG1, 168, \ + "Cyan Technology eCOG1X family") \ +_ELF_DEFINE_EM(EM_ECOG1X, 168, \ + "Cyan Technology eCOG1X family") \ +_ELF_DEFINE_EM(EM_MAXQ30, 169, \ + "Dallas Semiconductor MAXQ30 Core Micro-controllers") \ +_ELF_DEFINE_EM(EM_XIMO16, 170, \ + "New Japan Radio (NJR) 16-bit DSP Processor") \ +_ELF_DEFINE_EM(EM_MANIK, 171, \ + "M2000 Reconfigurable RISC Microprocessor") \ +_ELF_DEFINE_EM(EM_CRAYNV2, 172, \ + "Cray Inc. NV2 vector architecture") \ +_ELF_DEFINE_EM(EM_RX, 173, "Renesas RX family") \ +_ELF_DEFINE_EM(EM_METAG, 174, \ + "Imagination Technologies META processor architecture") \ +_ELF_DEFINE_EM(EM_MCST_ELBRUS, 175, \ + "MCST Elbrus general purpose hardware architecture") \ +_ELF_DEFINE_EM(EM_ECOG16, 176, \ + "Cyan Technology eCOG16 family") \ +_ELF_DEFINE_EM(EM_CR16, 177, \ + "National Semiconductor CompactRISC CR16 16-bit microprocessor") \ +_ELF_DEFINE_EM(EM_ETPU, 178, \ + "Freescale Extended Time Processing Unit") \ +_ELF_DEFINE_EM(EM_SLE9X, 179, \ + "Infineon Technologies SLE9X core") \ +_ELF_DEFINE_EM(EM_AVR32, 185, \ + "Atmel Corporation 32-bit microprocessor family") \ +_ELF_DEFINE_EM(EM_STM8, 186, \ + "STMicroeletronics STM8 8-bit microcontroller") \ +_ELF_DEFINE_EM(EM_TILE64, 187, \ + "Tilera TILE64 multicore architecture family") \ +_ELF_DEFINE_EM(EM_TILEPRO, 188, \ + "Tilera TILEPro multicore architecture family") \ +_ELF_DEFINE_EM(EM_MICROBLAZE, 189, \ + "Xilinx MicroBlaze 32-bit RISC soft processor core") \ +_ELF_DEFINE_EM(EM_CUDA, 190, "NVIDIA CUDA architecture") \ +_ELF_DEFINE_EM(EM_TILEGX, 191, \ + "Tilera TILE-Gx multicore architecture family") \ +_ELF_DEFINE_EM(EM_CLOUDSHIELD, 192, \ + "CloudShield architecture family") \ +_ELF_DEFINE_EM(EM_COREA_1ST, 193, \ + "KIPO-KAIST Core-A 1st generation processor family") \ +_ELF_DEFINE_EM(EM_COREA_2ND, 194, \ + "KIPO-KAIST Core-A 2nd generation processor family") \ +_ELF_DEFINE_EM(EM_ARC_COMPACT2, 195, "Synopsys ARCompact V2") \ +_ELF_DEFINE_EM(EM_OPEN8, 196, \ + "Open8 8-bit RISC soft processor core") \ +_ELF_DEFINE_EM(EM_RL78, 197, "Renesas RL78 family") \ +_ELF_DEFINE_EM(EM_VIDEOCORE5, 198, "Broadcom VideoCore V processor") \ +_ELF_DEFINE_EM(EM_78KOR, 199, "Renesas 78KOR family") \ +_ELF_DEFINE_EM(EM_AMDIL, 0x4154, "AMD Intermediate Language Target") \ +_ELF_DEFINE_EM(EM_HSAIL, 0xAF5A, "HSA Intermediate Language Target") \ +_ELF_DEFINE_EM(EM_AMDIL_64, 0x4155, "AMD64 Intermediate Language Target") \ +_ELF_DEFINE_EM(EM_HSAIL_64, 0xAF5B, "HSA64 Intermediate Language Target") \ +_ELF_DEFINE_EM(EM_ARM_64, 0xAA64, "Advanced RISC Machines ARM 64") + + +#undef _ELF_DEFINE_EM +#define _ELF_DEFINE_EM(N, V, DESCR) N = V , +enum { + _ELF_DEFINE_ELF_MACHINES() + EM__LAST__ +}; + +/* Older synonyms. */ +#ifndef EM_ARC_A5 +#define EM_ARC_A5 EM_ARC_COMPACT +#endif + +/* + * ELF file types: (ET_*). + */ +#define _ELF_DEFINE_ELF_TYPES() \ +_ELF_DEFINE_ET(ET_NONE, 0, "No file type") \ +_ELF_DEFINE_ET(ET_REL, 1, "Relocatable object") \ +_ELF_DEFINE_ET(ET_EXEC, 2, "Executable") \ +_ELF_DEFINE_ET(ET_DYN, 3, "Shared object") \ +_ELF_DEFINE_ET(ET_CORE, 4, "Core file") \ +_ELF_DEFINE_ET(ET_LOOS, 0xFE00U, "Begin OS-specific range") \ +_ELF_DEFINE_ET(ET_HIOS, 0xFEFFU, "End OS-specific range") \ +_ELF_DEFINE_ET(ET_LOPROC, 0xFF00U, "Begin processor-specific range") \ +_ELF_DEFINE_ET(ET_HIPROC, 0xFFFFU, "End processor-specific range") + +#undef _ELF_DEFINE_ET +#define _ELF_DEFINE_ET(N, V, DESCR) N = V , +enum { + _ELF_DEFINE_ELF_TYPES() + ET__LAST__ +}; + +/* ELF file format version numbers. */ +#define EV_NONE 0 +#define EV_CURRENT 1 + +/* + * Flags for section groups. + */ +#define GRP_COMDAT 0x1 /* COMDAT semantics */ +#define GRP_MASKOS 0x0ff00000 /* OS-specific flags */ +#define GRP_MASKPROC 0xf0000000 /* processor-specific flags */ + +/* + * Flags used by program header table entries. + */ + +#define _ELF_DEFINE_PHDR_FLAGS() \ +_ELF_DEFINE_PF(PF_X, 0x1, "Execute") \ +_ELF_DEFINE_PF(PF_W, 0x2, "Write") \ +_ELF_DEFINE_PF(PF_R, 0x4, "Read") \ +_ELF_DEFINE_PF(PF_MASKOS, 0x0ff00000, "OS-specific flags") \ +_ELF_DEFINE_PF(PF_MASKPROC, 0xf0000000, "Processor-specific flags") \ +_ELF_DEFINE_PF(PF_ARM_SB, 0x10000000, \ + "segment contains the location addressed by the static base") \ +_ELF_DEFINE_PF(PF_ARM_PI, 0x20000000, \ + "segment is position-independent") \ +_ELF_DEFINE_PF(PF_ARM_ABS, 0x40000000, \ + "segment must be loaded at its base address") + +#undef _ELF_DEFINE_PF +#define _ELF_DEFINE_PF(N, V, DESCR) N = V , +enum { + _ELF_DEFINE_PHDR_FLAGS() + PF__LAST__ +}; + +/* + * Types of program header table entries. + */ + +#define _ELF_DEFINE_PHDR_TYPES() \ +_ELF_DEFINE_PT(PT_NULL, 0, "ignored entry") \ +_ELF_DEFINE_PT(PT_LOAD, 1, "loadable segment") \ +_ELF_DEFINE_PT(PT_DYNAMIC, 2, \ + "contains dynamic linking information") \ +_ELF_DEFINE_PT(PT_INTERP, 3, "names an interpreter") \ +_ELF_DEFINE_PT(PT_NOTE, 4, "auxiliary information") \ +_ELF_DEFINE_PT(PT_SHLIB, 5, "reserved") \ +_ELF_DEFINE_PT(PT_PHDR, 6, \ + "describes the program header itself") \ +_ELF_DEFINE_PT(PT_TLS, 7, "thread local storage") \ +_ELF_DEFINE_PT(PT_LOOS, 0x60000000UL, \ + "start of OS-specific range") \ +_ELF_DEFINE_PT(PT_GNU_EH_FRAME, 0x6474E550UL, \ + "GCC generated .eh_frame_hdr segment ") \ +_ELF_DEFINE_PT(PT_GNU_STACK, 0x6474E551UL, \ + "Stack flags") \ +_ELF_DEFINE_PT(PT_GNU_RELRO, 0x6474E552UL, \ + "Segment becomes read-only after relocation") \ +_ELF_DEFINE_PT(PT_HIOS, 0x6FFFFFFFUL, \ + "end of OS-specific range") \ +_ELF_DEFINE_PT(PT_LOPROC, 0x70000000UL, \ + "start of processor-specific range") \ +_ELF_DEFINE_PT(PT_ARM_ARCHEXT, 0x70000000UL, \ + "platform architecture compatibility information") \ +_ELF_DEFINE_PT(PT_ARM_EXIDX, 0x70000001UL, \ + "exception unwind tables") \ +_ELF_DEFINE_PT(PT_MIPS_REGINFO, 0x70000000UL, \ + "register usage information") \ +_ELF_DEFINE_PT(PT_MIPS_RTPROC, 0x70000001UL, \ + "runtime procedure table") \ +_ELF_DEFINE_PT(PT_MIPS_OPTIONS, 0x70000002UL, \ + "options segment") \ +_ELF_DEFINE_PT(PT_HIPROC, 0x7FFFFFFFUL, \ + "end of processor-specific range") + +#undef _ELF_DEFINE_PT +#define _ELF_DEFINE_PT(N, V, DESCR) N = V , +enum { + _ELF_DEFINE_PHDR_TYPES() + PT__LAST__ = PT_HIPROC +}; + +/* synonyms. */ +#define PT_ARM_UNWIND PT_ARM_EXIDX + +/* + * Section flags. + */ + +#define _ELF_DEFINE_SECTION_FLAGS() \ +_ELF_DEFINE_SHF(SHF_WRITE, 0x1, \ + "writable during program execution") \ +_ELF_DEFINE_SHF(SHF_ALLOC, 0x2, \ + "occupies memory during program execution") \ +_ELF_DEFINE_SHF(SHF_EXECINSTR, 0x4, "executable instructions") \ +_ELF_DEFINE_SHF(SHF_MERGE, 0x10, \ + "may be merged to prevent duplication") \ +_ELF_DEFINE_SHF(SHF_STRINGS, 0x20, \ + "NUL-terminated character strings") \ +_ELF_DEFINE_SHF(SHF_INFO_LINK, 0x40, \ + "the sh_info field holds a link") \ +_ELF_DEFINE_SHF(SHF_LINK_ORDER, 0x80, \ + "special ordering requirements during linking") \ +_ELF_DEFINE_SHF(SHF_OS_NONCONFORMING, 0x100, \ + "requires OS-specific processing during linking") \ +_ELF_DEFINE_SHF(SHF_GROUP, 0x200, \ + "member of a section group") \ +_ELF_DEFINE_SHF(SHF_TLS, 0x400, \ + "holds thread-local storage") \ +_ELF_DEFINE_SHF(SHF_MASKOS, 0x0FF00000UL, \ + "bits reserved for OS-specific semantics") \ +_ELF_DEFINE_SHF(SHF_AMD64_LARGE, 0x10000000UL, \ + "section uses large code model") \ +_ELF_DEFINE_SHF(SHF_ENTRYSECT, 0x10000000UL, \ + "section contains an entry point (ARM)") \ +_ELF_DEFINE_SHF(SHF_COMDEF, 0x80000000UL, \ + "section may be multiply defined in input to link step (ARM)") \ +_ELF_DEFINE_SHF(SHF_MIPS_GPREL, 0x10000000UL, \ + "section must be part of global data area") \ +_ELF_DEFINE_SHF(SHF_MIPS_MERGE, 0x20000000UL, \ + "section data should be merged to eliminate duplication") \ +_ELF_DEFINE_SHF(SHF_MIPS_ADDR, 0x40000000UL, \ + "section data is addressed by default") \ +_ELF_DEFINE_SHF(SHF_MIPS_STRING, 0x80000000UL, \ + "section data is string data by default") \ +_ELF_DEFINE_SHF(SHF_MIPS_NOSTRIP, 0x08000000UL, \ + "section data may not be stripped") \ +_ELF_DEFINE_SHF(SHF_MIPS_LOCAL, 0x04000000UL, \ + "section data local to process") \ +_ELF_DEFINE_SHF(SHF_MIPS_NAMES, 0x02000000UL, \ + "linker must generate implicit hidden weak names") \ +_ELF_DEFINE_SHF(SHF_MIPS_NODUPE, 0x01000000UL, \ + "linker must retain only one copy") \ +_ELF_DEFINE_SHF(SHF_ORDERED, 0x40000000UL, \ + "section is ordered with respect to other sections") \ +_ELF_DEFINE_SHF(SHF_EXCLUDE, 0x80000000UL, \ + "section is excluded from executables and shared objects") \ +_ELF_DEFINE_SHF(SHF_MASKPROC, 0xF0000000UL, \ + "bits reserved for processor-specific semantics") + +#undef _ELF_DEFINE_SHF +#define _ELF_DEFINE_SHF(N, V, DESCR) N = V , +enum { + _ELF_DEFINE_SECTION_FLAGS() + SHF__LAST__ +}; + +/* + * Special section indices. + */ +#define _ELF_DEFINE_SECTION_INDICES() \ +_ELF_DEFINE_SHN(SHN_UNDEF, 0, "undefined section") \ +_ELF_DEFINE_SHN(SHN_LORESERVE, 0xFF00U, "start of reserved area") \ +_ELF_DEFINE_SHN(SHN_LOPROC, 0xFF00U, \ + "start of processor-specific range") \ +_ELF_DEFINE_SHN(SHN_BEFORE, 0xFF00U, "used for section ordering") \ +_ELF_DEFINE_SHN(SHN_AFTER, 0xFF01U, "used for section ordering") \ +_ELF_DEFINE_SHN(SHN_AMD64_LCOMMON, 0xFF02U, "large common block label") \ +_ELF_DEFINE_SHN(SHN_MIPS_ACOMMON, 0xFF00U, \ + "allocated common symbols in a DSO") \ +_ELF_DEFINE_SHN(SHN_MIPS_TEXT, 0xFF01U, "Reserved (obsolete)") \ +_ELF_DEFINE_SHN(SHN_MIPS_DATA, 0xFF02U, "Reserved (obsolete)") \ +_ELF_DEFINE_SHN(SHN_MIPS_SCOMMON, 0xFF03U, \ + "gp-addressable common symbols") \ +_ELF_DEFINE_SHN(SHN_MIPS_SUNDEFINED, 0xFF04U, \ + "gp-addressable undefined symbols") \ +_ELF_DEFINE_SHN(SHN_MIPS_LCOMMON, 0xFF05U, "local common symbols") \ +_ELF_DEFINE_SHN(SHN_MIPS_LUNDEFINED, 0xFF06U, \ + "local undefined symbols") \ +_ELF_DEFINE_SHN(SHN_HIPROC, 0xFF1FU, \ + "end of processor-specific range") \ +_ELF_DEFINE_SHN(SHN_LOOS, 0xFF20U, \ + "start of OS-specific range") \ +_ELF_DEFINE_SHN(SHN_SUNW_IGNORE, 0xFF3FU, "used by dtrace") \ +_ELF_DEFINE_SHN(SHN_HIOS, 0xFF3FU, \ + "end of OS-specific range") \ +_ELF_DEFINE_SHN(SHN_ABS, 0xFFF1U, "absolute references") \ +_ELF_DEFINE_SHN(SHN_COMMON, 0xFFF2U, "references to COMMON areas") \ +_ELF_DEFINE_SHN(SHN_XINDEX, 0xFFFFU, "extended index") \ +_ELF_DEFINE_SHN(SHN_HIRESERVE, 0xFFFFU, "end of reserved area") + +#undef _ELF_DEFINE_SHN +#define _ELF_DEFINE_SHN(N, V, DESCR) N = V , +enum { + _ELF_DEFINE_SECTION_INDICES() + SHN__LAST__ +}; + +/* + * Section types. + */ + +#define _ELF_DEFINE_SECTION_TYPES() \ +_ELF_DEFINE_SHT(SHT_NULL, 0, "inactive header") \ +_ELF_DEFINE_SHT(SHT_PROGBITS, 1, "program defined information") \ +_ELF_DEFINE_SHT(SHT_SYMTAB, 2, "symbol table") \ +_ELF_DEFINE_SHT(SHT_STRTAB, 3, "string table") \ +_ELF_DEFINE_SHT(SHT_RELA, 4, \ + "relocation entries with addends") \ +_ELF_DEFINE_SHT(SHT_HASH, 5, "symbol hash table") \ +_ELF_DEFINE_SHT(SHT_DYNAMIC, 6, \ + "information for dynamic linking") \ +_ELF_DEFINE_SHT(SHT_NOTE, 7, "additional notes") \ +_ELF_DEFINE_SHT(SHT_NOBITS, 8, "section occupying no space") \ +_ELF_DEFINE_SHT(SHT_REL, 9, \ + "relocation entries without addends") \ +_ELF_DEFINE_SHT(SHT_SHLIB, 10, "reserved") \ +_ELF_DEFINE_SHT(SHT_DYNSYM, 11, "symbol table") \ +_ELF_DEFINE_SHT(SHT_INIT_ARRAY, 14, \ + "pointers to initialization functions") \ +_ELF_DEFINE_SHT(SHT_FINI_ARRAY, 15, \ + "pointers to termination functions") \ +_ELF_DEFINE_SHT(SHT_PREINIT_ARRAY, 16, \ + "pointers to functions called before initialization") \ +_ELF_DEFINE_SHT(SHT_GROUP, 17, "defines a section group") \ +_ELF_DEFINE_SHT(SHT_SYMTAB_SHNDX, 18, \ + "used for extended section numbering") \ +_ELF_DEFINE_SHT(SHT_LOOS, 0x60000000UL, \ + "start of OS-specific range") \ +_ELF_DEFINE_SHT(SHT_SUNW_dof, 0x6FFFFFF4UL, \ + "used by dtrace") \ +_ELF_DEFINE_SHT(SHT_SUNW_cap, 0x6FFFFFF5UL, \ + "capability requirements") \ +_ELF_DEFINE_SHT(SHT_GNU_ATTRIBUTES, 0x6FFFFFF5UL, \ + "object attributes") \ +_ELF_DEFINE_SHT(SHT_SUNW_SIGNATURE, 0x6FFFFFF6UL, \ + "module verification signature") \ +_ELF_DEFINE_SHT(SHT_GNU_HASH, 0x6FFFFFF6UL, \ + "GNU Hash sections") \ +_ELF_DEFINE_SHT(SHT_GNU_LIBLIST, 0x6FFFFFF7UL, \ + "List of libraries to be prelinked") \ +_ELF_DEFINE_SHT(SHT_SUNW_ANNOTATE, 0x6FFFFFF7UL, \ + "special section where unresolved references are allowed") \ +_ELF_DEFINE_SHT(SHT_SUNW_DEBUGSTR, 0x6FFFFFF8UL, \ + "debugging information") \ +_ELF_DEFINE_SHT(SHT_CHECKSUM, 0x6FFFFFF8UL, \ + "checksum for dynamic shared objects") \ +_ELF_DEFINE_SHT(SHT_SUNW_DEBUG, 0x6FFFFFF9UL, \ + "debugging information") \ +_ELF_DEFINE_SHT(SHT_SUNW_move, 0x6FFFFFFAUL, \ + "information to handle partially initialized symbols") \ +_ELF_DEFINE_SHT(SHT_SUNW_COMDAT, 0x6FFFFFFBUL, \ + "section supporting merging of multiple copies of data") \ +_ELF_DEFINE_SHT(SHT_SUNW_syminfo, 0x6FFFFFFCUL, \ + "additional symbol information") \ +_ELF_DEFINE_SHT(SHT_SUNW_verdef, 0x6FFFFFFDUL, \ + "symbol versioning information") \ +_ELF_DEFINE_SHT(SHT_SUNW_verneed, 0x6FFFFFFEUL, \ + "symbol versioning requirements") \ +_ELF_DEFINE_SHT(SHT_SUNW_versym, 0x6FFFFFFFUL, \ + "symbol versioning table") \ +_ELF_DEFINE_SHT(SHT_HIOS, 0x6FFFFFFFUL, \ + "end of OS-specific range") \ +_ELF_DEFINE_SHT(SHT_LOPROC, 0x70000000UL, \ + "start of processor-specific range") \ +_ELF_DEFINE_SHT(SHT_ARM_EXIDX, 0x70000001UL, \ + "exception index table") \ +_ELF_DEFINE_SHT(SHT_ARM_PREEMPTMAP, 0x70000002UL, \ + "BPABI DLL dynamic linking preemption map") \ +_ELF_DEFINE_SHT(SHT_ARM_ATTRIBUTES, 0x70000003UL, \ + "object file compatibility attributes") \ +_ELF_DEFINE_SHT(SHT_ARM_DEBUGOVERLAY, 0x70000004UL, \ + "overlay debug information") \ +_ELF_DEFINE_SHT(SHT_ARM_OVERLAYSECTION, 0x70000005UL, \ + "overlay debug information") \ +_ELF_DEFINE_SHT(SHT_MIPS_LIBLIST, 0x70000000UL, \ + "DSO library information used in link") \ +_ELF_DEFINE_SHT(SHT_MIPS_MSYM, 0x70000001UL, \ + "MIPS symbol table extension") \ +_ELF_DEFINE_SHT(SHT_MIPS_CONFLICT, 0x70000002UL, \ + "symbol conflicting with DSO-defined symbols ") \ +_ELF_DEFINE_SHT(SHT_MIPS_GPTAB, 0x70000003UL, \ + "global pointer table") \ +_ELF_DEFINE_SHT(SHT_MIPS_UCODE, 0x70000004UL, \ + "reserved") \ +_ELF_DEFINE_SHT(SHT_MIPS_DEBUG, 0x70000005UL, \ + "reserved (obsolete debug information)") \ +_ELF_DEFINE_SHT(SHT_MIPS_REGINFO, 0x70000006UL, \ + "register usage information") \ +_ELF_DEFINE_SHT(SHT_MIPS_PACKAGE, 0x70000007UL, \ + "OSF reserved") \ +_ELF_DEFINE_SHT(SHT_MIPS_PACKSYM, 0x70000008UL, \ + "OSF reserved") \ +_ELF_DEFINE_SHT(SHT_MIPS_RELD, 0x70000009UL, \ + "dynamic relocation") \ +_ELF_DEFINE_SHT(SHT_MIPS_IFACE, 0x7000000BUL, \ + "subprogram interface information") \ +_ELF_DEFINE_SHT(SHT_MIPS_CONTENT, 0x7000000CUL, \ + "section content classification") \ +_ELF_DEFINE_SHT(SHT_MIPS_OPTIONS, 0x7000000DUL, \ + "general options") \ +_ELF_DEFINE_SHT(SHT_MIPS_DELTASYM, 0x7000001BUL, \ + "Delta C++: symbol table") \ +_ELF_DEFINE_SHT(SHT_MIPS_DELTAINST, 0x7000001CUL, \ + "Delta C++: instance table") \ +_ELF_DEFINE_SHT(SHT_MIPS_DELTACLASS, 0x7000001DUL, \ + "Delta C++: class table") \ +_ELF_DEFINE_SHT(SHT_MIPS_DWARF, 0x7000001EUL, \ + "DWARF debug information") \ +_ELF_DEFINE_SHT(SHT_MIPS_DELTADECL, 0x7000001FUL, \ + "Delta C++: declarations") \ +_ELF_DEFINE_SHT(SHT_MIPS_SYMBOL_LIB, 0x70000020UL, \ + "symbol-to-library mapping") \ +_ELF_DEFINE_SHT(SHT_MIPS_EVENTS, 0x70000021UL, \ + "event locations") \ +_ELF_DEFINE_SHT(SHT_MIPS_TRANSLATE, 0x70000022UL, \ + "???") \ +_ELF_DEFINE_SHT(SHT_MIPS_PIXIE, 0x70000023UL, \ + "special pixie sections") \ +_ELF_DEFINE_SHT(SHT_MIPS_XLATE, 0x70000024UL, \ + "address translation table") \ +_ELF_DEFINE_SHT(SHT_MIPS_XLATE_DEBUG, 0x70000025UL, \ + "SGI internal address translation table") \ +_ELF_DEFINE_SHT(SHT_MIPS_WHIRL, 0x70000026UL, \ + "intermediate code") \ +_ELF_DEFINE_SHT(SHT_MIPS_EH_REGION, 0x70000027UL, \ + "C++ exception handling region info") \ +_ELF_DEFINE_SHT(SHT_MIPS_XLATE_OLD, 0x70000028UL, \ + "obsolete") \ +_ELF_DEFINE_SHT(SHT_MIPS_PDR_EXCEPTION, 0x70000029UL, \ + "runtime procedure descriptor table exception information") \ +_ELF_DEFINE_SHT(SHT_SPARC_GOTDATA, 0x70000000UL, \ + "SPARC-specific data") \ +_ELF_DEFINE_SHT(SHT_AMD64_UNWIND, 0x70000001UL, \ + "unwind tables for the AMD64") \ +_ELF_DEFINE_SHT(SHT_ORDERED, 0x7FFFFFFFUL, \ + "sort entries in the section") \ +_ELF_DEFINE_SHT(SHT_HIPROC, 0x7FFFFFFFUL, \ + "end of processor-specific range") \ +_ELF_DEFINE_SHT(SHT_LOUSER, 0x80000000UL, \ + "start of application-specific range") \ +_ELF_DEFINE_SHT(SHT_HIUSER, 0xFFFFFFFFUL, \ + "end of application-specific range") + +#undef _ELF_DEFINE_SHT +#define _ELF_DEFINE_SHT(N, V, DESCR) N = V , +enum { + _ELF_DEFINE_SECTION_TYPES() + SHT__LAST__ = SHT_HIUSER +}; + +/* Aliases for section types. */ +#ifndef SHT_GNU_verdef +#define SHT_GNU_verdef SHT_SUNW_verdef +#endif +#ifndef SHT_GNU_verneed +#define SHT_GNU_verneed SHT_SUNW_verneed +#endif +#ifndef SHT_GNU_versym +#define SHT_GNU_versym SHT_SUNW_versym +#endif + +/* + * Symbol binding information. + */ + +#define _ELF_DEFINE_SYMBOL_BINDING() \ +_ELF_DEFINE_STB(STB_LOCAL, 0, \ + "not visible outside defining object file") \ +_ELF_DEFINE_STB(STB_GLOBAL, 1, \ + "visible across all object files being combined") \ +_ELF_DEFINE_STB(STB_WEAK, 2, \ + "visible across all object files but with low precedence") \ +_ELF_DEFINE_STB(STB_LOOS, 10, "start of OS-specific range") \ +_ELF_DEFINE_STB(STB_HIOS, 12, "end of OS-specific range") \ +_ELF_DEFINE_STB(STB_LOPROC, 13, \ + "start of processor-specific range") \ +_ELF_DEFINE_STB(STB_HIPROC, 15, \ + "end of processor-specific range") + +#undef _ELF_DEFINE_STB +#define _ELF_DEFINE_STB(N, V, DESCR) N = V , +enum { + _ELF_DEFINE_SYMBOL_BINDING() + STB__LAST__ +}; + +/* + * Symbol types + */ + +#define _ELF_DEFINE_SYMBOL_TYPES() \ +_ELF_DEFINE_STT(STT_NOTYPE, 0, "unspecified type") \ +_ELF_DEFINE_STT(STT_OBJECT, 1, "data object") \ +_ELF_DEFINE_STT(STT_FUNC, 2, "executable code") \ +_ELF_DEFINE_STT(STT_SECTION, 3, "section") \ +_ELF_DEFINE_STT(STT_FILE, 4, "source file") \ +_ELF_DEFINE_STT(STT_COMMON, 5, "uninitialized common block") \ +_ELF_DEFINE_STT(STT_TLS, 6, "thread local storage") \ +_ELF_DEFINE_STT(STT_LOOS, 10, "start of OS-specific types") \ +_ELF_DEFINE_STT(STT_HIOS, 12, "end of OS-specific types") \ +_ELF_DEFINE_STT(STT_LOPROC, 13, \ + "start of processor-specific types") \ +_ELF_DEFINE_STT(STT_ARM_TFUNC, 13, "Thumb function (GNU)") \ +_ELF_DEFINE_STT(STT_ARM_16BIT, 15, "Thumb label (GNU)") \ +_ELF_DEFINE_STT(STT_HIPROC, 15, \ + "end of processor-specific types") + +#undef _ELF_DEFINE_STT +#define _ELF_DEFINE_STT(N, V, DESCR) N = V , +enum { + _ELF_DEFINE_SYMBOL_TYPES() + STT__LAST__ +}; + +/* + * Symbol binding. + */ + +#define _ELF_DEFINE_SYMBOL_BINDING_KINDS() \ +_ELF_DEFINE_SYB(SYMINFO_BT_SELF, 0xFFFFU, \ + "bound to self") \ +_ELF_DEFINE_SYB(SYMINFO_BT_PARENT, 0xFFFEU, \ + "bound to parent") \ +_ELF_DEFINE_SYB(SYMINFO_BT_NONE, 0xFFFDU, \ + "no special binding") + +#undef _ELF_DEFINE_SYB +#define _ELF_DEFINE_SYB(N, V, DESCR) N = V , +enum { + _ELF_DEFINE_SYMBOL_BINDING_KINDS() + SYMINFO__LAST__ +}; + +/* + * Symbol visibility. + */ + +#define _ELF_DEFINE_SYMBOL_VISIBILITY() \ +_ELF_DEFINE_STV(STV_DEFAULT, 0, \ + "as specified by symbol type") \ +_ELF_DEFINE_STV(STV_INTERNAL, 1, \ + "as defined by processor semantics") \ +_ELF_DEFINE_STV(STV_HIDDEN, 2, \ + "hidden from other components") \ +_ELF_DEFINE_STV(STV_PROTECTED, 3, \ + "local references are not preemptable") + +#undef _ELF_DEFINE_STV +#define _ELF_DEFINE_STV(N, V, DESCR) N = V , +enum { + _ELF_DEFINE_SYMBOL_VISIBILITY() + STV__LAST__ +}; + +/* + * Symbol flags. + */ +#define _ELF_DEFINE_SYMBOL_FLAGS() \ +_ELF_DEFINE_SYF(SYMINFO_FLG_DIRECT, 0x01, \ + "directly assocated reference") \ +_ELF_DEFINE_SYF(SYMINFO_FLG_COPY, 0x04, \ + "definition by copy-relocation") \ +_ELF_DEFINE_SYF(SYMINFO_FLG_LAZYLOAD, 0x08, \ + "object should be lazily loaded") \ +_ELF_DEFINE_SYF(SYMINFO_FLG_DIRECTBIND, 0x10, \ + "reference should be directly bound") \ +_ELF_DEFINE_SYF(SYMINFO_FLG_NOEXTDIRECT, 0x20, \ + "external references not allowed to bind to definition") + +#undef _ELF_DEFINE_SYF +#define _ELF_DEFINE_SYF(N, V, DESCR) N = V , +enum { + _ELF_DEFINE_SYMBOL_FLAGS() + SYMINFO_FLG__LAST__ +}; + +/* + * Version dependencies. + */ +#define _ELF_DEFINE_VERSIONING_DEPENDENCIES() \ +_ELF_DEFINE_VERD(VER_NDX_LOCAL, 0, "local scope") \ +_ELF_DEFINE_VERD(VER_NDX_GLOBAL, 1, "global scope") +#undef _ELF_DEFINE_VERD +#define _ELF_DEFINE_VERD(N, V, DESCR) N = V , +enum { + _ELF_DEFINE_VERSIONING_DEPENDENCIES() + VER_NDX__LAST__ +}; + +/* + * Version flags. + */ +#define _ELF_DEFINE_VERSIONING_FLAGS() \ +_ELF_DEFINE_VERF(VER_FLG_BASE, 0x1, "file version") \ +_ELF_DEFINE_VERF(VER_FLG_WEAK, 0x2, "weak version") +#undef _ELF_DEFINE_VERF +#define _ELF_DEFINE_VERF(N, V, DESCR) N = V , +enum { + _ELF_DEFINE_VERSIONING_FLAGS() + VER_FLG__LAST__ +}; + +/* + * Version needs + */ +#define _ELF_DEFINE_VERSIONING_NEEDS() \ +_ELF_DEFINE_VRN(VER_NEED_NONE, 0, "invalid version") \ +_ELF_DEFINE_VRN(VER_NEED_CURRENT, 1, "current version") +#undef _ELF_DEFINE_VRN +#define _ELF_DEFINE_VRN(N, V, DESCR) N = V , +enum { + _ELF_DEFINE_VERSIONING_NEEDS() + VER_NEED__LAST__ +}; + +/* + * Version numbers. + */ +#define _ELF_DEFINE_VERSIONING_NUMBERS() \ +_ELF_DEFINE_VRNU(VER_DEF_NONE, 0, "invalid version") \ +_ELF_DEFINE_VRNU(VER_DEF_CURRENT, 1, "current version") +#undef _ELF_DEFINE_VRNU +#define _ELF_DEFINE_VRNU(N, V, DESCR) N = V , +enum { + _ELF_DEFINE_VERSIONING_NUMBERS() + VER_DEF__LAST__ +}; + +/** + ** Relocation types. + **/ + +#define _ELF_DEFINE_386_RELOCATIONS() \ +_ELF_DEFINE_RELOC(R_386_NONE, 0) \ +_ELF_DEFINE_RELOC(R_386_32, 1) \ +_ELF_DEFINE_RELOC(R_386_PC32, 2) \ +_ELF_DEFINE_RELOC(R_386_GOT32, 3) \ +_ELF_DEFINE_RELOC(R_386_PLT32, 4) \ +_ELF_DEFINE_RELOC(R_386_COPY, 5) \ +_ELF_DEFINE_RELOC(R_386_GLOB_DAT, 6) \ +_ELF_DEFINE_RELOC(R_386_JMP_SLOT, 7) \ +_ELF_DEFINE_RELOC(R_386_RELATIVE, 8) \ +_ELF_DEFINE_RELOC(R_386_GOTOFF, 9) \ +_ELF_DEFINE_RELOC(R_386_GOTPC, 10) \ +_ELF_DEFINE_RELOC(R_386_32PLT, 11) \ +_ELF_DEFINE_RELOC(R_386_16, 20) \ +_ELF_DEFINE_RELOC(R_386_PC16, 21) \ +_ELF_DEFINE_RELOC(R_386_8, 22) \ +_ELF_DEFINE_RELOC(R_386_PC8, 23) + +/* + * These are the symbols used in the Sun ``Linkers and Loaders + * Guide'', Document No: 817-1984-17. See the X86_64 relocations list + * below for the spellings used in the ELF specification. + */ +#define _ELF_DEFINE_AMD64_RELOCATIONS() \ +_ELF_DEFINE_RELOC(R_AMD64_NONE, 0) \ +_ELF_DEFINE_RELOC(R_AMD64_64, 1) \ +_ELF_DEFINE_RELOC(R_AMD64_PC32, 2) \ +_ELF_DEFINE_RELOC(R_AMD64_GOT32, 3) \ +_ELF_DEFINE_RELOC(R_AMD64_PLT32, 4) \ +_ELF_DEFINE_RELOC(R_AMD64_COPY, 5) \ +_ELF_DEFINE_RELOC(R_AMD64_GLOB_DAT, 6) \ +_ELF_DEFINE_RELOC(R_AMD64_JUMP_SLOT, 7) \ +_ELF_DEFINE_RELOC(R_AMD64_RELATIVE, 8) \ +_ELF_DEFINE_RELOC(R_AMD64_GOTPCREL, 9) \ +_ELF_DEFINE_RELOC(R_AMD64_32, 10) \ +_ELF_DEFINE_RELOC(R_AMD64_32S, 11) \ +_ELF_DEFINE_RELOC(R_AMD64_16, 12) \ +_ELF_DEFINE_RELOC(R_AMD64_PC16, 13) \ +_ELF_DEFINE_RELOC(R_AMD64_8, 14) \ +_ELF_DEFINE_RELOC(R_AMD64_PC8, 15) \ +_ELF_DEFINE_RELOC(R_AMD64_PC64, 24) \ +_ELF_DEFINE_RELOC(R_AMD64_GOTOFF64, 25) \ +_ELF_DEFINE_RELOC(R_AMD64_GOTPC32, 26) + +#define _ELF_DEFINE_ARM_RELOCATIONS() \ +_ELF_DEFINE_RELOC(R_ARM_NONE, 0) \ +_ELF_DEFINE_RELOC(R_ARM_PC24, 1) \ +_ELF_DEFINE_RELOC(R_ARM_ABS32, 2) \ +_ELF_DEFINE_RELOC(R_ARM_REL32, 3) \ +_ELF_DEFINE_RELOC(R_ARM_LDR_PC_G0, 4) \ +_ELF_DEFINE_RELOC(R_ARM_ABS16, 5) \ +_ELF_DEFINE_RELOC(R_ARM_ABS12, 6) \ +_ELF_DEFINE_RELOC(R_ARM_THM_ABS5, 7) \ +_ELF_DEFINE_RELOC(R_ARM_ABS8, 8) \ +_ELF_DEFINE_RELOC(R_ARM_SBREL32, 9) \ +_ELF_DEFINE_RELOC(R_ARM_THM_CALL, 10) \ +_ELF_DEFINE_RELOC(R_ARM_THM_PC8, 11) \ +_ELF_DEFINE_RELOC(R_ARM_BREL_ADJ, 12) \ +_ELF_DEFINE_RELOC(R_ARM_SWI24, 13) \ +_ELF_DEFINE_RELOC(R_ARM_THM_SWI8, 14) \ +_ELF_DEFINE_RELOC(R_ARM_XPC25, 15) \ +_ELF_DEFINE_RELOC(R_ARM_THM_XPC22, 16) \ +_ELF_DEFINE_RELOC(R_ARM_TLS_DTPMOD32, 17) \ +_ELF_DEFINE_RELOC(R_ARM_TLS_DTPOFF32, 18) \ +_ELF_DEFINE_RELOC(R_ARM_TLS_TPOFF32, 19) \ +_ELF_DEFINE_RELOC(R_ARM_COPY, 20) \ +_ELF_DEFINE_RELOC(R_ARM_GLOB_DAT, 21) \ +_ELF_DEFINE_RELOC(R_ARM_JUMP_SLOT, 22) \ +_ELF_DEFINE_RELOC(R_ARM_RELATIVE, 23) \ +_ELF_DEFINE_RELOC(R_ARM_GOTOFF32, 24) \ +_ELF_DEFINE_RELOC(R_ARM_BASE_PREL, 25) \ +_ELF_DEFINE_RELOC(R_ARM_GOT_BREL, 26) \ +_ELF_DEFINE_RELOC(R_ARM_PLT32, 27) \ +_ELF_DEFINE_RELOC(R_ARM_CALL, 28) \ +_ELF_DEFINE_RELOC(R_ARM_JUMP24, 29) \ +_ELF_DEFINE_RELOC(R_ARM_THM_JUMP24, 30) \ +_ELF_DEFINE_RELOC(R_ARM_BASE_ABS, 31) \ +_ELF_DEFINE_RELOC(R_ARM_ALU_PCREL7_0, 32) \ +_ELF_DEFINE_RELOC(R_ARM_ALU_PCREL15_8, 33) \ +_ELF_DEFINE_RELOC(R_ARM_ALU_PCREL23_15, 34) \ +_ELF_DEFINE_RELOC(R_ARM_LDR_SBREL_11_0, 35) \ +_ELF_DEFINE_RELOC(R_ARM_ALU_SBREL_19_12, 36) \ +_ELF_DEFINE_RELOC(R_ARM_ALU_SBREL_27_20, 37) \ +_ELF_DEFINE_RELOC(R_ARM_TARGET1, 38) \ +_ELF_DEFINE_RELOC(R_ARM_SBREL31, 39) \ +_ELF_DEFINE_RELOC(R_ARM_V4BX, 40) \ +_ELF_DEFINE_RELOC(R_ARM_TARGET2, 41) \ +_ELF_DEFINE_RELOC(R_ARM_PREL31, 42) \ +_ELF_DEFINE_RELOC(R_ARM_MOVW_ABS_NC, 43) \ +_ELF_DEFINE_RELOC(R_ARM_MOVT_ABS, 44) \ +_ELF_DEFINE_RELOC(R_ARM_MOVW_PREL_NC, 45) \ +_ELF_DEFINE_RELOC(R_ARM_MOVT_PREL, 46) \ +_ELF_DEFINE_RELOC(R_ARM_THM_MOVW_ABS_NC, 47) \ +_ELF_DEFINE_RELOC(R_ARM_THM_MOVT_ABS, 48) \ +_ELF_DEFINE_RELOC(R_ARM_MOVW_PREL_NC, 49) \ +_ELF_DEFINE_RELOC(R_ARM_THM_MOVT_PREL, 50) \ +_ELF_DEFINE_RELOC(R_ARM_THM_JUMP19, 51) \ +_ELF_DEFINE_RELOC(R_ARM_THM_JUMP6, 52) \ +_ELF_DEFINE_RELOC(R_ARM_THM_ALU_PREL_11_0, 53) \ +_ELF_DEFINE_RELOC(R_ARM_THM_PC12, 54) \ +_ELF_DEFINE_RELOC(R_ARM_ABS32_NOI, 55) \ +_ELF_DEFINE_RELOC(R_ARM_REL32_NOI, 56) \ +_ELF_DEFINE_RELOC(R_ARM_ALU_PC_G0_NC, 57) \ +_ELF_DEFINE_RELOC(R_ARM_ALU_PC_G0, 58) \ +_ELF_DEFINE_RELOC(R_ARM_ALU_PC_G1_NC, 59) \ +_ELF_DEFINE_RELOC(R_ARM_ALU_PC_G1, 60) \ +_ELF_DEFINE_RELOC(R_ARM_ALU_PC_G2, 61) \ +_ELF_DEFINE_RELOC(R_ARM_LDR_PC_G1, 62) \ +_ELF_DEFINE_RELOC(R_ARM_LDR_PC_G2, 63) \ +_ELF_DEFINE_RELOC(R_ARM_LDRS_PC_G0, 64) \ +_ELF_DEFINE_RELOC(R_ARM_LDRS_PC_G1, 65) \ +_ELF_DEFINE_RELOC(R_ARM_LDRS_PC_G2, 66) \ +_ELF_DEFINE_RELOC(R_ARM_LDC_PC_G0, 67) \ +_ELF_DEFINE_RELOC(R_ARM_LDC_PC_G1, 68) \ +_ELF_DEFINE_RELOC(R_ARM_LDC_PC_G2, 69) \ +_ELF_DEFINE_RELOC(R_ARM_ALU_SB_G0_NC, 70) \ +_ELF_DEFINE_RELOC(R_ARM_ALU_SB_G0, 71) \ +_ELF_DEFINE_RELOC(R_ARM_ALU_SB_G1_NC, 72) \ +_ELF_DEFINE_RELOC(R_ARM_ALU_SB_G1, 73) \ +_ELF_DEFINE_RELOC(R_ARM_ALU_SB_G2, 74) \ +_ELF_DEFINE_RELOC(R_ARM_LDR_SB_G0, 75) \ +_ELF_DEFINE_RELOC(R_ARM_LDR_SB_G1, 76) \ +_ELF_DEFINE_RELOC(R_ARM_LDR_SB_G2, 77) \ +_ELF_DEFINE_RELOC(R_ARM_LDRS_SB_G0, 78) \ +_ELF_DEFINE_RELOC(R_ARM_LDRS_SB_G1, 79) \ +_ELF_DEFINE_RELOC(R_ARM_LDRS_SB_G2, 80) \ +_ELF_DEFINE_RELOC(R_ARM_LDC_SB_G0, 81) \ +_ELF_DEFINE_RELOC(R_ARM_LDC_SB_G1, 82) \ +_ELF_DEFINE_RELOC(R_ARM_LDC_SB_G2, 83) \ +_ELF_DEFINE_RELOC(R_ARM_MOVW_BREL_NC, 84) \ +_ELF_DEFINE_RELOC(R_ARM_MOVT_BREL, 85) \ +_ELF_DEFINE_RELOC(R_ARM_MOVW_BREL, 86) \ +_ELF_DEFINE_RELOC(R_ARM_THM_MOVW_BREL_NC, 87) \ +_ELF_DEFINE_RELOC(R_ARM_THM_MOVT_BREL, 88) \ +_ELF_DEFINE_RELOC(R_ARM_THM_MOVW_BREL, 89) \ +_ELF_DEFINE_RELOC(R_ARM_TLS_GOTDESC, 90) \ +_ELF_DEFINE_RELOC(R_ARM_TLS_CALL, 91) \ +_ELF_DEFINE_RELOC(R_ARM_TLS_DESCSEQ, 92) \ +_ELF_DEFINE_RELOC(R_ARM_THM_TLS_CALL, 93) \ +_ELF_DEFINE_RELOC(R_ARM_PLT32_ABS, 94) \ +_ELF_DEFINE_RELOC(R_ARM_GOT_ABS, 95) \ +_ELF_DEFINE_RELOC(R_ARM_GOT_PREL, 96) \ +_ELF_DEFINE_RELOC(R_ARM_GOT_BREL12, 97) \ +_ELF_DEFINE_RELOC(R_ARM_GOTOFF12, 98) \ +_ELF_DEFINE_RELOC(R_ARM_GOTRELAX, 99) \ +_ELF_DEFINE_RELOC(R_ARM_GNU_VTENTRY, 100) \ +_ELF_DEFINE_RELOC(R_ARM_GNU_VTINHERIT, 101) \ +_ELF_DEFINE_RELOC(R_ARM_THM_JUMP11, 102) \ +_ELF_DEFINE_RELOC(R_ARM_THM_JUMP8, 103) \ +_ELF_DEFINE_RELOC(R_ARM_TLS_GD32, 104) \ +_ELF_DEFINE_RELOC(R_ARM_TLS_LDM32, 105) \ +_ELF_DEFINE_RELOC(R_ARM_TLS_LDO32, 106) \ +_ELF_DEFINE_RELOC(R_ARM_TLS_IE32, 107) \ +_ELF_DEFINE_RELOC(R_ARM_TLS_LE32, 108) \ +_ELF_DEFINE_RELOC(R_ARM_TLS_LDO12, 109) \ +_ELF_DEFINE_RELOC(R_ARM_TLS_LE12, 110) \ +_ELF_DEFINE_RELOC(R_ARM_TLS_IE12GP, 111) \ +_ELF_DEFINE_RELOC(R_ARM_ME_TOO, 128) \ +_ELF_DEFINE_RELOC(R_ARM_THM_TLS_DESCSEQ16, 129) \ +_ELF_DEFINE_RELOC(R_ARM_THM_TLS_DESCSEQ32, 130) + +#define _ELF_DEFINE_IA64_RELOCATIONS() \ +_ELF_DEFINE_RELOC(R_IA_64_NONE, 0) \ +_ELF_DEFINE_RELOC(R_IA_64_IMM14, 0x21) \ +_ELF_DEFINE_RELOC(R_IA_64_IMM22, 0x22) \ +_ELF_DEFINE_RELOC(R_IA_64_IMM64, 0x23) \ +_ELF_DEFINE_RELOC(R_IA_64_DIR32MSB, 0x24) \ +_ELF_DEFINE_RELOC(R_IA_64_DIR32LSB, 0x25) \ +_ELF_DEFINE_RELOC(R_IA_64_DIR64MSB, 0x26) \ +_ELF_DEFINE_RELOC(R_IA_64_DIR64LSB, 0x27) \ +_ELF_DEFINE_RELOC(R_IA_64_GPREL22, 0x2a) \ +_ELF_DEFINE_RELOC(R_IA_64_GPREL64I, 0x2b) \ +_ELF_DEFINE_RELOC(R_IA_64_GPREL32MSB, 0x2c) \ +_ELF_DEFINE_RELOC(R_IA_64_GPREL32LSB, 0x2d) \ +_ELF_DEFINE_RELOC(R_IA_64_GPREL64MSB, 0x2e) \ +_ELF_DEFINE_RELOC(R_IA_64_GPREL64LSB, 0x2f) \ +_ELF_DEFINE_RELOC(R_IA_64_LTOFF22, 0x32) \ +_ELF_DEFINE_RELOC(R_IA_64_LTOFF64I, 0x33) \ +_ELF_DEFINE_RELOC(R_IA_64_PLTOFF22, 0x3a) \ +_ELF_DEFINE_RELOC(R_IA_64_PLTOFF64I, 0x3b) \ +_ELF_DEFINE_RELOC(R_IA_64_PLTOFF64MSB, 0x3e) \ +_ELF_DEFINE_RELOC(R_IA_64_PLTOFF64LSB, 0x3f) \ +_ELF_DEFINE_RELOC(R_IA_64_FPTR64I, 0x43) \ +_ELF_DEFINE_RELOC(R_IA_64_FPTR32MSB, 0x44) \ +_ELF_DEFINE_RELOC(R_IA_64_FPTR32LSB, 0x45) \ +_ELF_DEFINE_RELOC(R_IA_64_FPTR64MSB, 0x46) \ +_ELF_DEFINE_RELOC(R_IA_64_FPTR64LSB, 0x47) \ +_ELF_DEFINE_RELOC(R_IA_64_PCREL60B, 0x48) \ +_ELF_DEFINE_RELOC(R_IA_64_PCREL21B, 0x49) \ +_ELF_DEFINE_RELOC(R_IA_64_PCREL21M, 0x4a) \ +_ELF_DEFINE_RELOC(R_IA_64_PCREL21F, 0x4b) \ +_ELF_DEFINE_RELOC(R_IA_64_PCREL32MSB, 0x4c) \ +_ELF_DEFINE_RELOC(R_IA_64_PCREL32LSB, 0x4d) \ +_ELF_DEFINE_RELOC(R_IA_64_PCREL64MSB, 0x4e) \ +_ELF_DEFINE_RELOC(R_IA_64_PCREL64LSB, 0x4f) \ +_ELF_DEFINE_RELOC(R_IA_64_LTOFF_FPTR22, 0x52) \ +_ELF_DEFINE_RELOC(R_IA_64_LTOFF_FPTR64I, 0x53) \ +_ELF_DEFINE_RELOC(R_IA_64_LTOFF_FPTR32MSB, 0x54) \ +_ELF_DEFINE_RELOC(R_IA_64_LTOFF_FPTR32LSB, 0x55) \ +_ELF_DEFINE_RELOC(R_IA_64_LTOFF_FPTR64MSB, 0x56) \ +_ELF_DEFINE_RELOC(R_IA_64_LTOFF_FPTR64LSB, 0x57) \ +_ELF_DEFINE_RELOC(R_IA_64_SEGREL32MSB, 0x5c) \ +_ELF_DEFINE_RELOC(R_IA_64_SEGREL32LSB, 0x5d) \ +_ELF_DEFINE_RELOC(R_IA_64_SEGREL64MSB, 0x5e) \ +_ELF_DEFINE_RELOC(R_IA_64_SEGREL64LSB, 0x5f) \ +_ELF_DEFINE_RELOC(R_IA_64_SECREL32MSB, 0x64) \ +_ELF_DEFINE_RELOC(R_IA_64_SECREL32LSB, 0x65) \ +_ELF_DEFINE_RELOC(R_IA_64_SECREL64MSB, 0x66) \ +_ELF_DEFINE_RELOC(R_IA_64_SECREL64LSB, 0x67) \ +_ELF_DEFINE_RELOC(R_IA_64_REL32MSB, 0x6c) \ +_ELF_DEFINE_RELOC(R_IA_64_REL32LSB, 0x6d) \ +_ELF_DEFINE_RELOC(R_IA_64_REL64MSB, 0x6e) \ +_ELF_DEFINE_RELOC(R_IA_64_REL64LSB, 0x6f) \ +_ELF_DEFINE_RELOC(R_IA_64_LTV32MSB, 0x74) \ +_ELF_DEFINE_RELOC(R_IA_64_LTV32LSB, 0x75) \ +_ELF_DEFINE_RELOC(R_IA_64_LTV64MSB, 0x76) \ +_ELF_DEFINE_RELOC(R_IA_64_LTV64LSB, 0x77) \ +_ELF_DEFINE_RELOC(R_IA_64_PCREL21BIa, 0x79) \ +_ELF_DEFINE_RELOC(R_IA_64_PCREL22, 0x7A) \ +_ELF_DEFINE_RELOC(R_IA_64_PCREL64I, 0x7B) \ +_ELF_DEFINE_RELOC(R_IA_64_IPLTMSB, 0x80) \ +_ELF_DEFINE_RELOC(R_IA_64_IPLTLSB, 0x81) \ +_ELF_DEFINE_RELOC(R_IA_64_SUB, 0x85) \ +_ELF_DEFINE_RELOC(R_IA_64_LTOFF22X, 0x86) \ +_ELF_DEFINE_RELOC(R_IA_64_LDXMOV, 0x87) \ +_ELF_DEFINE_RELOC(R_IA_64_TPREL14, 0x91) \ +_ELF_DEFINE_RELOC(R_IA_64_TPREL22, 0x92) \ +_ELF_DEFINE_RELOC(R_IA_64_TPREL64I, 0x93) \ +_ELF_DEFINE_RELOC(R_IA_64_TPREL64MSB, 0x96) \ +_ELF_DEFINE_RELOC(R_IA_64_TPREL64LSB, 0x97) \ +_ELF_DEFINE_RELOC(R_IA_64_LTOFF_TPREL22, 0x9A) \ +_ELF_DEFINE_RELOC(R_IA_64_DTPMOD64MSB, 0xA6) \ +_ELF_DEFINE_RELOC(R_IA_64_DTPMOD64LSB, 0xA7) \ +_ELF_DEFINE_RELOC(R_IA_64_LTOFF_DTPMOD22, 0xAA) \ +_ELF_DEFINE_RELOC(R_IA_64_DTPREL14, 0xB1) \ +_ELF_DEFINE_RELOC(R_IA_64_DTPREL22, 0xB2) \ +_ELF_DEFINE_RELOC(R_IA_64_DTPREL64I, 0xB3) \ +_ELF_DEFINE_RELOC(R_IA_64_DTPREL32MSB, 0xB4) \ +_ELF_DEFINE_RELOC(R_IA_64_DTPREL32LSB, 0xB5) \ +_ELF_DEFINE_RELOC(R_IA_64_DTPREL64MSB, 0xB6) \ +_ELF_DEFINE_RELOC(R_IA_64_DTPREL64LSB, 0xB7) \ +_ELF_DEFINE_RELOC(R_IA_64_LTOFF_DTPREL22, 0xBA) + +#define _ELF_DEFINE_MIPS_RELOCATIONS() \ +_ELF_DEFINE_RELOC(R_MIPS_NONE, 0) \ +_ELF_DEFINE_RELOC(R_MIPS_16, 1) \ +_ELF_DEFINE_RELOC(R_MIPS_32, 2) \ +_ELF_DEFINE_RELOC(R_MIPS_REL32, 3) \ +_ELF_DEFINE_RELOC(R_MIPS_26, 4) \ +_ELF_DEFINE_RELOC(R_MIPS_HI16, 5) \ +_ELF_DEFINE_RELOC(R_MIPS_LO16, 6) \ +_ELF_DEFINE_RELOC(R_MIPS_GPREL16, 7) \ +_ELF_DEFINE_RELOC(R_MIPS_LITERAL, 8) \ +_ELF_DEFINE_RELOC(R_MIPS_GOT16, 9) \ +_ELF_DEFINE_RELOC(R_MIPS_PC16, 10) \ +_ELF_DEFINE_RELOC(R_MIPS_CALL16, 11) \ +_ELF_DEFINE_RELOC(R_MIPS_GPREL32, 12) \ +_ELF_DEFINE_RELOC(R_MIPS_64, 18) \ +_ELF_DEFINE_RELOC(R_MIPS_GOTHI16, 21) \ +_ELF_DEFINE_RELOC(R_MIPS_GOTLO16, 22) \ +_ELF_DEFINE_RELOC(R_MIPS_CALLHI16, 30) \ +_ELF_DEFINE_RELOC(R_MIPS_CALLLO16, 31) + +#define _ELF_DEFINE_PPC32_RELOCATIONS() \ +_ELF_DEFINE_RELOC(R_PPC_NONE, 0) \ +_ELF_DEFINE_RELOC(R_PPC_ADDR32, 1) \ +_ELF_DEFINE_RELOC(R_PPC_ADDR24, 2) \ +_ELF_DEFINE_RELOC(R_PPC_ADDR16, 3) \ +_ELF_DEFINE_RELOC(R_PPC_ADDR16_LO, 4) \ +_ELF_DEFINE_RELOC(R_PPC_ADDR16_HI, 5) \ +_ELF_DEFINE_RELOC(R_PPC_ADDR16_HA, 6) \ +_ELF_DEFINE_RELOC(R_PPC_ADDR14, 7) \ +_ELF_DEFINE_RELOC(R_PPC_ADDR14_BRTAKEN, 8) \ +_ELF_DEFINE_RELOC(R_PPC_ADDR14_BRNTAKEN, 9) \ +_ELF_DEFINE_RELOC(R_PPC_REL24, 10) \ +_ELF_DEFINE_RELOC(R_PPC_REL14, 11) \ +_ELF_DEFINE_RELOC(R_PPC_REL14_BRTAKEN, 12) \ +_ELF_DEFINE_RELOC(R_PPC_REL14_BRNTAKEN, 13) \ +_ELF_DEFINE_RELOC(R_PPC_GOT16, 14) \ +_ELF_DEFINE_RELOC(R_PPC_GOT16_LO, 15) \ +_ELF_DEFINE_RELOC(R_PPC_GOT16_HI, 16) \ +_ELF_DEFINE_RELOC(R_PPC_GOT16_HA, 17) \ +_ELF_DEFINE_RELOC(R_PPC_PLTREL24, 18) \ +_ELF_DEFINE_RELOC(R_PPC_COPY, 19) \ +_ELF_DEFINE_RELOC(R_PPC_GLOB_DAT, 20) \ +_ELF_DEFINE_RELOC(R_PPC_JMP_SLOT, 21) \ +_ELF_DEFINE_RELOC(R_PPC_RELATIVE, 22) \ +_ELF_DEFINE_RELOC(R_PPC_LOCAL24PC, 23) \ +_ELF_DEFINE_RELOC(R_PPC_UADDR32, 24) \ +_ELF_DEFINE_RELOC(R_PPC_UADDR16, 25) \ +_ELF_DEFINE_RELOC(R_PPC_REL32, 26) \ +_ELF_DEFINE_RELOC(R_PPC_PLT32, 27) \ +_ELF_DEFINE_RELOC(R_PPC_PLTREL32, 28) \ +_ELF_DEFINE_RELOC(R_PPC_PLT16_LO, 29) \ +_ELF_DEFINE_RELOC(R_PPL_PLT16_HI, 30) \ +_ELF_DEFINE_RELOC(R_PPC_PLT16_HA, 31) \ +_ELF_DEFINE_RELOC(R_PPC_SDAREL16, 32) \ +_ELF_DEFINE_RELOC(R_PPC_SECTOFF, 33) \ +_ELF_DEFINE_RELOC(R_PPC_SECTOFF_LO, 34) \ +_ELF_DEFINE_RELOC(R_PPC_SECTOFF_HI, 35) \ +_ELF_DEFINE_RELOC(R_PPC_SECTOFF_HA, 36) \ +_ELF_DEFINE_RELOC(R_PPC_ADDR30, 37) \ +_ELF_DEFINE_RELOC(R_PPC_TLS, 67) \ +_ELF_DEFINE_RELOC(R_PPC_DTPMOD32, 68) \ +_ELF_DEFINE_RELOC(R_PPC_TPREL16, 69) \ +_ELF_DEFINE_RELOC(R_PPC_TPREL16_LO, 70) \ +_ELF_DEFINE_RELOC(R_PPC_TPREL16_HI, 71) \ +_ELF_DEFINE_RELOC(R_PPC_TPREL16_HA, 72) \ +_ELF_DEFINE_RELOC(R_PPC_TPREL32, 73) \ +_ELF_DEFINE_RELOC(R_PPC_DTPREL16, 74) \ +_ELF_DEFINE_RELOC(R_PPC_DTPREL16_LO, 75) \ +_ELF_DEFINE_RELOC(R_PPC_DTPREL16_HI, 76) \ +_ELF_DEFINE_RELOC(R_PPC_DTPREL16_HA, 77) \ +_ELF_DEFINE_RELOC(R_PPC_DTPREL32, 78) \ +_ELF_DEFINE_RELOC(R_PPC_GOT_TLSGD16, 79) \ +_ELF_DEFINE_RELOC(R_PPC_GOT_TLSGD16_LO, 80) \ +_ELF_DEFINE_RELOC(R_PPC_GOT_TLSGD16_HI, 81) \ +_ELF_DEFINE_RELOC(R_PPC_GOT_TLSGD16_HA, 82) \ +_ELF_DEFINE_RELOC(R_PPC_GOT_TLSLD16, 83) \ +_ELF_DEFINE_RELOC(R_PPC_GOT_TLSLD16_LO, 84) \ +_ELF_DEFINE_RELOC(R_PPC_GOT_TLSLD16_HI, 85) \ +_ELF_DEFINE_RELOC(R_PPC_GOT_TLSLD16_HA, 86) \ +_ELF_DEFINE_RELOC(R_PPC_GOT_TPREL16, 87) \ +_ELF_DEFINE_RELOC(R_PPC_GOT_TPREL16_LO, 88) \ +_ELF_DEFINE_RELOC(R_PPC_GOT_TPREL16_HI, 89) \ +_ELF_DEFINE_RELOC(R_PPC_GOT_TPREL16_HA, 90) \ +_ELF_DEFINE_RELOC(R_PPC_GOT_DTPREL16, 91) \ +_ELF_DEFINE_RELOC(R_PPC_GOT_DTPREL16_LO, 92) \ +_ELF_DEFINE_RELOC(R_PPC_GOT_DTPREL16_HI, 93) \ +_ELF_DEFINE_RELOC(R_PPC_GOT_DTPREL16_HA, 94) \ +_ELF_DEFINE_RELOC(R_PPC_TLSGD, 95) \ +_ELF_DEFINE_RELOC(R_PPC_TLSLD, 96) \ +_ELF_DEFINE_RELOC(R_PPC_EMB_NADDR32, 101) \ +_ELF_DEFINE_RELOC(R_PPC_EMB_NADDR16, 102) \ +_ELF_DEFINE_RELOC(R_PPC_EMB_NADDR16_LO, 103) \ +_ELF_DEFINE_RELOC(R_PPC_EMB_NADDR16_HI, 104) \ +_ELF_DEFINE_RELOC(R_PPC_EMB_NADDR16_HA, 105) \ +_ELF_DEFINE_RELOC(R_PPC_EMB_SDAI16, 106) \ +_ELF_DEFINE_RELOC(R_PPC_EMB_SDA2I16, 107) \ +_ELF_DEFINE_RELOC(R_PPC_EMB_SDA2REL, 108) \ +_ELF_DEFINE_RELOC(R_PPC_EMB_SDA21, 109) \ +_ELF_DEFINE_RELOC(R_PPC_EMB_MRKREF, 110) \ +_ELF_DEFINE_RELOC(R_PPC_EMB_RELSEC16, 111) \ +_ELF_DEFINE_RELOC(R_PPC_EMB_RELST_LO, 112) \ +_ELF_DEFINE_RELOC(R_PPC_EMB_RELST_HI, 113) \ +_ELF_DEFINE_RELOC(R_PPC_EMB_RELST_HA, 114) \ +_ELF_DEFINE_RELOC(R_PPC_EMB_BIT_FLD, 115) \ +_ELF_DEFINE_RELOC(R_PPC_EMB_RELSDA, 116) \ + +#define _ELF_DEFINE_PPC64_RELOCATIONS() \ +_ELF_DEFINE_RELOC(R_PPC64_NONE, 0) \ +_ELF_DEFINE_RELOC(R_PPC64_ADDR32, 1) \ +_ELF_DEFINE_RELOC(R_PPC64_ADDR24, 2) \ +_ELF_DEFINE_RELOC(R_PPC64_ADDR16, 3) \ +_ELF_DEFINE_RELOC(R_PPC64_ADDR16_LO, 4) \ +_ELF_DEFINE_RELOC(R_PPC64_ADDR16_HI, 5) \ +_ELF_DEFINE_RELOC(R_PPC64_ADDR16_HA, 6) \ +_ELF_DEFINE_RELOC(R_PPC64_ADDR14, 7) \ +_ELF_DEFINE_RELOC(R_PPC64_ADDR14_BRTAKEN, 8) \ +_ELF_DEFINE_RELOC(R_PPC64_ADDR14_BRNTAKEN, 9) \ +_ELF_DEFINE_RELOC(R_PPC64_REL24, 10) \ +_ELF_DEFINE_RELOC(R_PPC64_REL14, 11) \ +_ELF_DEFINE_RELOC(R_PPC64_REL14_BRTAKEN, 12) \ +_ELF_DEFINE_RELOC(R_PPC64_REL14_BRNTAKEN, 13) \ +_ELF_DEFINE_RELOC(R_PPC64_GOT16, 14) \ +_ELF_DEFINE_RELOC(R_PPC64_GOT16_LO, 15) \ +_ELF_DEFINE_RELOC(R_PPC64_GOT16_HI, 16) \ +_ELF_DEFINE_RELOC(R_PPC64_GOT16_HA, 17) \ +_ELF_DEFINE_RELOC(R_PPC64_COPY, 19) \ +_ELF_DEFINE_RELOC(R_PPC64_GLOB_DAT, 20) \ +_ELF_DEFINE_RELOC(R_PPC64_JMP_SLOT, 21) \ +_ELF_DEFINE_RELOC(R_PPC64_RELATIVE, 22) \ +_ELF_DEFINE_RELOC(R_PPC64_UADDR32, 24) \ +_ELF_DEFINE_RELOC(R_PPC64_UADDR16, 25) \ +_ELF_DEFINE_RELOC(R_PPC64_REL32, 26) \ +_ELF_DEFINE_RELOC(R_PPC64_PLT32, 27) \ +_ELF_DEFINE_RELOC(R_PPC64_PLTREL32, 28) \ +_ELF_DEFINE_RELOC(R_PPC64_PLT16_LO, 29) \ +_ELF_DEFINE_RELOC(R_PPC64_PLT16_HI, 30) \ +_ELF_DEFINE_RELOC(R_PPC64_PLT16_HA, 31) \ +_ELF_DEFINE_RELOC(R_PPC64_SECTOFF, 33) \ +_ELF_DEFINE_RELOC(R_PPC64_SECTOFF_LO, 34) \ +_ELF_DEFINE_RELOC(R_PPC64_SECTOFF_HI, 35) \ +_ELF_DEFINE_RELOC(R_PPC64_SECTOFF_HA, 36) \ +_ELF_DEFINE_RELOC(R_PPC64_ADDR30, 37) \ +_ELF_DEFINE_RELOC(R_PPC64_ADDR64, 38) \ +_ELF_DEFINE_RELOC(R_PPC64_ADDR16_HIGHER, 39) \ +_ELF_DEFINE_RELOC(R_PPC64_ADDR16_HIGHERA, 40) \ +_ELF_DEFINE_RELOC(R_PPC64_ADDR16_HIGHEST, 41) \ +_ELF_DEFINE_RELOC(R_PPC64_ADDR16_HIGHESTA, 42) \ +_ELF_DEFINE_RELOC(R_PPC64_UADDR64, 43) \ +_ELF_DEFINE_RELOC(R_PPC64_REL64, 44) \ +_ELF_DEFINE_RELOC(R_PPC64_PLT64, 45) \ +_ELF_DEFINE_RELOC(R_PPC64_PLTREL64, 46) \ +_ELF_DEFINE_RELOC(R_PPC64_TOC16, 47) \ +_ELF_DEFINE_RELOC(R_PPC64_TOC16_LO, 48) \ +_ELF_DEFINE_RELOC(R_PPC64_TOC16_HI, 49) \ +_ELF_DEFINE_RELOC(R_PPC64_TOC16_HA, 50) \ +_ELF_DEFINE_RELOC(R_PPC64_TOC, 51) \ +_ELF_DEFINE_RELOC(R_PPC64_PLTGOT16, 52) \ +_ELF_DEFINE_RELOC(R_PPC64_PLTGOT16_LO, 53) \ +_ELF_DEFINE_RELOC(R_PPC64_PLTGOT16_HI, 54) \ +_ELF_DEFINE_RELOC(R_PPC64_PLTGOT16_HA, 55) \ +_ELF_DEFINE_RELOC(R_PPC64_ADDR16_DS, 56) \ +_ELF_DEFINE_RELOC(R_PPC64_ADDR16_LO_DS, 57) \ +_ELF_DEFINE_RELOC(R_PPC64_GOT16_DS, 58) \ +_ELF_DEFINE_RELOC(R_PPC64_GOT16_LO_DS, 59) \ +_ELF_DEFINE_RELOC(R_PPC64_PLT16_LO_DS, 60) \ +_ELF_DEFINE_RELOC(R_PPC64_SECTOFF_DS, 61) \ +_ELF_DEFINE_RELOC(R_PPC64_SECTOFF_LO_DS, 62) \ +_ELF_DEFINE_RELOC(R_PPC64_TOC16_DS, 63) \ +_ELF_DEFINE_RELOC(R_PPC64_TOC16_LO_DS, 64) \ +_ELF_DEFINE_RELOC(R_PPC64_PLTGOT16_DS, 65) \ +_ELF_DEFINE_RELOC(R_PPC64_PLTGOT16_LO_DS, 66) \ +_ELF_DEFINE_RELOC(R_PPC64_TLS, 67) \ +_ELF_DEFINE_RELOC(R_PPC64_DTPMOD64, 68) \ +_ELF_DEFINE_RELOC(R_PPC64_TPREL16, 69) \ +_ELF_DEFINE_RELOC(R_PPC64_TPREL16_LO, 60) \ +_ELF_DEFINE_RELOC(R_PPC64_TPREL16_HI, 71) \ +_ELF_DEFINE_RELOC(R_PPC64_TPREL16_HA, 72) \ +_ELF_DEFINE_RELOC(R_PPC64_TPREL64, 73) \ +_ELF_DEFINE_RELOC(R_PPC64_DTPREL16, 74) \ +_ELF_DEFINE_RELOC(R_PPC64_DTPREL16_LO, 75) \ +_ELF_DEFINE_RELOC(R_PPC64_DTPREL16_HI, 76) \ +_ELF_DEFINE_RELOC(R_PPC64_DTPREL16_HA, 77) \ +_ELF_DEFINE_RELOC(R_PPC64_DTPREL64, 78) \ +_ELF_DEFINE_RELOC(R_PPC64_GOT_TLSGD16, 79) \ +_ELF_DEFINE_RELOC(R_PPC64_GOT_TLSGD16_LO, 80) \ +_ELF_DEFINE_RELOC(R_PPC64_GOT_TLSGD16_HI, 81) \ +_ELF_DEFINE_RELOC(R_PPC64_GOT_TLSGD16_HA, 82) \ +_ELF_DEFINE_RELOC(R_PPC64_GOT_TLSLD16, 83) \ +_ELF_DEFINE_RELOC(R_PPC64_GOT_TLSLD16_LO, 84) \ +_ELF_DEFINE_RELOC(R_PPC64_GOT_TLSLD16_HI, 85) \ +_ELF_DEFINE_RELOC(R_PPC64_GOT_TLSLD16_HA, 86) \ +_ELF_DEFINE_RELOC(R_PPC64_GOT_TPREL16_DS, 87) \ +_ELF_DEFINE_RELOC(R_PPC64_GOT_TPREL16_LO_DS, 88) \ +_ELF_DEFINE_RELOC(R_PPC64_GOT_TPREL16_HI, 89) \ +_ELF_DEFINE_RELOC(R_PPC64_GOT_TPREL16_HA, 90) \ +_ELF_DEFINE_RELOC(R_PPC64_GOT_DTPREL16_DS, 91) \ +_ELF_DEFINE_RELOC(R_PPC64_GOT_DTPREL16_LO_DS, 92) \ +_ELF_DEFINE_RELOC(R_PPC64_GOT_DTPREL16_HI, 93) \ +_ELF_DEFINE_RELOC(R_PPC64_GOT_DTPREL16_HA, 94) \ +_ELF_DEFINE_RELOC(R_PPC64_TPREL16_DS, 95) \ +_ELF_DEFINE_RELOC(R_PPC64_TPREL16_LO_DS, 96) \ +_ELF_DEFINE_RELOC(R_PPC64_TPREL16_HIGHER, 97) \ +_ELF_DEFINE_RELOC(R_PPC64_TPREL16_HIGHERA, 98) \ +_ELF_DEFINE_RELOC(R_PPC64_TPREL16_HIGHEST, 99) \ +_ELF_DEFINE_RELOC(R_PPC64_TPREL16_HIGHESTA, 100) \ +_ELF_DEFINE_RELOC(R_PPC64_DTPREL16_DS, 101) \ +_ELF_DEFINE_RELOC(R_PPC64_DTPREL16_LO_DS, 102) \ +_ELF_DEFINE_RELOC(R_PPC64_DTPREL16_HIGHER, 103) \ +_ELF_DEFINE_RELOC(R_PPC64_DTPREL16_HIGHERA, 104) \ +_ELF_DEFINE_RELOC(R_PPC64_DTPREL16_HIGHEST, 105) \ +_ELF_DEFINE_RELOC(R_PPC64_DTPREL16_HIGHESTA, 106) \ +_ELF_DEFINE_RELOC(R_PPC64_TLSGD, 107) \ +_ELF_DEFINE_RELOC(R_PPC64_TLSLD, 108) + +#define _ELF_DEFINE_SPARC_RELOCATIONS() \ +_ELF_DEFINE_RELOC(R_SPARC_NONE, 0) \ +_ELF_DEFINE_RELOC(R_SPARC_8, 1) \ +_ELF_DEFINE_RELOC(R_SPARC_16, 2) \ +_ELF_DEFINE_RELOC(R_SPARC_32, 3) \ +_ELF_DEFINE_RELOC(R_SPARC_DISP8, 4) \ +_ELF_DEFINE_RELOC(R_SPARC_DISP16, 5) \ +_ELF_DEFINE_RELOC(R_SPARC_DISP32, 6) \ +_ELF_DEFINE_RELOC(R_SPARC_WDISP30, 7) \ +_ELF_DEFINE_RELOC(R_SPARC_WDISP22, 8) \ +_ELF_DEFINE_RELOC(R_SPARC_HI22, 9) \ +_ELF_DEFINE_RELOC(R_SPARC_22, 10) \ +_ELF_DEFINE_RELOC(R_SPARC_13, 11) \ +_ELF_DEFINE_RELOC(R_SPARC_LO10, 12) \ +_ELF_DEFINE_RELOC(R_SPARC_GOT10, 13) \ +_ELF_DEFINE_RELOC(R_SPARC_GOT13, 14) \ +_ELF_DEFINE_RELOC(R_SPARC_GOT22, 15) \ +_ELF_DEFINE_RELOC(R_SPARC_PC10, 16) \ +_ELF_DEFINE_RELOC(R_SPARC_PC22, 17) \ +_ELF_DEFINE_RELOC(R_SPARC_WPLT30, 18) \ +_ELF_DEFINE_RELOC(R_SPARC_COPY, 19) \ +_ELF_DEFINE_RELOC(R_SPARC_GLOB_DAT, 20) \ +_ELF_DEFINE_RELOC(R_SPARC_JMP_SLOT, 21) \ +_ELF_DEFINE_RELOC(R_SPARC_RELATIVE, 22) \ +_ELF_DEFINE_RELOC(R_SPARC_UA32, 23) \ +_ELF_DEFINE_RELOC(R_SPARC_PLT32, 24) \ +_ELF_DEFINE_RELOC(R_SPARC_HIPLT22, 25) \ +_ELF_DEFINE_RELOC(R_SPARC_LOPLT10, 26) \ +_ELF_DEFINE_RELOC(R_SPARC_PCPLT32, 27) \ +_ELF_DEFINE_RELOC(R_SPARC_PCPLT22, 28) \ +_ELF_DEFINE_RELOC(R_SPARC_PCPLT10, 29) \ +_ELF_DEFINE_RELOC(R_SPARC_10, 30) \ +_ELF_DEFINE_RELOC(R_SPARC_11, 31) \ +_ELF_DEFINE_RELOC(R_SPARC_64, 32) \ +_ELF_DEFINE_RELOC(R_SPARC_OLO10, 33) \ +_ELF_DEFINE_RELOC(R_SPARC_HH22, 34) \ +_ELF_DEFINE_RELOC(R_SPARC_HM10, 35) \ +_ELF_DEFINE_RELOC(R_SPARC_LM22, 36) \ +_ELF_DEFINE_RELOC(R_SPARC_PC_HH22, 37) \ +_ELF_DEFINE_RELOC(R_SPARC_PC_HM10, 38) \ +_ELF_DEFINE_RELOC(R_SPARC_PC_LM22, 39) \ +_ELF_DEFINE_RELOC(R_SPARC_WDISP16, 40) \ +_ELF_DEFINE_RELOC(R_SPARC_WDISP19, 41) \ +_ELF_DEFINE_RELOC(R_SPARC_7, 43) \ +_ELF_DEFINE_RELOC(R_SPARC_5, 44) \ +_ELF_DEFINE_RELOC(R_SPARC_6, 45) \ +_ELF_DEFINE_RELOC(R_SPARC_DISP64, 46) \ +_ELF_DEFINE_RELOC(R_SPARC_PLT64, 47) \ +_ELF_DEFINE_RELOC(R_SPARC_HIX22, 48) \ +_ELF_DEFINE_RELOC(R_SPARC_LOX10, 49) \ +_ELF_DEFINE_RELOC(R_SPARC_H44, 50) \ +_ELF_DEFINE_RELOC(R_SPARC_M44, 51) \ +_ELF_DEFINE_RELOC(R_SPARC_L44, 52) \ +_ELF_DEFINE_RELOC(R_SPARC_REGISTER, 53) \ +_ELF_DEFINE_RELOC(R_SPARC_UA64, 54) \ +_ELF_DEFINE_RELOC(R_SPARC_UA16, 55) \ +_ELF_DEFINE_RELOC(R_SPARC_GOTDATA_HIX22, 80) \ +_ELF_DEFINE_RELOC(R_SPARC_GOTDATA_LOX10, 81) \ +_ELF_DEFINE_RELOC(R_SPARC_GOTDATA_OP_HIX22, 82) \ +_ELF_DEFINE_RELOC(R_SPARC_GOTDATA_OP_LOX10, 83) \ +_ELF_DEFINE_RELOC(R_SPARC_GOTDATA_OP, 84) \ +_ELF_DEFINE_RELOC(R_SPARC_H34, 85) + +#define _ELF_DEFINE_X86_64_RELOCATIONS() \ +_ELF_DEFINE_RELOC(R_X86_64_NONE, 0) \ +_ELF_DEFINE_RELOC(R_X86_64_64, 1) \ +_ELF_DEFINE_RELOC(R_X86_64_PC32, 2) \ +_ELF_DEFINE_RELOC(R_X86_64_GOT32, 3) \ +_ELF_DEFINE_RELOC(R_X86_64_PLT32, 4) \ +_ELF_DEFINE_RELOC(R_X86_64_COPY, 5) \ +_ELF_DEFINE_RELOC(R_X86_64_GLOB_DAT, 6) \ +_ELF_DEFINE_RELOC(R_X86_64_JUMP_SLOT, 7) \ +_ELF_DEFINE_RELOC(R_X86_64_RELATIVE, 8) \ +_ELF_DEFINE_RELOC(R_X86_64_GOTPCREL, 9) \ +_ELF_DEFINE_RELOC(R_X86_64_32, 10) \ +_ELF_DEFINE_RELOC(R_X86_64_32S, 11) \ +_ELF_DEFINE_RELOC(R_X86_64_16, 12) \ +_ELF_DEFINE_RELOC(R_X86_64_PC16, 13) \ +_ELF_DEFINE_RELOC(R_X86_64_8, 14) \ +_ELF_DEFINE_RELOC(R_X86_64_PC8, 15) \ +_ELF_DEFINE_RELOC(R_X86_64_DTPMOD64, 16) \ +_ELF_DEFINE_RELOC(R_X86_64_DTPOFF64, 17) \ +_ELF_DEFINE_RELOC(R_X86_64_TPOFF64, 18) \ +_ELF_DEFINE_RELOC(R_X86_64_TLSGD, 19) \ +_ELF_DEFINE_RELOC(R_X86_64_TLSLD, 20) \ +_ELF_DEFINE_RELOC(R_X86_64_DTPOFF32, 21) \ +_ELF_DEFINE_RELOC(R_X86_64_GOTTPOFF, 22) \ +_ELF_DEFINE_RELOC(R_X86_64_TPOFF32, 23) \ +_ELF_DEFINE_RELOC(R_X86_64_PC64, 24) \ +_ELF_DEFINE_RELOC(R_X86_64_GOTOFF64, 25) \ +_ELF_DEFINE_RELOC(R_X86_64_GOTPC32, 26) \ +_ELF_DEFINE_RELOC(R_X86_64_SIZE32, 32) \ +_ELF_DEFINE_RELOC(R_X86_64_SIZE64, 33) \ +_ELF_DEFINE_RELOC(R_X86_64_GOTPC32_TLSDESC, 34) \ +_ELF_DEFINE_RELOC(R_X86_64_TLSDESC_CALL, 35) \ +_ELF_DEFINE_RELOC(R_X86_64_TLSDESC, 36) + +#define _ELF_DEFINE_HSAIL_RELOCATIONS() \ + _ELF_DEFINE_RELOC(R_HSA_NONE, 0) \ + _ELF_DEFINE_RELOC(R_HSA_RESERVED_TYPE1, 1) /*reserved to use in BRIG linker */\ + _ELF_DEFINE_RELOC(R_HSA_RESERVED_TYPE2, 2) /*reserved to use in BRIG linker */\ + _ELF_DEFINE_RELOC(R_HSA_DWARF_32, 3) \ + _ELF_DEFINE_RELOC(R_HSA_DWARF_TO_BRIG_CODE32, 4) \ + _ELF_DEFINE_RELOC(R_HSA_DWARF_TO_BRIG_DIRECTIVES32, 5) \ + _ELF_DEFINE_RELOC(R_HSA_DWARF_64, 6) \ + _ELF_DEFINE_RELOC(R_HSA_DWARF_TO_BRIG_CODE64, 7) \ + _ELF_DEFINE_RELOC(R_HSA_DWARF_TO_BRIG_DIRECTIVES64, 8) + +#define _ELF_DEFINE_RELOCATIONS() \ +_ELF_DEFINE_386_RELOCATIONS() \ +_ELF_DEFINE_AMD64_RELOCATIONS() \ +_ELF_DEFINE_IA64_RELOCATIONS() \ +_ELF_DEFINE_MIPS_RELOCATIONS() \ +_ELF_DEFINE_PPC32_RELOCATIONS() \ +_ELF_DEFINE_PPC64_RELOCATIONS() \ +_ELF_DEFINE_SPARC_RELOCATIONS() \ +_ELF_DEFINE_X86_64_RELOCATIONS() \ +_ELF_DEFINE_HSAIL_RELOCATIONS() + +#undef _ELF_DEFINE_RELOC +#define _ELF_DEFINE_RELOC(N, V) N = V , +enum { + _ELF_DEFINE_RELOCATIONS() + R__LAST__ +}; + +#define PN_XNUM 0xFFFFU /* Use extended section numbering. */ + +/** + ** ELF Types. + **/ + +typedef uint32_t Elf32_Addr; /* Program address. */ +typedef uint8_t Elf32_Byte; /* Unsigned tiny integer. */ +typedef uint16_t Elf32_Half; /* Unsigned medium integer. */ +typedef uint32_t Elf32_Off; /* File offset. */ +typedef uint16_t Elf32_Section; /* Section index. */ +typedef int32_t Elf32_Sword; /* Signed integer. */ +typedef uint32_t Elf32_Word; /* Unsigned integer. */ +typedef uint64_t Elf32_Lword; /* Unsigned long integer. */ + +typedef uint64_t Elf64_Addr; /* Program address. */ +typedef uint8_t Elf64_Byte; /* Unsigned tiny integer. */ +typedef uint16_t Elf64_Half; /* Unsigned medium integer. */ +typedef uint64_t Elf64_Off; /* File offset. */ +typedef uint16_t Elf64_Section; /* Section index. */ +typedef int32_t Elf64_Sword; /* Signed integer. */ +typedef uint32_t Elf64_Word; /* Unsigned integer. */ +typedef uint64_t Elf64_Lword; /* Unsigned long integer. */ +typedef uint64_t Elf64_Xword; /* Unsigned long integer. */ +typedef int64_t Elf64_Sxword; /* Signed long integer. */ + + +/* + * Capability descriptors. + */ + +/* 32-bit capability descriptor. */ +typedef struct { + Elf32_Word c_tag; /* Type of entry. */ + union { + Elf32_Word c_val; /* Integer value. */ + Elf32_Addr c_ptr; /* Pointer value. */ + } c_un; +} Elf32_Cap; + +/* 64-bit capability descriptor. */ +typedef struct { + Elf64_Xword c_tag; /* Type of entry. */ + union { + Elf64_Xword c_val; /* Integer value. */ + Elf64_Addr c_ptr; /* Pointer value. */ + } c_un; +} Elf64_Cap; + +/* + * MIPS .conflict section entries. + */ + +/* 32-bit entry. */ +typedef struct { + Elf32_Addr c_index; +} Elf32_Conflict; + +/* 64-bit entry. */ +typedef struct { + Elf64_Addr c_index; +} Elf64_Conflict; + +/* + * Dynamic section entries. + */ + +/* 32-bit entry. */ +typedef struct { + Elf32_Sword d_tag; /* Type of entry. */ + union { + Elf32_Word d_val; /* Integer value. */ + Elf32_Addr d_ptr; /* Pointer value. */ + } d_un; +} Elf32_Dyn; + +/* 64-bit entry. */ +typedef struct { + Elf64_Sxword d_tag; /* Type of entry. */ + union { + Elf64_Xword d_val; /* Integer value. */ + Elf64_Addr d_ptr; /* Pointer value; */ + } d_un; +} Elf64_Dyn; + + +/* + * The executable header (EHDR). + */ + +/* 32 bit EHDR. */ +typedef struct { + unsigned char e_ident[EI_NIDENT]; /* ELF identification. */ + Elf32_Half e_type; /* Object file type (ET_*). */ + Elf32_Half e_machine; /* Machine type (EM_*). */ + Elf32_Word e_version; /* File format version (EV_*). */ + Elf32_Addr e_entry; /* Start address. */ + Elf32_Off e_phoff; /* File offset to the PHDR table. */ + Elf32_Off e_shoff; /* File offset to the SHDRheader. */ + Elf32_Word e_flags; /* Flags (EF_*). */ + Elf32_Half e_ehsize; /* Elf header size in bytes. */ + Elf32_Half e_phentsize; /* PHDR table entry size in bytes. */ + Elf32_Half e_phnum; /* Number of PHDR entries. */ + Elf32_Half e_shentsize; /* SHDR table entry size in bytes. */ + Elf32_Half e_shnum; /* Number of SHDR entries. */ + Elf32_Half e_shstrndx; /* Index of section name string table. */ +} Elf32_Ehdr; + + +/* 64 bit EHDR. */ +typedef struct { + unsigned char e_ident[EI_NIDENT]; /* ELF identification. */ + Elf64_Half e_type; /* Object file type (ET_*). */ + Elf64_Half e_machine; /* Machine type (EM_*). */ + Elf64_Word e_version; /* File format version (EV_*). */ + Elf64_Addr e_entry; /* Start address. */ + Elf64_Off e_phoff; /* File offset to the PHDR table. */ + Elf64_Off e_shoff; /* File offset to the SHDRheader. */ + Elf64_Word e_flags; /* Flags (EF_*). */ + Elf64_Half e_ehsize; /* Elf header size in bytes. */ + Elf64_Half e_phentsize; /* PHDR table entry size in bytes. */ + Elf64_Half e_phnum; /* Number of PHDR entries. */ + Elf64_Half e_shentsize; /* SHDR table entry size in bytes. */ + Elf64_Half e_shnum; /* Number of SHDR entries. */ + Elf64_Half e_shstrndx; /* Index of section name string table. */ +} Elf64_Ehdr; + + +/* + * Shared object information. + */ + +/* 32-bit entry. */ +typedef struct { + Elf32_Word l_name; /* The name of a shared object. */ + Elf32_Word l_time_stamp; /* 32-bit timestamp. */ + Elf32_Word l_checksum; /* Checksum of visible symbols, sizes. */ + Elf32_Word l_version; /* Interface version string index. */ + Elf32_Word l_flags; /* Flags (LL_*). */ +} Elf32_Lib; + +/* 64-bit entry. */ +typedef struct { + Elf64_Word l_name; + Elf64_Word l_time_stamp; + Elf64_Word l_checksum; + Elf64_Word l_version; + Elf64_Word l_flags; +} Elf64_Lib; + +#define _ELF_DEFINE_LL_FLAGS() \ +_ELF_DEFINE_LL(LL_NONE, 0, \ + "no flags") \ +_ELF_DEFINE_LL(LL_EXACT_MATCH, 0x1, \ + "require an exact match") \ +_ELF_DEFINE_LL(LL_IGNORE_INT_VER, 0x2, \ + "ignore version incompatibilities") \ +_ELF_DEFINE_LL(LL_REQUIRE_MINOR, 0x4, \ + "") \ +_ELF_DEFINE_LL(LL_EXPORTS, 0x8, \ + "") \ +_ELF_DEFINE_LL(LL_DELAY_LOAD, 0x10, \ + "") \ +_ELF_DEFINE_LL(LL_DELTA, 0x20, \ + "") + +#undef _ELF_DEFINE_LL +#define _ELF_DEFINE_LL(N, V, DESCR) N = V , +enum { + _ELF_DEFINE_LL_FLAGS() + LL__LAST__ +}; + +/* + * Note tags + */ + +#define _ELF_DEFINE_NOTE_ENTRY_TYPES() \ +_ELF_DEFINE_NT(NT_ABI_TAG, 1, "Tag indicating the ABI") \ +_ELF_DEFINE_NT(NT_GNU_HWCAP, 2, "Hardware capabilities") \ +_ELF_DEFINE_NT(NT_GNU_BUILD_ID, 3, "Build id, set by ld(1)") \ +_ELF_DEFINE_NT(NT_GNU_GOLD_VERSION, 4, \ + "Version number of the GNU gold linker") \ +_ELF_DEFINE_NT(NT_PRSTATUS, 1, "Process status") \ +_ELF_DEFINE_NT(NT_FPREGSET, 2, "Floating point information") \ +_ELF_DEFINE_NT(NT_PRPSINFO, 3, "Process information") \ +_ELF_DEFINE_NT(NT_AUXV, 6, "Auxiliary vector") \ +_ELF_DEFINE_NT(NT_PRXFPREG, 0x46E62B7FUL, \ + "Linux user_xfpregs structure") \ +_ELF_DEFINE_NT(NT_PSTATUS, 10, "Linux process status") \ +_ELF_DEFINE_NT(NT_FPREGS, 12, "Linux floating point regset") \ +_ELF_DEFINE_NT(NT_PSINFO, 13, "Linux process information") \ +_ELF_DEFINE_NT(NT_LWPSTATUS, 16, "Linux lwpstatus_t type") \ +_ELF_DEFINE_NT(NT_LWPSINFO, 17, "Linux lwpinfo_t type") + +#undef _ELF_DEFINE_NT +#define _ELF_DEFINE_NT(N, V, DESCR) N = V , +enum { + _ELF_DEFINE_NOTE_ENTRY_TYPES() + NT__LAST__ +}; + +/* Aliases for the ABI tag. */ +#define NT_FREEBSD_ABI_TAG NT_ABI_TAG +#define NT_GNU_ABI_TAG NT_ABI_TAG +#define NT_NETBSD_IDENT NT_ABI_TAG +#define NT_OPENBSD_IDENT NT_ABI_TAG + +/* + * Note descriptors. + */ + +typedef struct { + uint32_t n_namesz; /* Length of note's name. */ + uint32_t n_descsz; /* Length of note's value. */ + uint32_t n_type; /* Type of note. */ +} Elf_Note; + +typedef Elf_Note Elf32_Nhdr; /* 32-bit note header. */ +typedef Elf_Note Elf64_Nhdr; /* 64-bit note header. */ + +/* + * MIPS ELF options descriptor header. + */ + +typedef struct { + Elf64_Byte kind; /* Type of options. */ + Elf64_Byte size; /* Size of option descriptor. */ + Elf64_Half section; /* Index of section affected. */ + Elf64_Word info; /* Kind-specific information. */ +} Elf_Options; + +/* + * Option kinds. + */ + +#define _ELF_DEFINE_OPTION_KINDS() \ +_ELF_DEFINE_ODK(ODK_NULL, 0, "undefined") \ +_ELF_DEFINE_ODK(ODK_REGINFO, 1, "register usage info") \ +_ELF_DEFINE_ODK(ODK_EXCEPTIONS, 2, "exception processing info") \ +_ELF_DEFINE_ODK(ODK_PAD, 3, "section padding") \ +_ELF_DEFINE_ODK(ODK_HWPATCH, 4, "hardware patch applied") \ +_ELF_DEFINE_ODK(ODK_FILL, 5, "fill value used by linker") \ +_ELF_DEFINE_ODK(ODK_TAGS, 6, "reserved space for tools") \ +_ELF_DEFINE_ODK(ODK_HWAND, 7, "hardware AND patch applied") \ +_ELF_DEFINE_ODK(ODK_HWOR, 8, "hardware OR patch applied") \ +_ELF_DEFINE_ODK(ODK_GP_GROUP, 9, \ + "GP group to use for text/data sections") \ +_ELF_DEFINE_ODK(ODK_IDENT, 10, "ID information") \ +_ELF_DEFINE_ODK(ODK_PAGESIZE, 11, "page size infomation") + +#undef _ELF_DEFINE_ODK +#define _ELF_DEFINE_ODK(N, V, DESCR) N = V , +enum { + _ELF_DEFINE_OPTION_KINDS() + ODK__LAST__ +}; + +/* + * ODK_EXCEPTIONS info field masks. + */ + +#define _ELF_DEFINE_ODK_EXCEPTIONS_MASK() \ +_ELF_DEFINE_OEX(OEX_FPU_MIN, 0x0000001FUL, \ + "minimum FPU exception which must be enabled") \ +_ELF_DEFINE_OEX(OEX_FPU_MAX, 0x00001F00UL, \ + "maximum FPU exception which can be enabled") \ +_ELF_DEFINE_OEX(OEX_PAGE0, 0x00010000UL, \ + "page zero must be mapped") \ +_ELF_DEFINE_OEX(OEX_SMM, 0x00020000UL, \ + "run in sequential memory mode") \ +_ELF_DEFINE_OEX(OEX_PRECISEFP, 0x00040000UL, \ + "run in precise FP exception mode") \ +_ELF_DEFINE_OEX(OEX_DISMISS, 0x00080000UL, \ + "dismiss invalid address traps") + +#undef _ELF_DEFINE_OEX +#define _ELF_DEFINE_OEX(N, V, DESCR) N = V , +enum { + _ELF_DEFINE_ODK_EXCEPTIONS_MASK() + OEX__LAST__ +}; + +/* + * ODK_PAD info field masks. + */ + +#define _ELF_DEFINE_ODK_PAD_MASK() \ +_ELF_DEFINE_OPAD(OPAD_PREFIX, 0x0001) \ +_ELF_DEFINE_OPAD(OPAD_POSTFIX, 0x0002) \ +_ELF_DEFINE_OPAD(OPAD_SYMBOL, 0x0004) + +#undef _ELF_DEFINE_OPAD +#define _ELF_DEFINE_OPAD(N, V) N = V , +enum { + _ELF_DEFINE_ODK_PAD_MASK() + OPAD__LAST__ +}; + +/* + * ODK_HWPATCH info field masks. + */ + +#define _ELF_DEFINE_ODK_HWPATCH_MASK() \ +_ELF_DEFINE_OHW(OHW_R4KEOP, 0x00000001UL, \ + "patch for R4000 branch at end-of-page bug") \ +_ELF_DEFINE_OHW(OHW_R8KPFETCH, 0x00000002UL, \ + "R8000 prefetch bug may occur") \ +_ELF_DEFINE_OHW(OHW_R5KEOP, 0x00000004UL, \ + "patch for R5000 branch at end-of-page bug") \ +_ELF_DEFINE_OHW(OHW_R5KCVTL, 0x00000008UL, \ + "R5000 cvt.[ds].l bug: clean == 1") \ +_ELF_DEFINE_OHW(OHW_R10KLDL, 0x00000010UL, \ + "needd patch for R10000 misaligned load") + +#undef _ELF_DEFINE_OHW +#define _ELF_DEFINE_OHW(N, V, DESCR) N = V , +enum { + _ELF_DEFINE_ODK_HWPATCH_MASK() + OHW__LAST__ +}; + +/* + * ODK_HWAND/ODK_HWOR info field and hwp_flags[12] masks. + */ + +#define _ELF_DEFINE_ODK_HWP_MASK() \ +_ELF_DEFINE_HWP(OHWA0_R4KEOP_CHECKED, 0x00000001UL, \ + "object checked for R4000 end-of-page bug") \ +_ELF_DEFINE_HWP(OHWA0_R4KEOP_CLEAN, 0x00000002UL, \ + "object verified clean for R4000 end-of-page bug") \ +_ELF_DEFINE_HWP(OHWO0_FIXADE, 0x00000001UL, \ + "object requires call to fixade") + +#undef _ELF_DEFINE_HWP +#define _ELF_DEFINE_HWP(N, V, DESCR) N = V , +enum { + _ELF_DEFINE_ODK_HWP_MASK() + OHWX0__LAST__ +}; + +/* + * ODK_IDENT/ODK_GP_GROUP info field masks. + */ + +#define _ELF_DEFINE_ODK_GP_MASK() \ +_ELF_DEFINE_OGP(OGP_GROUP, 0x0000FFFFUL, "GP group number") \ +_ELF_DEFINE_OGP(OGP_SELF, 0x00010000UL, \ + "GP group is self-contained") + +#undef _ELF_DEFINE_OGP +#define _ELF_DEFINE_OGP(N, V, DESCR) N = V , +enum { + _ELF_DEFINE_ODK_GP_MASK() + OGP__LAST__ +}; + +/* + * MIPS ELF register info descriptor. + */ + +/* 32 bit RegInfo entry. */ +typedef struct { + Elf32_Word ri_gprmask; /* Mask of general register used. */ + Elf32_Word ri_cprmask[4]; /* Mask of coprocessor register used. */ + Elf32_Addr ri_gp_value; /* GP register value. */ +} Elf32_RegInfo; + +/* 64 bit RegInfo entry. */ +typedef struct { + Elf64_Word ri_gprmask; /* Mask of general register used. */ + Elf64_Word ri_pad; /* Padding. */ + Elf64_Word ri_cprmask[4]; /* Mask of coprocessor register used. */ + Elf64_Addr ri_gp_value; /* GP register value. */ +} Elf64_RegInfo; + +/* + * Program Header Table (PHDR) entries. + */ + +/* 32 bit PHDR entry. */ +typedef struct { + Elf32_Word p_type; /* Type of segment. */ + Elf32_Off p_offset; /* File offset to segment. */ + Elf32_Addr p_vaddr; /* Virtual address in memory. */ + Elf32_Addr p_paddr; /* Physical address (if relevant). */ + Elf32_Word p_filesz; /* Size of segment in file. */ + Elf32_Word p_memsz; /* Size of segment in memory. */ + Elf32_Word p_flags; /* Segment flags. */ + Elf32_Word p_align; /* Alignment constraints. */ +} Elf32_Phdr; + +/* 64 bit PHDR entry. */ +typedef struct { + Elf64_Word p_type; /* Type of segment. */ + Elf64_Word p_flags; /* File offset to segment. */ + Elf64_Off p_offset; /* Virtual address in memory. */ + Elf64_Addr p_vaddr; /* Physical address (if relevant). */ + Elf64_Addr p_paddr; /* Size of segment in file. */ + Elf64_Xword p_filesz; /* Size of segment in memory. */ + Elf64_Xword p_memsz; /* Segment flags. */ + Elf64_Xword p_align; /* Alignment constraints. */ +} Elf64_Phdr; + + +/* + * Move entries, for describing data in COMMON blocks in a compact + * manner. + */ + +/* 32-bit move entry. */ +typedef struct { + Elf32_Lword m_value; /* Initialization value. */ + Elf32_Word m_info; /* Encoded size and index. */ + Elf32_Word m_poffset; /* Offset relative to symbol. */ + Elf32_Half m_repeat; /* Repeat count. */ + Elf32_Half m_stride; /* Number of units to skip. */ +} Elf32_Move; + +/* 64-bit move entry. */ +typedef struct { + Elf64_Lword m_value; /* Initialization value. */ + Elf64_Xword m_info; /* Encoded size and index. */ + Elf64_Xword m_poffset; /* Offset relative to symbol. */ + Elf64_Half m_repeat; /* Repeat count. */ + Elf64_Half m_stride; /* Number of units to skip. */ +} Elf64_Move; +#ifndef ELF32_M_SYM +#define ELF32_M_SYM(I) ((I) >> 8) +#endif +#ifndef ELF32_M_SIZE +#define ELF32_M_SIZE(I) ((unsigned char) (I)) +#endif +#ifndef ELF32_M_INFO +#define ELF32_M_INFO(M, S) (((M) << 8) + (unsigned char) (S)) +#endif + +#ifndef ELF64_M_SYM +#define ELF64_M_SYM(I) ((I) >> 8) +#endif +#ifndef ELF64_M_SIZE +#define ELF64_M_SIZE(I) ((unsigned char) (I)) +#endif +#ifndef ELF64_M_INFO +#define ELF64_M_INFO(M, S) (((M) << 8) + (unsigned char) (S)) +#endif + +/* + * Section Header Table (SHDR) entries. + */ + +/* 32 bit SHDR */ +typedef struct { + Elf32_Word sh_name; /* index of section name */ + Elf32_Word sh_type; /* section type */ + Elf32_Word sh_flags; /* section flags */ + Elf32_Addr sh_addr; /* in-memory address of section */ + Elf32_Off sh_offset; /* file offset of section */ + Elf32_Word sh_size; /* section size in bytes */ + Elf32_Word sh_link; /* section header table link */ + Elf32_Word sh_info; /* extra information */ + Elf32_Word sh_addralign; /* alignment constraint */ + Elf32_Word sh_entsize; /* size for fixed-size entries */ +} Elf32_Shdr; + +/* 64 bit SHDR */ +typedef struct { + Elf64_Word sh_name; /* index of section name */ + Elf64_Word sh_type; /* section type */ + Elf64_Xword sh_flags; /* section flags */ + Elf64_Addr sh_addr; /* in-memory address of section */ + Elf64_Off sh_offset; /* file offset of section */ + Elf64_Xword sh_size; /* section size in bytes */ + Elf64_Word sh_link; /* section header table link */ + Elf64_Word sh_info; /* extra information */ + Elf64_Xword sh_addralign; /* alignment constraint */ + Elf64_Xword sh_entsize; /* size for fixed-size entries */ +} Elf64_Shdr; + + +/* + * Symbol table entries. + */ + +typedef struct { + Elf32_Word st_name; /* index of symbol's name */ + Elf32_Addr st_value; /* value for the symbol */ + Elf32_Word st_size; /* size of associated data */ + unsigned char st_info; /* type and binding attributes */ + unsigned char st_other; /* visibility */ + Elf32_Half st_shndx; /* index of related section */ +} Elf32_Sym; + +typedef struct { + Elf64_Word st_name; /* index of symbol's name */ + unsigned char st_info; /* value for the symbol */ + unsigned char st_other; /* size of associated data */ + Elf64_Half st_shndx; /* type and binding attributes */ + Elf64_Addr st_value; /* visibility */ + Elf64_Xword st_size; /* index of related section */ +} Elf64_Sym; +#ifndef ELF32_ST_BIND +#define ELF32_ST_BIND(I) ((I) >> 4) +#endif +#ifndef ELF32_ST_TYPE +#define ELF32_ST_TYPE(I) ((I) & 0xFU) +#endif +#ifndef ELF32_ST_INFO +#define ELF32_ST_INFO(B,T) (((B) << 4) + ((T) & 0xF)) +#endif + +#ifndef ELF64_ST_BIND +#define ELF64_ST_BIND(I) ((I) >> 4) +#endif +#ifndef ELF64_ST_TYPE +#define ELF64_ST_TYPE(I) ((I) & 0xFU) +#endif +#ifndef ELF64_ST_INFO +#define ELF64_ST_INFO(B,T) (((B) << 4) + ((T) & 0xF)) +#endif + +#ifndef ELF32_ST_VISIBILITY +#define ELF32_ST_VISIBILITY(O) ((O) & 0x3) +#endif + +#ifndef ELF64_ST_VISIBILITY +#define ELF64_ST_VISIBILITY(O) ((O) & 0x3) +#endif + +/* + * Syminfo descriptors, containing additional symbol information. + */ + +/* 32-bit entry. */ +typedef struct { + Elf32_Half si_boundto; /* Entry index with additional flags. */ + Elf32_Half si_flags; /* Flags. */ +} Elf32_Syminfo; + +/* 64-bit entry. */ +typedef struct { + Elf64_Half si_boundto; /* Entry index with additional flags. */ + Elf64_Half si_flags; /* Flags. */ +} Elf64_Syminfo; + +/* + * Relocation descriptors. + */ + +typedef struct { + Elf32_Addr r_offset; /* location to apply relocation to */ + Elf32_Word r_info; /* type+section for relocation */ +} Elf32_Rel; + +typedef struct { + Elf32_Addr r_offset; /* location to apply relocation to */ + Elf32_Word r_info; /* type+section for relocation */ + Elf32_Sword r_addend; /* constant addend */ +} Elf32_Rela; + +typedef struct { + Elf64_Addr r_offset; /* location to apply relocation to */ + Elf64_Xword r_info; /* type+section for relocation */ +} Elf64_Rel; + +typedef struct { + Elf64_Addr r_offset; /* location to apply relocation to */ + Elf64_Xword r_info; /* type+section for relocation */ + Elf64_Sxword r_addend; /* constant addend */ +} Elf64_Rela; + +#ifndef ELF32_R_SYM +#define ELF32_R_SYM(I) ((I) >> 8) +#endif +#ifndef ELF32_R_TYPE +#define ELF32_R_TYPE(I) ((unsigned char) (I)) +#endif +#ifndef ELF32_R_INFO +#define ELF32_R_INFO(S,T) (((S) << 8) + (unsigned char) (T)) +#endif +#ifndef ELF64_R_SYM +#define ELF64_R_SYM(I) ((I) >> 32) +#endif +#ifndef ELF64_R_TYPE +#define ELF64_R_TYPE(I) ((I) & 0xFFFFFFFFUL) +#endif +#ifndef ELF64_R_INFO +#define ELF64_R_INFO(S,T) (((S) << 32) + ((T) & 0xFFFFFFFFUL)) +#endif + +/* + * Symbol versioning structures. + */ + +/* 32-bit structures. */ +typedef struct +{ + Elf32_Word vda_name; /* Index to name. */ + Elf32_Word vda_next; /* Offset to next entry. */ +} Elf32_Verdaux; + +typedef struct +{ + Elf32_Word vna_hash; /* Hash value of dependency name. */ + Elf32_Half vna_flags; /* Flags. */ + Elf32_Half vna_other; /* Unused. */ + Elf32_Word vna_name; /* Offset to dependency name. */ + Elf32_Word vna_next; /* Offset to next vernaux entry. */ +} Elf32_Vernaux; + +typedef struct +{ + Elf32_Half vd_version; /* Version information. */ + Elf32_Half vd_flags; /* Flags. */ + Elf32_Half vd_ndx; /* Index into the versym section. */ + Elf32_Half vd_cnt; /* Number of aux entries. */ + Elf32_Word vd_hash; /* Hash value of name. */ + Elf32_Word vd_aux; /* Offset to aux entries. */ + Elf32_Word vd_next; /* Offset to next version definition. */ +} Elf32_Verdef; + +typedef struct +{ + Elf32_Half vn_version; /* Version number. */ + Elf32_Half vn_cnt; /* Number of aux entries. */ + Elf32_Word vn_file; /* Offset of associated file name. */ + Elf32_Word vn_aux; /* Offset of vernaux array. */ + Elf32_Word vn_next; /* Offset of next verneed entry. */ +} Elf32_Verneed; + +typedef Elf32_Half Elf32_Versym; + +/* 64-bit structures. */ + +typedef struct { + Elf64_Word vda_name; /* Index to name. */ + Elf64_Word vda_next; /* Offset to next entry. */ +} Elf64_Verdaux; + +typedef struct { + Elf64_Word vna_hash; /* Hash value of dependency name. */ + Elf64_Half vna_flags; /* Flags. */ + Elf64_Half vna_other; /* Unused. */ + Elf64_Word vna_name; /* Offset to dependency name. */ + Elf64_Word vna_next; /* Offset to next vernaux entry. */ +} Elf64_Vernaux; + +typedef struct { + Elf64_Half vd_version; /* Version information. */ + Elf64_Half vd_flags; /* Flags. */ + Elf64_Half vd_ndx; /* Index into the versym section. */ + Elf64_Half vd_cnt; /* Number of aux entries. */ + Elf64_Word vd_hash; /* Hash value of name. */ + Elf64_Word vd_aux; /* Offset to aux entries. */ + Elf64_Word vd_next; /* Offset to next version definition. */ +} Elf64_Verdef; + +typedef struct { + Elf64_Half vn_version; /* Version number. */ + Elf64_Half vn_cnt; /* Number of aux entries. */ + Elf64_Word vn_file; /* Offset of associated file name. */ + Elf64_Word vn_aux; /* Offset of vernaux array. */ + Elf64_Word vn_next; /* Offset of next verneed entry. */ +} Elf64_Verneed; + +typedef Elf64_Half Elf64_Versym; + + +#ifndef LIBELF_CONFIG_GNUHASH +#define LIBELF_CONFIG_GNUHASH 1 + +/* + * The header for GNU-style hash sections. + */ + +typedef struct { + uint32_t gh_nbuckets; /* Number of hash buckets. */ + uint32_t gh_symndx; /* First visible symbol in .dynsym. */ + uint32_t gh_maskwords; /* #maskwords used in bloom filter. */ + uint32_t gh_shift2; /* Bloom filter shift count. */ +} Elf_GNU_Hash_Header; +#endif + +#endif /* _ELFDEFINITIONS_H_ */ diff --git a/rocclr/compiler/lib/loaders/elf/utils/common/native-elf-format b/rocclr/compiler/lib/loaders/elf/utils/common/native-elf-format new file mode 100644 index 0000000000..af707591d1 --- /dev/null +++ b/rocclr/compiler/lib/loaders/elf/utils/common/native-elf-format @@ -0,0 +1,47 @@ +#!/bin/sh +# +# $Id: native-elf-format 2064 2011-10-26 15:12:32Z jkoshy $ +# +# Find the native ELF format for a host platform by compiling a +# test object and examining the resulting object. +# +# This script is used if there is no easy way to determine this +# information statically at compile time. + +program=`basename $0` +tmp_c=`mktemp -u nefXXXXXX`.c +tmp_o=`echo ${tmp_c} | sed -e 's/.c$/.o/'` + +trap "rm -f ${tmp_c} ${tmp_o}" 0 1 2 3 15 + +touch ${tmp_c} + +echo "/* Generated by ${program} on `date` */" + +cc -c ${tmp_c} -o ${tmp_o} +readelf -h ${tmp_o} | awk ' +$1 ~ "Class:" { + sub("ELF","",$2); elfclass = $2; + } +$1 ~ "Data:" { + if (match($0, "little")) { + elfdata = "LSB"; + } else { + elfdata = "MSB"; + } + } +$1 ~ "Machine:" { + if (match($0, "Intel.*386")) { + elfarch = "EM_386"; + } else if (match($0, ".*X86-64")) { + elfarch = "EM_X86_64"; + } else { + elfarch = "unknown"; + } + } +END { + printf("#define ELFTC_CLASS ELFCLASS%s\n", elfclass); + printf("#define ELFTC_ARCH %s\n", elfarch); + printf("#define ELFTC_BYTEORDER ELFDATA2%s\n", elfdata); +}' + diff --git a/rocclr/compiler/lib/loaders/elf/utils/common/native-elf-format.h b/rocclr/compiler/lib/loaders/elf/utils/common/native-elf-format.h new file mode 100644 index 0000000000..bc997a7df6 --- /dev/null +++ b/rocclr/compiler/lib/loaders/elf/utils/common/native-elf-format.h @@ -0,0 +1,7 @@ +#if !defined(_LP64) +#define ELFTC_CLASS ELFCLASS32 +#else +#define ELFTC_CLASS ELFCLASS64 +#endif +#define ELFTC_ARCH EM_386 +#define ELFTC_BYTEORDER ELFDATA2LSB diff --git a/rocclr/compiler/lib/loaders/elf/utils/common/uthash.h b/rocclr/compiler/lib/loaders/elf/utils/common/uthash.h new file mode 100644 index 0000000000..8428b9c0dd --- /dev/null +++ b/rocclr/compiler/lib/loaders/elf/utils/common/uthash.h @@ -0,0 +1,906 @@ +/* +Copyright (c) 2003-2011, Troy D. Hanson http://uthash.sourceforge.net +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS +IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED +TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A +PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER +OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, +EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, +PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR +PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF +LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING +NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + +/* $Id: uthash.h 2064 2011-10-26 15:12:32Z jkoshy $ */ + +#ifndef UTHASH_H +#define UTHASH_H + +#include /* memcmp,strlen */ +#include /* ptrdiff_t */ +#include /* exit() */ + +/* These macros use decltype or the earlier __typeof GNU extension. + As decltype is only available in newer compilers (VS2010 or gcc 4.3+ + when compiling c++ source) this code uses whatever method is needed + or, for VS2008 where neither is available, uses casting workarounds. */ +#ifdef _MSC_VER /* MS compiler */ +#if _MSC_VER >= 1600 && defined(__cplusplus) /* VS2010 or newer in C++ mode */ +#define DECLTYPE(x) (decltype(x)) +#else /* VS2008 or older (or VS2010 in C mode) */ +#define NO_DECLTYPE +#define DECLTYPE(x) +#endif +#else /* GNU, Sun and other compilers */ +#define DECLTYPE(x) (__typeof(x)) +#endif + +#ifdef NO_DECLTYPE +#define DECLTYPE_ASSIGN(dst,src) \ +do { \ + char **_da_dst = (char**)(&(dst)); \ + *_da_dst = (char*)(src); \ +} while(0) +#else +#define DECLTYPE_ASSIGN(dst,src) \ +do { \ + (dst) = DECLTYPE(dst)(src); \ +} while(0) +#endif + +/* a number of the hash function use uint32_t which isn't defined on win32 */ +#ifdef _MSC_VER +typedef unsigned int uint32_t; +typedef unsigned char uint8_t; +#else +#include /* uint32_t */ +#endif + +#define UTHASH_VERSION 1.9.4 + +#define uthash_fatal(msg) exit(-1) /* fatal error (out of memory,etc) */ +#define uthash_malloc(sz) malloc(sz) /* malloc fcn */ +#define uthash_free(ptr,sz) free(ptr) /* free fcn */ + +#define uthash_noexpand_fyi(tbl) /* can be defined to log noexpand */ +#define uthash_expand_fyi(tbl) /* can be defined to log expands */ + +/* initial number of buckets */ +#define HASH_INITIAL_NUM_BUCKETS 32 /* initial number of buckets */ +#define HASH_INITIAL_NUM_BUCKETS_LOG2 5 /* lg2 of initial number of buckets */ +#define HASH_BKT_CAPACITY_THRESH 10 /* expand when bucket count reaches */ + +/* calculate the element whose hash handle address is hhe */ +#define ELMT_FROM_HH(tbl,hhp) ((void*)(((char*)(hhp)) - ((tbl)->hho))) + +#define HASH_FIND(hh,head,keyptr,keylen,out) \ +do { \ + unsigned _hf_bkt,_hf_hashv; \ + out=NULL; \ + if (head) { \ + HASH_FCN(keyptr,keylen, (head)->hh.tbl->num_buckets, _hf_hashv, _hf_bkt); \ + if (HASH_BLOOM_TEST((head)->hh.tbl, _hf_hashv)) { \ + HASH_FIND_IN_BKT((head)->hh.tbl, hh, (head)->hh.tbl->buckets[ _hf_bkt ], \ + keyptr,keylen,out); \ + } \ + } \ +} while (0) + +#ifdef HASH_BLOOM +#define HASH_BLOOM_BITLEN (1ULL << HASH_BLOOM) +#define HASH_BLOOM_BYTELEN (HASH_BLOOM_BITLEN/8) + ((HASH_BLOOM_BITLEN%8) ? 1:0) +#define HASH_BLOOM_MAKE(tbl) \ +do { \ + (tbl)->bloom_nbits = HASH_BLOOM; \ + (tbl)->bloom_bv = (uint8_t*)uthash_malloc(HASH_BLOOM_BYTELEN); \ + if (!((tbl)->bloom_bv)) { uthash_fatal( "out of memory"); } \ + memset((tbl)->bloom_bv, 0, HASH_BLOOM_BYTELEN); \ + (tbl)->bloom_sig = HASH_BLOOM_SIGNATURE; \ +} while (0); + +#define HASH_BLOOM_FREE(tbl) \ +do { \ + uthash_free((tbl)->bloom_bv, HASH_BLOOM_BYTELEN); \ +} while (0); + +#define HASH_BLOOM_BITSET(bv,idx) (bv[(idx)/8] |= (1U << ((idx)%8))) +#define HASH_BLOOM_BITTEST(bv,idx) (bv[(idx)/8] & (1U << ((idx)%8))) + +#define HASH_BLOOM_ADD(tbl,hashv) \ + HASH_BLOOM_BITSET((tbl)->bloom_bv, (hashv & (uint32_t)((1ULL << (tbl)->bloom_nbits) - 1))) + +#define HASH_BLOOM_TEST(tbl,hashv) \ + HASH_BLOOM_BITTEST((tbl)->bloom_bv, (hashv & (uint32_t)((1ULL << (tbl)->bloom_nbits) - 1))) + +#else +#define HASH_BLOOM_MAKE(tbl) +#define HASH_BLOOM_FREE(tbl) +#define HASH_BLOOM_ADD(tbl,hashv) +#define HASH_BLOOM_TEST(tbl,hashv) (1) +#endif + +#define HASH_MAKE_TABLE(hh,head) \ +do { \ + (head)->hh.tbl = (UT_hash_table*)uthash_malloc( \ + sizeof(UT_hash_table)); \ + if (!((head)->hh.tbl)) { uthash_fatal( "out of memory"); } \ + memset((head)->hh.tbl, 0, sizeof(UT_hash_table)); \ + (head)->hh.tbl->tail = &((head)->hh); \ + (head)->hh.tbl->num_buckets = HASH_INITIAL_NUM_BUCKETS; \ + (head)->hh.tbl->log2_num_buckets = HASH_INITIAL_NUM_BUCKETS_LOG2; \ + (head)->hh.tbl->hho = (char*)(&(head)->hh) - (char*)(head); \ + (head)->hh.tbl->buckets = (UT_hash_bucket*)uthash_malloc( \ + HASH_INITIAL_NUM_BUCKETS*sizeof(struct UT_hash_bucket)); \ + if (! (head)->hh.tbl->buckets) { uthash_fatal( "out of memory"); } \ + memset((head)->hh.tbl->buckets, 0, \ + HASH_INITIAL_NUM_BUCKETS*sizeof(struct UT_hash_bucket)); \ + HASH_BLOOM_MAKE((head)->hh.tbl); \ + (head)->hh.tbl->signature = HASH_SIGNATURE; \ +} while(0) + +#define HASH_ADD(hh,head,fieldname,keylen_in,add) \ + HASH_ADD_KEYPTR(hh,head,&add->fieldname,keylen_in,add) + +#define HASH_ADD_KEYPTR(hh,head,keyptr,keylen_in,add) \ +do { \ + unsigned _ha_bkt; \ + (add)->hh.next = NULL; \ + (add)->hh.key = (char*)keyptr; \ + (add)->hh.keylen = keylen_in; \ + if (!(head)) { \ + head = (add); \ + (head)->hh.prev = NULL; \ + HASH_MAKE_TABLE(hh,head); \ + } else { \ + (head)->hh.tbl->tail->next = (add); \ + (add)->hh.prev = ELMT_FROM_HH((head)->hh.tbl, (head)->hh.tbl->tail); \ + (head)->hh.tbl->tail = &((add)->hh); \ + } \ + (head)->hh.tbl->num_items++; \ + (add)->hh.tbl = (head)->hh.tbl; \ + HASH_FCN(keyptr,keylen_in, (head)->hh.tbl->num_buckets, \ + (add)->hh.hashv, _ha_bkt); \ + HASH_ADD_TO_BKT((head)->hh.tbl->buckets[_ha_bkt],&(add)->hh); \ + HASH_BLOOM_ADD((head)->hh.tbl,(add)->hh.hashv); \ + HASH_EMIT_KEY(hh,head,keyptr,keylen_in); \ + HASH_FSCK(hh,head); \ +} while(0) + +#define HASH_TO_BKT( hashv, num_bkts, bkt ) \ +do { \ + bkt = ((hashv) & ((num_bkts) - 1)); \ +} while(0) + +/* delete "delptr" from the hash table. + * "the usual" patch-up process for the app-order doubly-linked-list. + * The use of _hd_hh_del below deserves special explanation. + * These used to be expressed using (delptr) but that led to a bug + * if someone used the same symbol for the head and deletee, like + * HASH_DELETE(hh,users,users); + * We want that to work, but by changing the head (users) below + * we were forfeiting our ability to further refer to the deletee (users) + * in the patch-up process. Solution: use scratch space to + * copy the deletee pointer, then the latter references are via that + * scratch pointer rather than through the repointed (users) symbol. + */ +#define HASH_DELETE(hh,head,delptr) \ +do { \ + unsigned _hd_bkt; \ + struct UT_hash_handle *_hd_hh_del; \ + if ( ((delptr)->hh.prev == NULL) && ((delptr)->hh.next == NULL) ) { \ + uthash_free((head)->hh.tbl->buckets, \ + (head)->hh.tbl->num_buckets*sizeof(struct UT_hash_bucket) ); \ + HASH_BLOOM_FREE((head)->hh.tbl); \ + uthash_free((head)->hh.tbl, sizeof(UT_hash_table)); \ + head = NULL; \ + } else { \ + _hd_hh_del = &((delptr)->hh); \ + if ((delptr) == ELMT_FROM_HH((head)->hh.tbl,(head)->hh.tbl->tail)) { \ + (head)->hh.tbl->tail = \ + (UT_hash_handle*)((char*)((delptr)->hh.prev) + \ + (head)->hh.tbl->hho); \ + } \ + if ((delptr)->hh.prev) { \ + ((UT_hash_handle*)((char*)((delptr)->hh.prev) + \ + (head)->hh.tbl->hho))->next = (delptr)->hh.next; \ + } else { \ + DECLTYPE_ASSIGN(head,(delptr)->hh.next); \ + } \ + if (_hd_hh_del->next) { \ + ((UT_hash_handle*)((char*)_hd_hh_del->next + \ + (head)->hh.tbl->hho))->prev = \ + _hd_hh_del->prev; \ + } \ + HASH_TO_BKT( _hd_hh_del->hashv, (head)->hh.tbl->num_buckets, _hd_bkt); \ + HASH_DEL_IN_BKT(hh,(head)->hh.tbl->buckets[_hd_bkt], _hd_hh_del); \ + (head)->hh.tbl->num_items--; \ + } \ + HASH_FSCK(hh,head); \ +} while (0) + + +/* convenience forms of HASH_FIND/HASH_ADD/HASH_DEL */ +#define HASH_FIND_STR(head,findstr,out) \ + HASH_FIND(hh,head,findstr,strlen(findstr),out) +#define HASH_ADD_STR(head,strfield,add) \ + HASH_ADD(hh,head,strfield,strlen(add->strfield),add) +#define HASH_FIND_INT(head,findint,out) \ + HASH_FIND(hh,head,findint,sizeof(int),out) +#define HASH_ADD_INT(head,intfield,add) \ + HASH_ADD(hh,head,intfield,sizeof(int),add) +#define HASH_FIND_PTR(head,findptr,out) \ + HASH_FIND(hh,head,findptr,sizeof(void *),out) +#define HASH_ADD_PTR(head,ptrfield,add) \ + HASH_ADD(hh,head,ptrfield,sizeof(void *),add) +#define HASH_DEL(head,delptr) \ + HASH_DELETE(hh,head,delptr) + +/* HASH_FSCK checks hash integrity on every add/delete when HASH_DEBUG is defined. + * This is for uthash developer only; it compiles away if HASH_DEBUG isn't defined. + */ +#ifdef HASH_DEBUG +#define HASH_OOPS(...) do { fprintf(stderr,__VA_ARGS__); exit(-1); } while (0) +#define HASH_FSCK(hh,head) \ +do { \ + unsigned _bkt_i; \ + unsigned _count, _bkt_count; \ + char *_prev; \ + struct UT_hash_handle *_thh; \ + if (head) { \ + _count = 0; \ + for( _bkt_i = 0; _bkt_i < (head)->hh.tbl->num_buckets; _bkt_i++) { \ + _bkt_count = 0; \ + _thh = (head)->hh.tbl->buckets[_bkt_i].hh_head; \ + _prev = NULL; \ + while (_thh) { \ + if (_prev != (char*)(_thh->hh_prev)) { \ + HASH_OOPS("invalid hh_prev %p, actual %p\n", \ + _thh->hh_prev, _prev ); \ + } \ + _bkt_count++; \ + _prev = (char*)(_thh); \ + _thh = _thh->hh_next; \ + } \ + _count += _bkt_count; \ + if ((head)->hh.tbl->buckets[_bkt_i].count != _bkt_count) { \ + HASH_OOPS("invalid bucket count %d, actual %d\n", \ + (head)->hh.tbl->buckets[_bkt_i].count, _bkt_count); \ + } \ + } \ + if (_count != (head)->hh.tbl->num_items) { \ + HASH_OOPS("invalid hh item count %d, actual %d\n", \ + (head)->hh.tbl->num_items, _count ); \ + } \ + /* traverse hh in app order; check next/prev integrity, count */ \ + _count = 0; \ + _prev = NULL; \ + _thh = &(head)->hh; \ + while (_thh) { \ + _count++; \ + if (_prev !=(char*)(_thh->prev)) { \ + HASH_OOPS("invalid prev %p, actual %p\n", \ + _thh->prev, _prev ); \ + } \ + _prev = (char*)ELMT_FROM_HH((head)->hh.tbl, _thh); \ + _thh = ( _thh->next ? (UT_hash_handle*)((char*)(_thh->next) + \ + (head)->hh.tbl->hho) : NULL ); \ + } \ + if (_count != (head)->hh.tbl->num_items) { \ + HASH_OOPS("invalid app item count %d, actual %d\n", \ + (head)->hh.tbl->num_items, _count ); \ + } \ + } \ +} while (0) +#else +#define HASH_FSCK(hh,head) +#endif + +/* When compiled with -DHASH_EMIT_KEYS, length-prefixed keys are emitted to + * the descriptor to which this macro is defined for tuning the hash function. + * The app can #include to get the prototype for write(2). */ +#ifdef HASH_EMIT_KEYS +#define HASH_EMIT_KEY(hh,head,keyptr,fieldlen) \ +do { \ + unsigned _klen = fieldlen; \ + write(HASH_EMIT_KEYS, &_klen, sizeof(_klen)); \ + write(HASH_EMIT_KEYS, keyptr, fieldlen); \ +} while (0) +#else +#define HASH_EMIT_KEY(hh,head,keyptr,fieldlen) +#endif + +/* default to Jenkin's hash unless overridden e.g. DHASH_FUNCTION=HASH_SAX */ +#ifdef HASH_FUNCTION +#define HASH_FCN HASH_FUNCTION +#else +#define HASH_FCN HASH_JEN +#endif + +/* The Bernstein hash function, used in Perl prior to v5.6 */ +#define HASH_BER(key,keylen,num_bkts,hashv,bkt) \ +do { \ + unsigned _hb_keylen=keylen; \ + char *_hb_key=(char*)(key); \ + (hashv) = 0; \ + while (_hb_keylen--) { (hashv) = ((hashv) * 33) + *_hb_key++; } \ + bkt = (hashv) & (num_bkts-1); \ +} while (0) + + +/* SAX/FNV/OAT/JEN hash functions are macro variants of those listed at + * http://eternallyconfuzzled.com/tuts/algorithms/jsw_tut_hashing.aspx */ +#define HASH_SAX(key,keylen,num_bkts,hashv,bkt) \ +do { \ + unsigned _sx_i; \ + char *_hs_key=(char*)(key); \ + hashv = 0; \ + for(_sx_i=0; _sx_i < keylen; _sx_i++) \ + hashv ^= (hashv << 5) + (hashv >> 2) + _hs_key[_sx_i]; \ + bkt = hashv & (num_bkts-1); \ +} while (0) + +#define HASH_FNV(key,keylen,num_bkts,hashv,bkt) \ +do { \ + unsigned _fn_i; \ + char *_hf_key=(char*)(key); \ + hashv = 2166136261UL; \ + for(_fn_i=0; _fn_i < keylen; _fn_i++) \ + hashv = (hashv * 16777619) ^ _hf_key[_fn_i]; \ + bkt = hashv & (num_bkts-1); \ +} while(0); + +#define HASH_OAT(key,keylen,num_bkts,hashv,bkt) \ +do { \ + unsigned _ho_i; \ + char *_ho_key=(char*)(key); \ + hashv = 0; \ + for(_ho_i=0; _ho_i < keylen; _ho_i++) { \ + hashv += _ho_key[_ho_i]; \ + hashv += (hashv << 10); \ + hashv ^= (hashv >> 6); \ + } \ + hashv += (hashv << 3); \ + hashv ^= (hashv >> 11); \ + hashv += (hashv << 15); \ + bkt = hashv & (num_bkts-1); \ +} while(0) + +#define HASH_JEN_MIX(a,b,c) \ +do { \ + a -= b; a -= c; a ^= ( c >> 13 ); \ + b -= c; b -= a; b ^= ( a << 8 ); \ + c -= a; c -= b; c ^= ( b >> 13 ); \ + a -= b; a -= c; a ^= ( c >> 12 ); \ + b -= c; b -= a; b ^= ( a << 16 ); \ + c -= a; c -= b; c ^= ( b >> 5 ); \ + a -= b; a -= c; a ^= ( c >> 3 ); \ + b -= c; b -= a; b ^= ( a << 10 ); \ + c -= a; c -= b; c ^= ( b >> 15 ); \ +} while (0) + +#define HASH_JEN(key,keylen,num_bkts,hashv,bkt) \ +do { \ + unsigned _hj_i,_hj_j,_hj_k; \ + char *_hj_key=(char*)(key); \ + hashv = 0xfeedbeef; \ + _hj_i = _hj_j = 0x9e3779b9; \ + _hj_k = keylen; \ + while (_hj_k >= 12) { \ + _hj_i += (_hj_key[0] + ( (unsigned)_hj_key[1] << 8 ) \ + + ( (unsigned)_hj_key[2] << 16 ) \ + + ( (unsigned)_hj_key[3] << 24 ) ); \ + _hj_j += (_hj_key[4] + ( (unsigned)_hj_key[5] << 8 ) \ + + ( (unsigned)_hj_key[6] << 16 ) \ + + ( (unsigned)_hj_key[7] << 24 ) ); \ + hashv += (_hj_key[8] + ( (unsigned)_hj_key[9] << 8 ) \ + + ( (unsigned)_hj_key[10] << 16 ) \ + + ( (unsigned)_hj_key[11] << 24 ) ); \ + \ + HASH_JEN_MIX(_hj_i, _hj_j, hashv); \ + \ + _hj_key += 12; \ + _hj_k -= 12; \ + } \ + hashv += keylen; \ + switch ( _hj_k ) { \ + case 11: hashv += ( (unsigned)_hj_key[10] << 24 ); \ + case 10: hashv += ( (unsigned)_hj_key[9] << 16 ); \ + case 9: hashv += ( (unsigned)_hj_key[8] << 8 ); \ + case 8: _hj_j += ( (unsigned)_hj_key[7] << 24 ); \ + case 7: _hj_j += ( (unsigned)_hj_key[6] << 16 ); \ + case 6: _hj_j += ( (unsigned)_hj_key[5] << 8 ); \ + case 5: _hj_j += _hj_key[4]; \ + case 4: _hj_i += ( (unsigned)_hj_key[3] << 24 ); \ + case 3: _hj_i += ( (unsigned)_hj_key[2] << 16 ); \ + case 2: _hj_i += ( (unsigned)_hj_key[1] << 8 ); \ + case 1: _hj_i += _hj_key[0]; \ + } \ + HASH_JEN_MIX(_hj_i, _hj_j, hashv); \ + bkt = hashv & (num_bkts-1); \ +} while(0) + +/* The Paul Hsieh hash function */ +#undef get16bits +#if (defined(__GNUC__) && defined(__i386__)) || defined(__WATCOMC__) \ + || defined(_MSC_VER) || defined (__BORLANDC__) || defined (__TURBOC__) +#define get16bits(d) (*((const uint16_t *) (d))) +#endif + +#if !defined (get16bits) +#define get16bits(d) ((((uint32_t)(((const uint8_t *)(d))[1])) << 8) \ + +(uint32_t)(((const uint8_t *)(d))[0]) ) +#endif +#define HASH_SFH(key,keylen,num_bkts,hashv,bkt) \ +do { \ + char *_sfh_key=(char*)(key); \ + uint32_t _sfh_tmp, _sfh_len = keylen; \ + \ + int _sfh_rem = _sfh_len & 3; \ + _sfh_len >>= 2; \ + hashv = 0xcafebabe; \ + \ + /* Main loop */ \ + for (;_sfh_len > 0; _sfh_len--) { \ + hashv += get16bits (_sfh_key); \ + _sfh_tmp = (get16bits (_sfh_key+2) << 11) ^ hashv; \ + hashv = (hashv << 16) ^ _sfh_tmp; \ + _sfh_key += 2*sizeof (uint16_t); \ + hashv += hashv >> 11; \ + } \ + \ + /* Handle end cases */ \ + switch (_sfh_rem) { \ + case 3: hashv += get16bits (_sfh_key); \ + hashv ^= hashv << 16; \ + hashv ^= _sfh_key[sizeof (uint16_t)] << 18; \ + hashv += hashv >> 11; \ + break; \ + case 2: hashv += get16bits (_sfh_key); \ + hashv ^= hashv << 11; \ + hashv += hashv >> 17; \ + break; \ + case 1: hashv += *_sfh_key; \ + hashv ^= hashv << 10; \ + hashv += hashv >> 1; \ + } \ + \ + /* Force "avalanching" of final 127 bits */ \ + hashv ^= hashv << 3; \ + hashv += hashv >> 5; \ + hashv ^= hashv << 4; \ + hashv += hashv >> 17; \ + hashv ^= hashv << 25; \ + hashv += hashv >> 6; \ + bkt = hashv & (num_bkts-1); \ +} while(0); + +#ifdef HASH_USING_NO_STRICT_ALIASING +/* The MurmurHash exploits some CPU's (x86,x86_64) tolerance for unaligned reads. + * For other types of CPU's (e.g. Sparc) an unaligned read causes a bus error. + * MurmurHash uses the faster approach only on CPU's where we know it's safe. + * + * Note the preprocessor built-in defines can be emitted using: + * + * gcc -m64 -dM -E - < /dev/null (on gcc) + * cc -## a.c (where a.c is a simple test file) (Sun Studio) + */ +#if (defined(__i386__) || defined(__x86_64__)) +#define MUR_GETBLOCK(p,i) p[i] +#else /* non intel */ +#define MUR_PLUS0_ALIGNED(p) (((unsigned long)p & 0x3) == 0) +#define MUR_PLUS1_ALIGNED(p) (((unsigned long)p & 0x3) == 1) +#define MUR_PLUS2_ALIGNED(p) (((unsigned long)p & 0x3) == 2) +#define MUR_PLUS3_ALIGNED(p) (((unsigned long)p & 0x3) == 3) +#define WP(p) ((uint32_t*)((unsigned long)(p) & ~3UL)) +#if (defined(__BIG_ENDIAN__) || defined(SPARC) || defined(__ppc__) || defined(__ppc64__)) +#define MUR_THREE_ONE(p) ((((*WP(p))&0x00ffffff) << 8) | (((*(WP(p)+1))&0xff000000) >> 24)) +#define MUR_TWO_TWO(p) ((((*WP(p))&0x0000ffff) <<16) | (((*(WP(p)+1))&0xffff0000) >> 16)) +#define MUR_ONE_THREE(p) ((((*WP(p))&0x000000ff) <<24) | (((*(WP(p)+1))&0xffffff00) >> 8)) +#else /* assume little endian non-intel */ +#define MUR_THREE_ONE(p) ((((*WP(p))&0xffffff00) >> 8) | (((*(WP(p)+1))&0x000000ff) << 24)) +#define MUR_TWO_TWO(p) ((((*WP(p))&0xffff0000) >>16) | (((*(WP(p)+1))&0x0000ffff) << 16)) +#define MUR_ONE_THREE(p) ((((*WP(p))&0xff000000) >>24) | (((*(WP(p)+1))&0x00ffffff) << 8)) +#endif +#define MUR_GETBLOCK(p,i) (MUR_PLUS0_ALIGNED(p) ? ((p)[i]) : \ + (MUR_PLUS1_ALIGNED(p) ? MUR_THREE_ONE(p) : \ + (MUR_PLUS2_ALIGNED(p) ? MUR_TWO_TWO(p) : \ + MUR_ONE_THREE(p)))) +#endif +#define MUR_ROTL32(x,r) (((x) << (r)) | ((x) >> (32 - (r)))) +#define MUR_FMIX(_h) \ +do { \ + _h ^= _h >> 16; \ + _h *= 0x85ebca6b; \ + _h ^= _h >> 13; \ + _h *= 0xc2b2ae35l; \ + _h ^= _h >> 16; \ +} while(0) + +#define HASH_MUR(key,keylen,num_bkts,hashv,bkt) \ +do { \ + const uint8_t *_mur_data = (const uint8_t*)(key); \ + const int _mur_nblocks = (keylen) / 4; \ + uint32_t _mur_h1 = 0xf88D5353; \ + uint32_t _mur_c1 = 0xcc9e2d51; \ + uint32_t _mur_c2 = 0x1b873593; \ + const uint32_t *_mur_blocks = (const uint32_t*)(_mur_data+_mur_nblocks*4); \ + int _mur_i; \ + for(_mur_i = -_mur_nblocks; _mur_i; _mur_i++) { \ + uint32_t _mur_k1 = MUR_GETBLOCK(_mur_blocks,_mur_i); \ + _mur_k1 *= _mur_c1; \ + _mur_k1 = MUR_ROTL32(_mur_k1,15); \ + _mur_k1 *= _mur_c2; \ + \ + _mur_h1 ^= _mur_k1; \ + _mur_h1 = MUR_ROTL32(_mur_h1,13); \ + _mur_h1 = _mur_h1*5+0xe6546b64; \ + } \ + const uint8_t *_mur_tail = (const uint8_t*)(_mur_data + _mur_nblocks*4); \ + uint32_t _mur_k1=0; \ + switch((keylen) & 3) { \ + case 3: _mur_k1 ^= _mur_tail[2] << 16; \ + case 2: _mur_k1 ^= _mur_tail[1] << 8; \ + case 1: _mur_k1 ^= _mur_tail[0]; \ + _mur_k1 *= _mur_c1; \ + _mur_k1 = MUR_ROTL32(_mur_k1,15); \ + _mur_k1 *= _mur_c2; \ + _mur_h1 ^= _mur_k1; \ + } \ + _mur_h1 ^= (keylen); \ + MUR_FMIX(_mur_h1); \ + hashv = _mur_h1; \ + bkt = hashv & (num_bkts-1); \ +} while(0) +#endif /* HASH_USING_NO_STRICT_ALIASING */ + +/* key comparison function; return 0 if keys equal */ +#define HASH_KEYCMP(a,b,len) memcmp(a,b,len) + +/* iterate over items in a known bucket to find desired item */ +#define HASH_FIND_IN_BKT(tbl,hh,head,keyptr,keylen_in,out) \ +do { \ + if (head.hh_head) DECLTYPE_ASSIGN(out,ELMT_FROM_HH(tbl,head.hh_head)); \ + else out=NULL; \ + while (out) { \ + if (out->hh.keylen == keylen_in) { \ + if ((HASH_KEYCMP(out->hh.key,keyptr,keylen_in)) == 0) break; \ + } \ + if (out->hh.hh_next) DECLTYPE_ASSIGN(out,ELMT_FROM_HH(tbl,out->hh.hh_next)); \ + else out = NULL; \ + } \ +} while(0) + +/* add an item to a bucket */ +#define HASH_ADD_TO_BKT(head,addhh) \ +do { \ + head.count++; \ + (addhh)->hh_next = head.hh_head; \ + (addhh)->hh_prev = NULL; \ + if (head.hh_head) { (head).hh_head->hh_prev = (addhh); } \ + (head).hh_head=addhh; \ + if (head.count >= ((head.expand_mult+1) * HASH_BKT_CAPACITY_THRESH) \ + && (addhh)->tbl->noexpand != 1) { \ + HASH_EXPAND_BUCKETS((addhh)->tbl); \ + } \ +} while(0) + +/* remove an item from a given bucket */ +#define HASH_DEL_IN_BKT(hh,head,hh_del) \ + (head).count--; \ + if ((head).hh_head == hh_del) { \ + (head).hh_head = hh_del->hh_next; \ + } \ + if (hh_del->hh_prev) { \ + hh_del->hh_prev->hh_next = hh_del->hh_next; \ + } \ + if (hh_del->hh_next) { \ + hh_del->hh_next->hh_prev = hh_del->hh_prev; \ + } + +/* Bucket expansion has the effect of doubling the number of buckets + * and redistributing the items into the new buckets. Ideally the + * items will distribute more or less evenly into the new buckets + * (the extent to which this is true is a measure of the quality of + * the hash function as it applies to the key domain). + * + * With the items distributed into more buckets, the chain length + * (item count) in each bucket is reduced. Thus by expanding buckets + * the hash keeps a bound on the chain length. This bounded chain + * length is the essence of how a hash provides constant time lookup. + * + * The calculation of tbl->ideal_chain_maxlen below deserves some + * explanation. First, keep in mind that we're calculating the ideal + * maximum chain length based on the *new* (doubled) bucket count. + * In fractions this is just n/b (n=number of items,b=new num buckets). + * Since the ideal chain length is an integer, we want to calculate + * ceil(n/b). We don't depend on floating point arithmetic in this + * hash, so to calculate ceil(n/b) with integers we could write + * + * ceil(n/b) = (n/b) + ((n%b)?1:0) + * + * and in fact a previous version of this hash did just that. + * But now we have improved things a bit by recognizing that b is + * always a power of two. We keep its base 2 log handy (call it lb), + * so now we can write this with a bit shift and logical AND: + * + * ceil(n/b) = (n>>lb) + ( (n & (b-1)) ? 1:0) + * + */ +#define HASH_EXPAND_BUCKETS(tbl) \ +do { \ + unsigned _he_bkt; \ + unsigned _he_bkt_i; \ + struct UT_hash_handle *_he_thh, *_he_hh_nxt; \ + UT_hash_bucket *_he_new_buckets, *_he_newbkt; \ + _he_new_buckets = (UT_hash_bucket*)uthash_malloc( \ + 2 * tbl->num_buckets * sizeof(struct UT_hash_bucket)); \ + if (!_he_new_buckets) { uthash_fatal( "out of memory"); } \ + memset(_he_new_buckets, 0, \ + 2 * tbl->num_buckets * sizeof(struct UT_hash_bucket)); \ + tbl->ideal_chain_maxlen = \ + (tbl->num_items >> (tbl->log2_num_buckets+1)) + \ + ((tbl->num_items & ((tbl->num_buckets*2)-1)) ? 1 : 0); \ + tbl->nonideal_items = 0; \ + for(_he_bkt_i = 0; _he_bkt_i < tbl->num_buckets; _he_bkt_i++) \ + { \ + _he_thh = tbl->buckets[ _he_bkt_i ].hh_head; \ + while (_he_thh) { \ + _he_hh_nxt = _he_thh->hh_next; \ + HASH_TO_BKT( _he_thh->hashv, tbl->num_buckets*2, _he_bkt); \ + _he_newbkt = &(_he_new_buckets[ _he_bkt ]); \ + if (++(_he_newbkt->count) > tbl->ideal_chain_maxlen) { \ + tbl->nonideal_items++; \ + _he_newbkt->expand_mult = _he_newbkt->count / \ + tbl->ideal_chain_maxlen; \ + } \ + _he_thh->hh_prev = NULL; \ + _he_thh->hh_next = _he_newbkt->hh_head; \ + if (_he_newbkt->hh_head) _he_newbkt->hh_head->hh_prev = \ + _he_thh; \ + _he_newbkt->hh_head = _he_thh; \ + _he_thh = _he_hh_nxt; \ + } \ + } \ + uthash_free( tbl->buckets, tbl->num_buckets*sizeof(struct UT_hash_bucket) ); \ + tbl->num_buckets *= 2; \ + tbl->log2_num_buckets++; \ + tbl->buckets = _he_new_buckets; \ + tbl->ineff_expands = (tbl->nonideal_items > (tbl->num_items >> 1)) ? \ + (tbl->ineff_expands+1) : 0; \ + if (tbl->ineff_expands > 1) { \ + tbl->noexpand=1; \ + uthash_noexpand_fyi(tbl); \ + } \ + uthash_expand_fyi(tbl); \ +} while(0) + + +/* This is an adaptation of Simon Tatham's O(n log(n)) mergesort */ +/* Note that HASH_SORT assumes the hash handle name to be hh. + * HASH_SRT was added to allow the hash handle name to be passed in. */ +#define HASH_SORT(head,cmpfcn) HASH_SRT(hh,head,cmpfcn) +#define HASH_SRT(hh,head,cmpfcn) \ +do { \ + unsigned _hs_i; \ + unsigned _hs_looping,_hs_nmerges,_hs_insize,_hs_psize,_hs_qsize; \ + struct UT_hash_handle *_hs_p, *_hs_q, *_hs_e, *_hs_list, *_hs_tail; \ + if (head) { \ + _hs_insize = 1; \ + _hs_looping = 1; \ + _hs_list = &((head)->hh); \ + while (_hs_looping) { \ + _hs_p = _hs_list; \ + _hs_list = NULL; \ + _hs_tail = NULL; \ + _hs_nmerges = 0; \ + while (_hs_p) { \ + _hs_nmerges++; \ + _hs_q = _hs_p; \ + _hs_psize = 0; \ + for ( _hs_i = 0; _hs_i < _hs_insize; _hs_i++ ) { \ + _hs_psize++; \ + _hs_q = (UT_hash_handle*)((_hs_q->next) ? \ + ((void*)((char*)(_hs_q->next) + \ + (head)->hh.tbl->hho)) : NULL); \ + if (! (_hs_q) ) break; \ + } \ + _hs_qsize = _hs_insize; \ + while ((_hs_psize > 0) || ((_hs_qsize > 0) && _hs_q )) { \ + if (_hs_psize == 0) { \ + _hs_e = _hs_q; \ + _hs_q = (UT_hash_handle*)((_hs_q->next) ? \ + ((void*)((char*)(_hs_q->next) + \ + (head)->hh.tbl->hho)) : NULL); \ + _hs_qsize--; \ + } else if ( (_hs_qsize == 0) || !(_hs_q) ) { \ + _hs_e = _hs_p; \ + _hs_p = (UT_hash_handle*)((_hs_p->next) ? \ + ((void*)((char*)(_hs_p->next) + \ + (head)->hh.tbl->hho)) : NULL); \ + _hs_psize--; \ + } else if (( \ + cmpfcn(DECLTYPE(head)(ELMT_FROM_HH((head)->hh.tbl,_hs_p)), \ + DECLTYPE(head)(ELMT_FROM_HH((head)->hh.tbl,_hs_q))) \ + ) <= 0) { \ + _hs_e = _hs_p; \ + _hs_p = (UT_hash_handle*)((_hs_p->next) ? \ + ((void*)((char*)(_hs_p->next) + \ + (head)->hh.tbl->hho)) : NULL); \ + _hs_psize--; \ + } else { \ + _hs_e = _hs_q; \ + _hs_q = (UT_hash_handle*)((_hs_q->next) ? \ + ((void*)((char*)(_hs_q->next) + \ + (head)->hh.tbl->hho)) : NULL); \ + _hs_qsize--; \ + } \ + if ( _hs_tail ) { \ + _hs_tail->next = ((_hs_e) ? \ + ELMT_FROM_HH((head)->hh.tbl,_hs_e) : NULL); \ + } else { \ + _hs_list = _hs_e; \ + } \ + _hs_e->prev = ((_hs_tail) ? \ + ELMT_FROM_HH((head)->hh.tbl,_hs_tail) : NULL); \ + _hs_tail = _hs_e; \ + } \ + _hs_p = _hs_q; \ + } \ + _hs_tail->next = NULL; \ + if ( _hs_nmerges <= 1 ) { \ + _hs_looping=0; \ + (head)->hh.tbl->tail = _hs_tail; \ + DECLTYPE_ASSIGN(head,ELMT_FROM_HH((head)->hh.tbl, _hs_list)); \ + } \ + _hs_insize *= 2; \ + } \ + HASH_FSCK(hh,head); \ + } \ +} while (0) + +/* This function selects items from one hash into another hash. + * The end result is that the selected items have dual presence + * in both hashes. There is no copy of the items made; rather + * they are added into the new hash through a secondary hash + * hash handle that must be present in the structure. */ +#define HASH_SELECT(hh_dst, dst, hh_src, src, cond) \ +do { \ + unsigned _src_bkt, _dst_bkt; \ + void *_last_elt=NULL, *_elt; \ + UT_hash_handle *_src_hh, *_dst_hh, *_last_elt_hh=NULL; \ + ptrdiff_t _dst_hho = ((char*)(&(dst)->hh_dst) - (char*)(dst)); \ + if (src) { \ + for(_src_bkt=0; _src_bkt < (src)->hh_src.tbl->num_buckets; _src_bkt++) { \ + for(_src_hh = (src)->hh_src.tbl->buckets[_src_bkt].hh_head; \ + _src_hh; \ + _src_hh = _src_hh->hh_next) { \ + _elt = ELMT_FROM_HH((src)->hh_src.tbl, _src_hh); \ + if (cond(_elt)) { \ + _dst_hh = (UT_hash_handle*)(((char*)_elt) + _dst_hho); \ + _dst_hh->key = _src_hh->key; \ + _dst_hh->keylen = _src_hh->keylen; \ + _dst_hh->hashv = _src_hh->hashv; \ + _dst_hh->prev = _last_elt; \ + _dst_hh->next = NULL; \ + if (_last_elt_hh) { _last_elt_hh->next = _elt; } \ + if (!dst) { \ + DECLTYPE_ASSIGN(dst,_elt); \ + HASH_MAKE_TABLE(hh_dst,dst); \ + } else { \ + _dst_hh->tbl = (dst)->hh_dst.tbl; \ + } \ + HASH_TO_BKT(_dst_hh->hashv, _dst_hh->tbl->num_buckets, _dst_bkt); \ + HASH_ADD_TO_BKT(_dst_hh->tbl->buckets[_dst_bkt],_dst_hh); \ + (dst)->hh_dst.tbl->num_items++; \ + _last_elt = _elt; \ + _last_elt_hh = _dst_hh; \ + } \ + } \ + } \ + } \ + HASH_FSCK(hh_dst,dst); \ +} while (0) + +#define HASH_CLEAR(hh,head) \ +do { \ + if (head) { \ + uthash_free((head)->hh.tbl->buckets, \ + (head)->hh.tbl->num_buckets*sizeof(struct UT_hash_bucket)); \ + uthash_free((head)->hh.tbl, sizeof(UT_hash_table)); \ + (head)=NULL; \ + } \ +} while(0) + +#ifdef NO_DECLTYPE +#define HASH_ITER(hh,head,el,tmp) \ +for((el)=(head), (*(char**)(&(tmp)))=(char*)((head)?(head)->hh.next:NULL); \ + el; (el)=(tmp),(*(char**)(&(tmp)))=(char*)((tmp)?(tmp)->hh.next:NULL)) +#else +#define HASH_ITER(hh,head,el,tmp) \ +for((el)=(head),(tmp)=DECLTYPE(el)((head)?(head)->hh.next:NULL); \ + el; (el)=(tmp),(tmp)=DECLTYPE(el)((tmp)?(tmp)->hh.next:NULL)) +#endif + +/* obtain a count of items in the hash */ +#define HASH_COUNT(head) HASH_CNT(hh,head) +#define HASH_CNT(hh,head) ((head)?((head)->hh.tbl->num_items):0) + +typedef struct UT_hash_bucket { + struct UT_hash_handle *hh_head; + unsigned count; + + /* expand_mult is normally set to 0. In this situation, the max chain length + * threshold is enforced at its default value, HASH_BKT_CAPACITY_THRESH. (If + * the bucket's chain exceeds this length, bucket expansion is triggered). + * However, setting expand_mult to a non-zero value delays bucket expansion + * (that would be triggered by additions to this particular bucket) + * until its chain length reaches a *multiple* of HASH_BKT_CAPACITY_THRESH. + * (The multiplier is simply expand_mult+1). The whole idea of this + * multiplier is to reduce bucket expansions, since they are expensive, in + * situations where we know that a particular bucket tends to be overused. + * It is better to let its chain length grow to a longer yet-still-bounded + * value, than to do an O(n) bucket expansion too often. + */ + unsigned expand_mult; + +} UT_hash_bucket; + +/* random signature used only to find hash tables in external analysis */ +#define HASH_SIGNATURE 0xa0111fe1 +#define HASH_BLOOM_SIGNATURE 0xb12220f2 + +typedef struct UT_hash_table { + UT_hash_bucket *buckets; + unsigned num_buckets, log2_num_buckets; + unsigned num_items; + struct UT_hash_handle *tail; /* tail hh in app order, for fast append */ + ptrdiff_t hho; /* hash handle offset (byte pos of hash handle in element */ + + /* in an ideal situation (all buckets used equally), no bucket would have + * more than ceil(#items/#buckets) items. that's the ideal chain length. */ + unsigned ideal_chain_maxlen; + + /* nonideal_items is the number of items in the hash whose chain position + * exceeds the ideal chain maxlen. these items pay the penalty for an uneven + * hash distribution; reaching them in a chain traversal takes >ideal steps */ + unsigned nonideal_items; + + /* ineffective expands occur when a bucket doubling was performed, but + * afterward, more than half the items in the hash had nonideal chain + * positions. If this happens on two consecutive expansions we inhibit any + * further expansion, as it's not helping; this happens when the hash + * function isn't a good fit for the key domain. When expansion is inhibited + * the hash will still work, albeit no longer in constant time. */ + unsigned ineff_expands, noexpand; + + uint32_t signature; /* used only to find hash tables in external analysis */ +#ifdef HASH_BLOOM + uint32_t bloom_sig; /* used only to test bloom exists in external analysis */ + uint8_t *bloom_bv; + char bloom_nbits; +#endif + +} UT_hash_table; + +typedef struct UT_hash_handle { + struct UT_hash_table *tbl; + void *prev; /* prev element in app order */ + void *next; /* next element in app order */ + struct UT_hash_handle *hh_prev; /* previous hh in bucket order */ + struct UT_hash_handle *hh_next; /* next hh in bucket order */ + void *key; /* ptr to enclosing struct's key */ + unsigned keylen; /* enclosing struct's key len */ + unsigned hashv; /* result of hash-fcn(key) */ +} UT_hash_handle; + +#endif /* UTHASH_H */ diff --git a/rocclr/compiler/lib/loaders/elf/utils/libelf/README b/rocclr/compiler/lib/loaders/elf/utils/libelf/README new file mode 100644 index 0000000000..726fcc92e4 --- /dev/null +++ b/rocclr/compiler/lib/loaders/elf/utils/libelf/README @@ -0,0 +1,12 @@ +# $FreeBSD$ +# $NetBSD$ + +libelf: a BSD-licensed implementation of the ELF(3)/GELF(3) API. + +Documentation: + * Manual page elf.3 contains an overview of the library. Other + manual pages document individual APIs in the library. + * A tutorial "libelf by Example" is available at: + http://people.freebsd.org/~jkoshy/download/libelf/article.html + +For ongoing development please see http://elftoolchain.sourceforge.net/ diff --git a/rocclr/compiler/lib/loaders/elf/utils/libelf/README.build b/rocclr/compiler/lib/loaders/elf/utils/libelf/README.build new file mode 100644 index 0000000000..6fc04c64e9 --- /dev/null +++ b/rocclr/compiler/lib/loaders/elf/utils/libelf/README.build @@ -0,0 +1,64 @@ +: README.build -- notes on the build process + +The "Makefile" in this directory uses BSD make(1) syntax. If you are +trying to build this library on a platform that does not have a +pre-built BSD compatible make(1), then you could try porting NetBSD's +make(1). NetBSD's make(1) is available at: + + http://www.crufty.net/help/sjg/bmake.html + + +: Supporting cross builds + +In the general case, libelf may be built for a target operating system +and machine architecture that is different from the host operating +system and machine architecture that the compilation is happening on. +For example, compilation could be running on a Linux/i386 host, with +target binaries being created for a NetBSD/sparc64 system. + +To support cross building: +- The top-level "Makefile" pulls in the appropriate make rules for the + target system. + + Inside of makefiles, we determine the target OS by looking at the + contents of the ${unix} make variable. The top-level makefile then + includes any target specific makefiles if they exist. + +- Operating systems differ in the names and locations of the headers + where their ELF types are defined. They also differ in the set of + ELF types supported. Inside of libelf's implementation these + differences are abstracted out by the auxiliary header + "_libelf_config.h". + + +: OS Specific Configuration : + +:: Debian :: + +The following packages are needed for the build: +- `build-essential' +- `m4' +- `freebsd-buildutils' or `freebsd5-buildutils' + +You would need to use `freebsd-make' instead of GNU make to build +the tools. You would also need to place /usr/lib/freebsd in the +shell's `PATH', preferably at the beginning. + +:: FreeBSD :: + +libelf should build out of the box on FreeBSD versions later than 6.0. + +:: NetBSD :: + +libelf should build out of the box on NetBSD versions later than 4.0. + +:: Ubuntu :: + +See the section on 'Debian' above. + + +: Porting resources on the 'net + +The 'predef' project [http://predef.sourceforge.net/] has a +comprehensive list of CPP macros predefined by various OSes. + diff --git a/rocclr/compiler/lib/loaders/elf/utils/libelf/Version.map b/rocclr/compiler/lib/loaders/elf/utils/libelf/Version.map new file mode 100644 index 0000000000..2c595eafba --- /dev/null +++ b/rocclr/compiler/lib/loaders/elf/utils/libelf/Version.map @@ -0,0 +1,97 @@ +/* + * $Id: Version.map 2033 2011-10-23 09:21:13Z jkoshy $ + * + * $FreeBSD: src/lib/libelf/Version.map,v 1.3 2007/04/29 14:05:22 deischen Exp $ + */ +FBSD_1.0 { +global: + elf32_checksum; + elf32_fsize; + elf32_getehdr; + elf32_getphdr; + elf32_getshdr; + elf32_newehdr; + elf32_newphdr; + elf32_xlatetof; + elf32_xlatetom; + elf64_checksum; + elf64_fsize; + elf64_getehdr; + elf64_getphdr; + elf64_getshdr; + elf64_newehdr; + elf64_newphdr; + elf64_xlatetof; + elf64_xlatetom; + elf_begin; + elf_cntl; + elf_end; + elf_errmsg; + elf_errno; + elf_fill; + elf_flagarhdr; + elf_flagdata; + elf_flagehdr; + elf_flagelf; + elf_flagphdr; + elf_flagscn; + elf_flagshdr; + elf_getarhdr; + elf_getarsym; + elf_getbase; + elf_getdata; + elf_getident; + elf_getscn; + elf_getphdrnum; + elf_getphnum; + elf_getshdrnum; + elf_getshnum; + elf_getshdrstrndx; + elf_getshstrndx; + elf_hash; + elf_kind; + elf_memory; + elf_ndxscn; + elf_newdata; + elf_newscn; + elf_next; + elf_nextscn; + elf_rand; + elf_rawdata; + elf_rawfile; + elf_setshstrndx; + elf_strptr; + elf_update; + elf_version; + gelf_checksum; + gelf_fsize; + gelf_getcap; + gelf_getclass; + gelf_getdyn; + gelf_getehdr; + gelf_getmove; + gelf_getphdr; + gelf_getrel; + gelf_getrela; + gelf_getshdr; + gelf_getsym; + gelf_getsyminfo; + gelf_getsymshndx; + gelf_newehdr; + gelf_newphdr; + gelf_update_cap; + gelf_update_dyn; + gelf_update_ehdr; + gelf_update_move; + gelf_update_phdr; + gelf_update_rel; + gelf_update_rela; + gelf_update_shdr; + gelf_update_sym; + gelf_update_syminfo; + gelf_update_symshndx; + gelf_xlatetof; + gelf_xlatetom; +local: + *; +}; diff --git a/rocclr/compiler/lib/loaders/elf/utils/libelf/_libelf.h b/rocclr/compiler/lib/loaders/elf/utils/libelf/_libelf.h new file mode 100644 index 0000000000..23d21b3661 --- /dev/null +++ b/rocclr/compiler/lib/loaders/elf/utils/libelf/_libelf.h @@ -0,0 +1,217 @@ +/*- + * Copyright (c) 2006,2008-2011 Joseph Koshy + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * $Id: _libelf.h 1921 2011-09-23 08:04:02Z jkoshy $ + */ + +#ifndef __LIBELF_H_ +#define __LIBELF_H_ + +#include + +#include "_libelf_config.h" + +#include "_elftc.h" + +/* + * Library-private data structures. + */ + +#define LIBELF_MSG_SIZE 256 + +struct _libelf_globals { + int libelf_arch; + unsigned int libelf_byteorder; + int libelf_class; + int libelf_error; + int libelf_fillchar; + unsigned int libelf_version; + char libelf_msg[LIBELF_MSG_SIZE]; +}; + +extern struct _libelf_globals _libelf; + +#define LIBELF_PRIVATE(N) (_libelf.libelf_##N) + +#define LIBELF_ELF_ERROR_MASK 0xFF +#define LIBELF_OS_ERROR_SHIFT 8 + +#define LIBELF_SET_ERROR(E, O) do { \ + LIBELF_PRIVATE(error) = ((ELF_E_##E & LIBELF_ELF_ERROR_MASK)| \ + ((O) << LIBELF_OS_ERROR_SHIFT)); \ + } while (0) + +#define LIBELF_ADJUST_AR_SIZE(S) (((S) + 1U) & ~1U) + +/* + * Flags for library internal use. These use the upper 16 bits of the + * `e_flags' field. + */ +#define LIBELF_F_API_MASK 0x00FFFF /* Flags defined by the API. */ +#define LIBELF_F_AR_HEADER 0x010000 /* translated header available */ +#define LIBELF_F_AR_VARIANT_SVR4 0x020000 /* BSD style ar(1) archive */ +#define LIBELF_F_DATA_MALLOCED 0x040000 /* whether data was malloc'ed */ +#define LIBELF_F_RAWFILE_MALLOC 0x080000 /* whether e_rawfile was malloc'ed */ +#define LIBELF_F_RAWFILE_MMAP 0x100000 /* whether e_rawfile was mmap'ed */ +#define LIBELF_F_SHDRS_LOADED 0x200000 /* whether all shdrs were read in */ +#define LIBELF_F_SPECIAL_FILE 0x400000 /* non-regular file */ +struct _Elf_Mem { + void (*dealloc)(void*); + void* (*alloc)(size_t); +}; + +struct _Elf { + int e_activations; /* activation count */ + unsigned int e_byteorder; /* ELFDATA* */ + int e_class; /* ELFCLASS* */ + Elf_Cmd e_cmd; /* ELF_C_* used at creation time */ + int e_fd; /* associated file descriptor */ + unsigned int e_flags; /* ELF_F_* & LIBELF_F_* flags */ + Elf_Kind e_kind; /* ELF_K_* */ + Elf *e_parent; /* non-NULL for archive members */ + char *e_rawfile; /* uninterpreted bytes */ + size_t e_rawsize; /* size of uninterpreted bytes */ + unsigned int e_version; /* file version */ + /* AMD Memory interface */ + struct _Elf_Mem e_mem; + + /* + * Header information for archive members. See the + * LIBELF_F_AR_HEADER flag. + */ + union { + Elf_Arhdr *e_arhdr; /* translated header */ + char *e_rawhdr; /* untranslated header */ + } e_hdr; + + union { + struct { /* ar(1) archives */ + off_t e_next; /* set by elf_rand()/elf_next() */ + int e_nchildren; + char *e_rawstrtab; /* file name strings */ + size_t e_rawstrtabsz; + char *e_rawsymtab; /* symbol table */ + size_t e_rawsymtabsz; + Elf_Arsym *e_symtab; + size_t e_symtabsz; + } e_ar; + struct { /* regular ELF files */ + union { + Elf32_Ehdr *e_ehdr32; + Elf64_Ehdr *e_ehdr64; + } e_ehdr; + union { + Elf32_Phdr *e_phdr32; + Elf64_Phdr *e_phdr64; + } e_phdr; + STAILQ_HEAD(, _Elf_Scn) e_scn; /* section list */ + size_t e_nphdr; /* number of Phdr entries */ + size_t e_nscn; /* number of sections */ + size_t e_strndx; /* string table section index */ + } e_elf; + } e_u; +}; + +struct _Elf_Scn { + union { + Elf32_Shdr s_shdr32; + Elf64_Shdr s_shdr64; + } s_shdr; + STAILQ_HEAD(, _Elf_Data) s_data; /* list of Elf_Data descriptors */ + STAILQ_HEAD(, _Elf_Data) s_rawdata; /* raw data for this section */ + STAILQ_ENTRY(_Elf_Scn) s_next; + struct _Elf *s_elf; /* parent ELF descriptor */ + unsigned int s_flags; /* flags for the section as a whole */ + size_t s_ndx; /* index# for this section */ + uint64_t s_offset; /* managed by elf_update() */ + uint64_t s_rawoff; /* original offset in the file */ + uint64_t s_size; /* managed by elf_update() */ +}; + + +enum { + ELF_TOFILE, + ELF_TOMEMORY +}; + +#define LIBELF_COPY_U32(DST,SRC,NAME) do { \ + if ((SRC)->NAME > UINT_MAX) { \ + LIBELF_SET_ERROR(RANGE, 0); \ + return (0); \ + } \ + (DST)->NAME = (SRC)->NAME; \ + } while (0) + +#define LIBELF_COPY_S32(DST,SRC,NAME) do { \ + if ((SRC)->NAME > INT_MAX || \ + (SRC)->NAME < INT_MIN) { \ + LIBELF_SET_ERROR(RANGE, 0); \ + return (0); \ + } \ + (DST)->NAME = (SRC)->NAME; \ + } while (0) + + +/* + * Function Prototypes. + */ + +__BEGIN_DECLS +Elf_Data *_libelf_allocate_data(Elf_Scn *_s); +Elf *_libelf_allocate_elf(Elf_Mem *mem); +Elf_Scn *_libelf_allocate_scn(Elf *_e, size_t _ndx); +Elf_Arhdr *_libelf_ar_gethdr(Elf *_e); +Elf *_libelf_ar_open(Elf *_e); +Elf *_libelf_ar_open_member(int _fd, Elf_Cmd _c, Elf *_ar, Elf_Mem *mem); +int _libelf_ar_get_member(char *_s, size_t _sz, int _base, size_t *_ret); +Elf_Arsym *_libelf_ar_process_bsd_symtab(Elf *_ar, size_t *_dst); +Elf_Arsym *_libelf_ar_process_svr4_symtab(Elf *_ar, size_t *_dst); +unsigned long _libelf_checksum(Elf *_e, int _elfclass); +void *_libelf_ehdr(Elf *_e, int _elfclass, int _allocate); +int _libelf_falign(Elf_Type _t, int _elfclass); +size_t _libelf_fsize(Elf_Type _t, int _elfclass, unsigned int _version, + size_t count); +int (*_libelf_get_translator(Elf_Type _t, int _direction, int _elfclass)) + (char *_dst, size_t dsz, char *_src, size_t _cnt, int _byteswap); +void *_libelf_getphdr(Elf *_e, int _elfclass); +void *_libelf_getshdr(Elf_Scn *_scn, int _elfclass); +void _libelf_init_elf(Elf *_e, Elf_Kind _kind); +int _libelf_load_section_headers(Elf *e, void *ehdr); +int _libelf_malign(Elf_Type _t, int _elfclass); +size_t _libelf_msize(Elf_Type _t, int _elfclass, unsigned int _version); +void *_libelf_newphdr(Elf *_e, int _elfclass, size_t _count); +Elf_Data *_libelf_release_data(Elf_Data *_d); +Elf *_libelf_release_elf(Elf *_e); +Elf_Scn *_libelf_release_scn(Elf_Scn *_s); +int _libelf_setphnum(Elf *_e, void *_eh, int _elfclass, size_t _phnum); +int _libelf_setshnum(Elf *_e, void *_eh, int _elfclass, size_t _shnum); +int _libelf_setshstrndx(Elf *_e, void *_eh, int _elfclass, + size_t _shstrndx); +Elf_Data *_libelf_xlate(Elf_Data *_d, const Elf_Data *_s, + unsigned int _encoding, int _elfclass, int _direction); +int _libelf_xlate_shtype(uint32_t _sht); +__END_DECLS + +#endif /* __LIBELF_H_ */ diff --git a/rocclr/compiler/lib/loaders/elf/utils/libelf/_libelf_ar.h b/rocclr/compiler/lib/loaders/elf/utils/libelf/_libelf_ar.h new file mode 100644 index 0000000000..d6b15a7501 --- /dev/null +++ b/rocclr/compiler/lib/loaders/elf/utils/libelf/_libelf_ar.h @@ -0,0 +1,56 @@ +/*- + * Copyright (c) 2010 Joseph Koshy + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS `AS IS' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * $Id: _libelf_ar.h 2032 2011-10-23 09:07:00Z jkoshy $ + */ + +#ifndef __LIBELF_AR_H_ +#define __LIBELF_AR_H_ + +/* + * Prototypes and declarations needed by libelf's ar(1) archive + * handling code. + */ + +#include + +#define LIBELF_AR_BSD_EXTENDED_NAME_PREFIX "#1/" +#define LIBELF_AR_BSD_SYMTAB_NAME "__.SYMDEF" +#define LIBELF_AR_BSD_EXTENDED_NAME_PREFIX_SIZE \ + (sizeof(LIBELF_AR_BSD_EXTENDED_NAME_PREFIX) - 1) + +#define IS_EXTENDED_BSD_NAME(NAME) \ + (strncmp((NAME), LIBELF_AR_BSD_EXTENDED_NAME_PREFIX, \ + LIBELF_AR_BSD_EXTENDED_NAME_PREFIX_SIZE) == 0) + + +char *_libelf_ar_get_string(const char *_buf, size_t _sz, int _rawname, + int _svr4names); +char *_libelf_ar_get_raw_name(const struct ar_hdr *_arh); +char *_libelf_ar_get_translated_name(const struct ar_hdr *_arh, Elf *_ar); +int _libelf_ar_get_number(const char *_buf, size_t _sz, int _base, + size_t *_ret); + +#endif /* __LIBELF_AR_H_ */ diff --git a/rocclr/compiler/lib/loaders/elf/utils/libelf/_libelf_config.h b/rocclr/compiler/lib/loaders/elf/utils/libelf/_libelf_config.h new file mode 100644 index 0000000000..d42216c37f --- /dev/null +++ b/rocclr/compiler/lib/loaders/elf/utils/libelf/_libelf_config.h @@ -0,0 +1,218 @@ +/*- + * Copyright (c) 2008-2011 Joseph Koshy + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * $Id: _libelf_config.h 2032 2011-10-23 09:07:00Z jkoshy $ + */ + +#ifdef __FreeBSD__ + +#define LIBELF_VCSID(ID) __FBSDID(ID) + +/* + * Define LIBELF_{ARCH,BYTEORDER,CLASS} based on the machine architecture. + * See also: . + */ + +#if defined(__amd64__) + +#define LIBELF_ARCH EM_X86_64 +#define LIBELF_BYTEORDER ELFDATA2LSB +#define LIBELF_CLASS ELFCLASS64 + +#elif defined(__arm__) + +#define LIBELF_ARCH EM_ARM +#if defined(__ARMEB__) /* Big-endian ARM. */ +#define LIBELF_BYTEORDER ELFDATA2MSB +#else +#define LIBELF_BYTEORDER ELFDATA2LSB +#endif +#define LIBELF_CLASS ELFCLASS32 + +#elif defined(__i386__) + +#define LIBELF_ARCH EM_386 +#define LIBELF_BYTEORDER ELFDATA2LSB +#define LIBELF_CLASS ELFCLASS32 + +#elif defined(__ia64__) + +#define LIBELF_ARCH EM_IA_64 +#define LIBELF_BYTEORDER ELFDATA2LSB +#define LIBELF_CLASS ELFCLASS64 + +#elif defined(__mips__) + +#define LIBELF_ARCH EM_MIPS +#if defined(__MIPSEB__) +#define LIBELF_BYTEORDER ELFDATA2MSB +#else +#define LIBELF_BYTEORDER ELFDATA2LSB +#endif +#define LIBELF_CLASS ELFCLASS32 + +#elif defined(__powerpc__) + +#define LIBELF_ARCH EM_PPC +#define LIBELF_BYTEORDER ELFDATA2MSB +#define LIBELF_CLASS ELFCLASS32 + +#elif defined(__sparc__) + +#define LIBELF_ARCH EM_SPARCV9 +#define LIBELF_BYTEORDER ELFDATA2MSB +#define LIBELF_CLASS ELFCLASS64 + +#else +#error Unknown FreeBSD architecture. +#endif +#endif /* __FreeBSD__ */ + + +#ifdef __NetBSD__ + +#include + +#define LIBELF_VCSID(ID) __RCSID(ID) + +#if !defined(ARCH_ELFSIZE) +#error ARCH_ELFSIZE is not defined. +#endif + +#if ARCH_ELFSIZE == 32 +#define LIBELF_ARCH ELF32_MACHDEP_ID +#define LIBELF_BYTEORDER ELF32_MACHDEP_ENDIANNESS +#define LIBELF_CLASS ELFCLASS32 +#define Elf_Note Elf32_Nhdr +#else +#define LIBELF_ARCH ELF64_MACHDEP_ID +#define LIBELF_BYTEORDER ELF64_MACHDEP_ENDIANNESS +#define LIBELF_CLASS ELFCLASS64 +#define Elf_Note Elf64_Nhdr +#endif + +#endif /* __NetBSD__ */ + +/* + * GNU & Linux compatibility. + * + * `__linux__' is defined in an environment runs the Linux kernel and glibc. + * `__GNU__' is defined in an environment runs a GNU kernel (Hurd) and glibc. + * `__GLIBC__' is defined for an environment that runs glibc over a non-GNU + * kernel such as GNU/kFreeBSD. + */ + +#if defined(__linux__) || defined(__GNU__) || defined(__GLIBC__) + +#if defined(__linux__) + +#include "native-elf-format.h" + +#define LIBELF_CLASS ELFTC_CLASS +#define LIBELF_ARCH ELFTC_ARCH +#define LIBELF_BYTEORDER ELFTC_BYTEORDER + +#endif /* defined(__linux__) */ + +#define LIBELF_VCSID(ID) + +#if LIBELF_CLASS == ELFCLASS32 +#define Elf_Note Elf32_Nhdr +#elif LIBELF_CLASS == ELFCLASS64 +#define Elf_Note Elf64_Nhdr +#else +#error LIBELF_CLASS needs to be one of ELFCLASS32 or ELFCLASS64 +#endif + +#define roundup2 roundup + +#endif /* defined(__linux__) || defined(__GNU__) || defined(__GLIBC__) */ +/* + * Common configuration for the GNU environment. + */ + +#define LIBELF_CONFIG_ADDR 1 +#define LIBELF_CONFIG_BYTE 1 +#define LIBELF_CONFIG_DYN 1 +#define LIBELF_CONFIG_EHDR 1 +#define LIBELF_CONFIG_HALF 1 +#define LIBELF_CONFIG_MOVEP 1 +#define LIBELF_CONFIG_NOTE 1 +#define LIBELF_CONFIG_OFF 1 +#define LIBELF_CONFIG_PHDR 1 +#define LIBELF_CONFIG_REL 1 +#define LIBELF_CONFIG_RELA 1 +#define LIBELF_CONFIG_SHDR 1 +#define LIBELF_CONFIG_SWORD 1 +#define LIBELF_CONFIG_SXWORD 1 +#define LIBELF_CONFIG_SYM 1 +#define LIBELF_CONFIG_VDEF 1 +#define LIBELF_CONFIG_VNEED 1 +#define LIBELF_CONFIG_WORD 1 +#define LIBELF_CONFIG_XWORD 1 + +#if defined(WIN32) + +#include "native-elf-format.h" + +#define LIBELF_CLASS ELFTC_CLASS +#define LIBELF_ARCH ELFTC_ARCH +#define LIBELF_BYTEORDER ELFTC_BYTEORDER + +#define LIBELF_CONFIG_ADDR 1 +#define LIBELF_CONFIG_BYTE 1 +#define LIBELF_CONFIG_DYN 1 +#define LIBELF_CONFIG_EHDR 1 +#define LIBELF_CONFIG_HALF 1 +#define LIBELF_CONFIG_MOVEP 1 +#define LIBELF_CONFIG_OFF 1 +#define LIBELF_CONFIG_PHDR 1 +#define LIBELF_CONFIG_REL 1 +#define LIBELF_CONFIG_RELA 1 +#define LIBELF_CONFIG_SHDR 1 +#define LIBELF_CONFIG_SWORD 1 +#define LIBELF_CONFIG_SXWORD 1 +#define LIBELF_CONFIG_SYM 1 +#define LIBELF_CONFIG_WORD 1 +#define LIBELF_CONFIG_XWORD 1 + +#define LIBELF_VCSID(ID) + +#define roundup2 roundup +#endif // defined(WIN32) +#ifndef LIBELF_CONFIG_GNUHASH +#define LIBELF_CONFIG_GNUHASH 1 + +/* + * The header for GNU-style hash sections. + */ + +typedef struct { + u_int32_t gh_nbuckets; /* Number of hash buckets. */ + u_int32_t gh_symndx; /* First visible symbol in .dynsym. */ + u_int32_t gh_maskwords; /* #maskwords used in bloom filter. */ + u_int32_t gh_shift2; /* Bloom filter shift count. */ +} Elf_GNU_Hash_Header; +#endif diff --git a/rocclr/compiler/lib/loaders/elf/utils/libelf/elf.c b/rocclr/compiler/lib/loaders/elf/utils/libelf/elf.c new file mode 100644 index 0000000000..9028aed367 --- /dev/null +++ b/rocclr/compiler/lib/loaders/elf/utils/libelf/elf.c @@ -0,0 +1,41 @@ +/*- + * Copyright (c) 2006,2008,2011 Joseph Koshy + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include + +#include "_libelf.h" + +LIBELF_VCSID("$Id: elf.c 1345 2011-01-01 11:17:52Z jkoshy $"); + +struct _libelf_globals _libelf = { + /*.libelf_arch = */LIBELF_ARCH, + /*.libelf_byteorder = */LIBELF_BYTEORDER, + /*.libelf_class = */LIBELF_CLASS, + /*.libelf_error = */0, + /*.libelf_fillchar = */0, + /*.libelf_version = */EV_NONE, + { 0 } +}; diff --git a/rocclr/compiler/lib/loaders/elf/utils/libelf/elf_begin.c b/rocclr/compiler/lib/loaders/elf/utils/libelf/elf_begin.c new file mode 100644 index 0000000000..af6177b818 --- /dev/null +++ b/rocclr/compiler/lib/loaders/elf/utils/libelf/elf_begin.c @@ -0,0 +1,337 @@ +/*- + * Copyright (c) 2006,2008-2011 Joseph Koshy + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include + +#include +#if !defined(WIN32) +#include +#include +#else +#ifndef PROT_READ +#define PROT_READ FILE_MAP_READ +#endif +#ifndef MAP_PRIVATE +#define MAP_PRIVATE FILE_MAP_COPY +#endif +#ifndef MAP_FAILED +#define MAP_FAILED NULL +#endif +#include +#endif +#include + +#include +#include +#include +#include +#include +#include +#include +#if !defined(WIN32) +#include +#else +#include "compat.h" +#endif + +#include "_libelf.h" + +LIBELF_VCSID("$Id: elf_begin.c 1923 2011-09-23 09:01:13Z jkoshy $"); + +#define _LIBELF_INITSIZE (64*1024) + +/* + * Read from a device file, pipe or socket. + */ +static void * +_libelf_read_special_file(int fd, size_t *fsz) +{ + ssize_t readsz; + size_t bufsz, datasz; + unsigned char *buf, *t; + + datasz = 0; + readsz = 0; + bufsz = _LIBELF_INITSIZE; + if ((buf = malloc(bufsz)) == NULL) + goto resourceerror; + + /* + * Read data from the file descriptor till we reach EOF, or + * till an error is encountered. + */ + do { + /* Check if we need to expand the data buffer. */ + if (datasz == bufsz) { + bufsz *= 2; + if ((t = realloc(buf, bufsz)) == NULL) + goto resourceerror; + buf = t; + } + + do { + readsz = bufsz - datasz; + t = buf + datasz; + if ((readsz = read(fd, t, readsz)) <= 0) + break; + datasz += readsz; + } while (datasz < bufsz); + + } while (readsz > 0); + + if (readsz < 0) { + LIBELF_SET_ERROR(IO, errno); + goto error; + } + + assert(readsz == 0); + + /* + * Free up extra buffer space. + */ + if (bufsz > datasz) { + if (datasz > 0) { + if ((t = realloc(buf, datasz)) == NULL) + goto resourceerror; + buf = t; + } else { /* Zero bytes read. */ + LIBELF_SET_ERROR(ARGUMENT, 0); + free(buf); + buf = NULL; + } + } + + *fsz = datasz; + return (buf); + +resourceerror: + LIBELF_SET_ERROR(RESOURCE, 0); +error: + if (buf != NULL) + free(buf); + return (NULL); +} + + +static Elf * +_libelf_open_object(int fd, Elf_Cmd c, Elf_Mem *mem) +{ + Elf *e; + void *m; + mode_t mode; + size_t fsize; + struct stat sb; + unsigned int flags; + + assert(c == ELF_C_READ || c == ELF_C_RDWR || c == ELF_C_WRITE); + + if (fstat(fd, &sb) < 0) { + LIBELF_SET_ERROR(IO, errno); + return (NULL); + } + + mode = sb.st_mode; + fsize = (size_t) sb.st_size; + + /* + * Reject unsupported file types. + */ + if (!S_ISREG(mode) && !S_ISCHR(mode) +#if !defined(WIN32) + && !S_ISFIFO(mode) && + !S_ISSOCK(mode) +#endif + ) { + LIBELF_SET_ERROR(ARGUMENT, 0); + return (NULL); + } + + /* + * For ELF_C_WRITE mode, allocate and return a descriptor. + * For ELF_C_RDWR mode, if the file is empty, allocate and return. + */ + if (c == ELF_C_WRITE || (c == ELF_C_RDWR && !fsize)) { + if ((e = _libelf_allocate_elf(mem)) != NULL) { + _libelf_init_elf(e, ELF_K_ELF); + e->e_byteorder = LIBELF_PRIVATE(byteorder); + e->e_fd = fd; + e->e_cmd = c; + if (!S_ISREG(mode)) + e->e_flags |= LIBELF_F_SPECIAL_FILE; + } + + return (e); + } + + + /* + * ELF_C_READ and ELF_C_RDWR mode. + */ + m = NULL; + flags = 0; + if (S_ISREG(mode)) { + /* + * Always map regular files in with 'PROT_READ' + * permissions. + * + * For objects opened in ELF_C_RDWR mode, when + * elf_update(3) is called, we remove this mapping, + * write file data out using write(2), and map the new + * contents back. + */ + if ((m = mmap(NULL, fsize, PROT_READ, MAP_PRIVATE, fd, + (off_t) 0)) == MAP_FAILED) { + LIBELF_SET_ERROR(IO, errno); + return (NULL); + } + flags = LIBELF_F_RAWFILE_MMAP; +#if 0 + m = mem.alloc(fsize); + if (!fread(m, 1, fsize, _fdopen(fd, "w+b"))) { + LIBELF_SET_ERROR(IO, errno); + mem.dealloc(m); + return(NULL); + } + flags = LIBELF_F_RAWFILE_MALLOC; +#endif + } else if ((m = _libelf_read_special_file(fd, &fsize)) != NULL) + flags = LIBELF_F_RAWFILE_MALLOC | LIBELF_F_SPECIAL_FILE; + else + return (NULL); + + if ((e = elf_memory(m, fsize, mem)) == NULL) { + assert((flags & LIBELF_F_RAWFILE_MALLOC) || + (flags & LIBELF_F_RAWFILE_MMAP)); + if (flags & LIBELF_F_RAWFILE_MMAP) + (void) munmap(m, fsize); + else + e->e_mem.dealloc(m); + return (NULL); + } + + /* ar(1) archives aren't supported in RDWR mode. */ + if (c == ELF_C_RDWR && e->e_kind == ELF_K_AR) { + (void) elf_end(e); + LIBELF_SET_ERROR(ARGUMENT, 0); + return (NULL); + } + + e->e_flags |= flags; + e->e_fd = fd; + e->e_cmd = c; + + return (e); +} + +Elf * +elf_begin(int fd, Elf_Cmd c, Elf *a, Elf_Mem *mem) +{ + Elf *e; + + e = NULL; + + if (LIBELF_PRIVATE(version) == EV_NONE) { + LIBELF_SET_ERROR(SEQUENCE, 0); + return (NULL); + } + + switch (c) { + case ELF_C_NULL: + return (NULL); + + case ELF_C_WRITE: + /* + * The ELF_C_WRITE command is required to ignore the + * descriptor passed in. + */ + a = NULL; + break; + + case ELF_C_RDWR: + if (a != NULL && a->e_kind == ELF_K_AR) { /* not allowed for ar(1) archives. */ + LIBELF_SET_ERROR(ARGUMENT, 0); + return (NULL); + } + /*FALLTHROUGH*/ + case ELF_C_READ: + /* + * Descriptor `a' could be for a regular ELF file, or + * for an ar(1) archive. If descriptor `a' was opened + * using a valid file descriptor, we need to check if + * the passed in `fd' value matches the original one. + */ + if (a && + ((a->e_fd != -1 && a->e_fd != fd) || c != a->e_cmd)) { + LIBELF_SET_ERROR(ARGUMENT, 0); + return (NULL); + } + break; + + default: + LIBELF_SET_ERROR(ARGUMENT, 0); + return (NULL); + + } + + if (a == NULL) + e = _libelf_open_object(fd, c, mem); + else if (a->e_kind == ELF_K_AR) + e = _libelf_ar_open_member(a->e_fd, c, a, mem); + else + (e = a)->e_activations++; + + return (e); +} +#if defined(WIN32) +// This code taken from: +// http://git.661346.n2.nabble.com/PATCH-mmap-implementation-for-mingw-td1560056.html +// This code is in public domain according to the FAQ here: +// http://www.mingw.org/wiki/FAQ +// http://www.mingw.org/license +// FIXME: This needs to be more robust to the protection and flag options. +void *mmap(void *start, size_t length, int prot, int flags, int fd, +unsigned offset) +{ + HANDLE handle; + + if (start != NULL || !(flags & MAP_PRIVATE)) + assert(!"Invalid usage of mingw_mmap"); + + handle = CreateFileMapping((HANDLE)_get_osfhandle(fd), NULL, PAGE_READONLY, 0, 0, NULL); + if (handle != NULL) { + start = MapViewOfFile(handle, flags, 0, offset, +length); + CloseHandle(handle); + } + return start; +} + + +int munmap(void *start, size_t length) { + UnmapViewOfFile(start); + return 0; +} +#endif diff --git a/rocclr/compiler/lib/loaders/elf/utils/libelf/elf_cntl.c b/rocclr/compiler/lib/loaders/elf/utils/libelf/elf_cntl.c new file mode 100644 index 0000000000..2021917437 --- /dev/null +++ b/rocclr/compiler/lib/loaders/elf/utils/libelf/elf_cntl.c @@ -0,0 +1,58 @@ +/*- + * Copyright (c) 2006,2008 Joseph Koshy + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include + +#include "_libelf.h" + +LIBELF_VCSID("$Id: elf_cntl.c 189 2008-07-20 10:38:08Z jkoshy $"); + +int +elf_cntl(Elf *e, Elf_Cmd c) +{ + if (e == NULL || + (c != ELF_C_FDDONE && c != ELF_C_FDREAD)) { + LIBELF_SET_ERROR(ARGUMENT, 0); + return (-1); + } + + if (e->e_parent) { + LIBELF_SET_ERROR(ARCHIVE, 0); + return (-1); + } + + if (c == ELF_C_FDREAD) { + if (e->e_cmd == ELF_C_WRITE) { + LIBELF_SET_ERROR(MODE, 0); + return (-1); + } + else + return (0); + } + + e->e_fd = -1; + return 0; +} diff --git a/rocclr/compiler/lib/loaders/elf/utils/libelf/elf_data.c b/rocclr/compiler/lib/loaders/elf/utils/libelf/elf_data.c new file mode 100644 index 0000000000..45b503a6b7 --- /dev/null +++ b/rocclr/compiler/lib/loaders/elf/utils/libelf/elf_data.c @@ -0,0 +1,254 @@ +/*- + * Copyright (c) 2006,2008,2011 Joseph Koshy + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include +#include +#include +#include + +#include "_libelf.h" + +LIBELF_VCSID("$Id: elf_data.c 1765 2011-08-22 05:59:05Z jkoshy $"); + +Elf_Data * +elf_getdata(Elf_Scn *s, Elf_Data *d) +{ + Elf *e; + size_t fsz, msz, count; + int elfclass, elftype; + unsigned int sh_type; + uint64_t sh_align, sh_offset, sh_size; + int (*xlate)(char *_d, size_t _dsz, char *_s, size_t _c, int _swap); + + if (s == NULL || (e = s->s_elf) == NULL || + (d != NULL && s != d->d_scn)) { + LIBELF_SET_ERROR(ARGUMENT, 0); + return (NULL); + } + + assert(e->e_kind == ELF_K_ELF); + + if (d == NULL && (d = STAILQ_FIRST(&s->s_data)) != NULL) + return (d); + + if (d != NULL) + return (STAILQ_NEXT(d, d_next)); + + if (e->e_rawfile == NULL) { + /* + * In the ELF_C_WRITE case, there is no source that + * can provide data for the section. + */ + LIBELF_SET_ERROR(ARGUMENT, 0); + return (NULL); + } + + elfclass = e->e_class; + + assert(elfclass == ELFCLASS32 || elfclass == ELFCLASS64); + + if (elfclass == ELFCLASS32) { + sh_type = s->s_shdr.s_shdr32.sh_type; + sh_offset = (uint64_t) s->s_shdr.s_shdr32.sh_offset; + sh_size = (uint64_t) s->s_shdr.s_shdr32.sh_size; + sh_align = (uint64_t) s->s_shdr.s_shdr32.sh_addralign; + } else { + sh_type = s->s_shdr.s_shdr64.sh_type; + sh_offset = s->s_shdr.s_shdr64.sh_offset; + sh_size = s->s_shdr.s_shdr64.sh_size; + sh_align = s->s_shdr.s_shdr64.sh_addralign; + } + + if (sh_type == SHT_NULL) { + LIBELF_SET_ERROR(SECTION, 0); + return (NULL); + } + + if ((elftype = _libelf_xlate_shtype(sh_type)) < ELF_T_FIRST || + elftype > ELF_T_LAST || (sh_type != SHT_NOBITS && + sh_offset + sh_size > (uint64_t) e->e_rawsize)) { + LIBELF_SET_ERROR(SECTION, 0); + return (NULL); + } + + if ((fsz = (elfclass == ELFCLASS32 ? elf32_fsize : elf64_fsize) + (elftype, (size_t) 1, e->e_version)) == 0) { + LIBELF_SET_ERROR(UNIMPL, 0); + return (NULL); + } + + if (sh_size % fsz) { + LIBELF_SET_ERROR(SECTION, 0); + return (NULL); + } + + count = sh_size / fsz; + + msz = _libelf_msize(elftype, elfclass, e->e_version); + + assert(msz > 0); + + if ((d = _libelf_allocate_data(s)) == NULL) + return (NULL); + + d->d_buf = NULL; + d->d_off = 0; + d->d_align = sh_align; + d->d_size = msz * count; + d->d_type = elftype; + d->d_version = e->e_version; + + if (sh_type == SHT_NOBITS || sh_size == 0) { + STAILQ_INSERT_TAIL(&s->s_data, d, d_next); + return (d); + } + + if ((d->d_buf = e->e_mem.alloc(msz*count)) == NULL) { + (void) _libelf_release_data(d); + LIBELF_SET_ERROR(RESOURCE, 0); + return (NULL); + } + + d->d_flags |= LIBELF_F_DATA_MALLOCED; + + xlate = _libelf_get_translator(elftype, ELF_TOMEMORY, elfclass); + if (!(*xlate)(d->d_buf, d->d_size, e->e_rawfile + sh_offset, count, + e->e_byteorder != LIBELF_PRIVATE(byteorder))) { + _libelf_release_data(d); + LIBELF_SET_ERROR(DATA, 0); + return (NULL); + } + + STAILQ_INSERT_TAIL(&s->s_data, d, d_next); + + return (d); +} + +Elf_Data * +elf_newdata(Elf_Scn *s) +{ + Elf *e; + Elf_Data *d; + + if (s == NULL || (e = s->s_elf) == NULL) { + LIBELF_SET_ERROR(ARGUMENT, 0); + return (NULL); + } + + assert(e->e_kind == ELF_K_ELF); + + /* + * elf_newdata() has to append a data descriptor, so + * bring in existing section data if not already present. + */ + if (e->e_rawfile && s->s_size > 0 && STAILQ_EMPTY(&s->s_data)) + if (elf_getdata(s, NULL) == NULL) + return (NULL); + + if ((d = _libelf_allocate_data(s)) == NULL) + return (NULL); + + STAILQ_INSERT_TAIL(&s->s_data, d, d_next); + + d->d_align = 1; + d->d_buf = NULL; + d->d_off = (uint64_t) ~0; + d->d_size = 0; + d->d_type = ELF_T_BYTE; + d->d_version = LIBELF_PRIVATE(version); + + (void) elf_flagscn(s, ELF_C_SET, ELF_F_DIRTY); + + return (d); +} + +/* + * Retrieve a data descriptor for raw (untranslated) data for section + * `s'. + */ + +Elf_Data * +elf_rawdata(Elf_Scn *s, Elf_Data *d) +{ + Elf *e; + int elf_class; + uint32_t sh_type; + uint64_t sh_align, sh_offset, sh_size; + + if (s == NULL || (e = s->s_elf) == NULL || e->e_rawfile == NULL) { + LIBELF_SET_ERROR(ARGUMENT, 0); + return (NULL); + } + + assert(e->e_kind == ELF_K_ELF); + + if (d == NULL && (d = STAILQ_FIRST(&s->s_rawdata)) != NULL) + return (d); + + if (d != NULL) + return (STAILQ_NEXT(d, d_next)); + + elf_class = e->e_class; + + assert(elf_class == ELFCLASS32 || elf_class == ELFCLASS64); + + if (elf_class == ELFCLASS32) { + sh_type = s->s_shdr.s_shdr32.sh_type; + sh_offset = (uint64_t) s->s_shdr.s_shdr32.sh_offset; + sh_size = (uint64_t) s->s_shdr.s_shdr32.sh_size; + sh_align = (uint64_t) s->s_shdr.s_shdr32.sh_addralign; + } else { + sh_type = s->s_shdr.s_shdr64.sh_type; + sh_offset = s->s_shdr.s_shdr64.sh_offset; + sh_size = s->s_shdr.s_shdr64.sh_size; + sh_align = s->s_shdr.s_shdr64.sh_addralign; + } + + if (sh_type == SHT_NULL) + return (NULL); + + if ((d = _libelf_allocate_data(s)) == NULL) + return (NULL); + + d->d_buf = (sh_type == SHT_NOBITS || sh_size == 0) ? NULL : + e->e_rawfile + sh_offset; + d->d_off = 0; + d->d_align = sh_align; + d->d_size = sh_size; + d->d_type = ELF_T_BYTE; + d->d_version = e->e_version; + + STAILQ_INSERT_TAIL(&s->s_rawdata, d, d_next); + + return (d); +} + +void +elf_removedata(Elf_Scn *s, Elf_Data *d) +{ + STAILQ_REMOVE(&s->s_data, d, _Elf_Data, d_next); + d = _libelf_release_data(d); +} diff --git a/rocclr/compiler/lib/loaders/elf/utils/libelf/elf_end.c b/rocclr/compiler/lib/loaders/elf/utils/libelf/elf_end.c new file mode 100644 index 0000000000..6f954af321 --- /dev/null +++ b/rocclr/compiler/lib/loaders/elf/utils/libelf/elf_end.c @@ -0,0 +1,99 @@ +/*- + * Copyright (c) 2006,2008-2009,2011 Joseph Koshy + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include + +#if !defined(WIN32) +#include +#endif + +#include +#include +#include + +#include "_libelf.h" + +LIBELF_VCSID("$Id: elf_end.c 1922 2011-09-23 08:04:33Z jkoshy $"); + +int +elf_end(Elf *e) +{ + Elf *sv; + Elf_Scn *scn, *tscn; + + if (e == NULL || e->e_activations == 0) + return (0); + + if (--e->e_activations > 0) + return (e->e_activations); + + assert(e->e_activations == 0); + + while (e && e->e_activations == 0) { + switch (e->e_kind) { + case ELF_K_AR: + /* + * If we still have open child descriptors, we + * need to defer reclaiming resources till all + * the child descriptors for the archive are + * closed. + */ + if (e->e_u.e_ar.e_nchildren > 0) + return (0); + break; + case ELF_K_ELF: + /* + * Reclaim all section descriptors. + */ + STAILQ_FOREACH_SAFE(scn, &e->e_u.e_elf.e_scn, s_next, + tscn) + scn = _libelf_release_scn(scn); + break; + case ELF_K_NUM: + assert(0); + default: + break; + } + + if (e->e_rawfile) { + if (e->e_flags & LIBELF_F_RAWFILE_MMAP) +#if !defined(WIN32) + (void) munmap(e->e_rawfile, e->e_rawsize); +#else + abort(); +#endif + else if (e->e_flags & LIBELF_F_RAWFILE_MALLOC) + e->e_mem.dealloc(e->e_rawfile); + } + + sv = e; + if ((e = e->e_parent) != NULL) + e->e_u.e_ar.e_nchildren--; + sv = _libelf_release_elf(sv); + } + + return (0); +} diff --git a/rocclr/compiler/lib/loaders/elf/utils/libelf/elf_errmsg.c b/rocclr/compiler/lib/loaders/elf/utils/libelf/elf_errmsg.c new file mode 100644 index 0000000000..225ec8d5f0 --- /dev/null +++ b/rocclr/compiler/lib/loaders/elf/utils/libelf/elf_errmsg.c @@ -0,0 +1,88 @@ +/*- + * Copyright (c) 2006,2008,2011 Joseph Koshy + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include + +#include +#include +#include + +#include "_libelf.h" + +LIBELF_VCSID("$Id: elf_errmsg.c 1345 2011-01-01 11:17:52Z jkoshy $"); + +/* + * Retrieve a human readable translation for an error message. + */ +#if defined(__GNUC__) +#define DEFINE_ERROR(N,S) [ELF_E_##N] = S +#else +#define DEFINE_ERROR(N,S) S +#endif +const char *_libelf_errors[] = { + DEFINE_ERROR(NONE, "No Error"), + DEFINE_ERROR(ARCHIVE, "Malformed ar(1) archive"), + DEFINE_ERROR(ARGUMENT, "Invalid argument"), + DEFINE_ERROR(CLASS, "ELF class mismatch"), + DEFINE_ERROR(DATA, "Invalid data buffer descriptor"), + DEFINE_ERROR(HEADER, "Missing or malformed ELF header"), + DEFINE_ERROR(IO, "I/O error"), + DEFINE_ERROR(LAYOUT, "Layout constraint violation"), + DEFINE_ERROR(MODE, "Incorrect ELF descriptor mode"), + DEFINE_ERROR(RANGE, "Value out of range of target"), + DEFINE_ERROR(RESOURCE, "Resource exhaustion"), + DEFINE_ERROR(SECTION, "Invalid section descriptor"), + DEFINE_ERROR(SEQUENCE, "API calls out of sequence"), + DEFINE_ERROR(UNIMPL, "Unimplemented feature"), + DEFINE_ERROR(VERSION, "Unknown ELF API version"), + DEFINE_ERROR(NUM, "Unknown error") +}; +#undef DEFINE_ERROR + +const char * +elf_errmsg(int error) +{ + int oserr; + + if (error == ELF_E_NONE && + (error = LIBELF_PRIVATE(error)) == 0) + return NULL; + else if (error == -1) + error = LIBELF_PRIVATE(error); + + oserr = error >> LIBELF_OS_ERROR_SHIFT; + error &= LIBELF_ELF_ERROR_MASK; + + if (error < ELF_E_NONE || error >= ELF_E_NUM) + return _libelf_errors[ELF_E_NUM]; + if (oserr) { + (void) snprintf(LIBELF_PRIVATE(msg), + sizeof(LIBELF_PRIVATE(msg)), "%s: %s", + _libelf_errors[error], strerror(oserr)); + return (const char *)&LIBELF_PRIVATE(msg); + } + return _libelf_errors[error]; +} diff --git a/rocclr/compiler/lib/loaders/elf/utils/libelf/elf_errno.c b/rocclr/compiler/lib/loaders/elf/utils/libelf/elf_errno.c new file mode 100644 index 0000000000..95e91b93ff --- /dev/null +++ b/rocclr/compiler/lib/loaders/elf/utils/libelf/elf_errno.c @@ -0,0 +1,43 @@ +/*- + * Copyright (c) 2006,2008,2011 Joseph Koshy + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include + +#include + +#include "_libelf.h" + +LIBELF_VCSID("$Id: elf_errno.c 1345 2011-01-01 11:17:52Z jkoshy $"); + +int +elf_errno(void) +{ + int old; + + old = LIBELF_PRIVATE(error); + LIBELF_PRIVATE(error) = 0; + return (old & LIBELF_ELF_ERROR_MASK); +} diff --git a/rocclr/compiler/lib/loaders/elf/utils/libelf/elf_fill.c b/rocclr/compiler/lib/loaders/elf/utils/libelf/elf_fill.c new file mode 100644 index 0000000000..ac9e02ead0 --- /dev/null +++ b/rocclr/compiler/lib/loaders/elf/utils/libelf/elf_fill.c @@ -0,0 +1,39 @@ +/*- + * Copyright (c) 2006,2008 Joseph Koshy + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include + +#include + +#include "_libelf.h" + +LIBELF_VCSID("$Id: elf_fill.c 189 2008-07-20 10:38:08Z jkoshy $"); + +void +elf_fill(int fill) +{ + LIBELF_PRIVATE(fillchar) = fill; +} diff --git a/rocclr/compiler/lib/loaders/elf/utils/libelf/elf_flag.c b/rocclr/compiler/lib/loaders/elf/utils/libelf/elf_flag.c new file mode 100644 index 0000000000..9d3171952f --- /dev/null +++ b/rocclr/compiler/lib/loaders/elf/utils/libelf/elf_flag.c @@ -0,0 +1,195 @@ +/*- + * Copyright (c) 2006,2008-2009,2011 Joseph Koshy + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include + +#include + +#include "_libelf.h" + +LIBELF_VCSID("$Id: elf_flag.c 1918 2011-09-22 10:42:06Z jkoshy $"); + +unsigned int +elf_flagarhdr(Elf_Arhdr *a, Elf_Cmd c, unsigned int flags) +{ + unsigned int r; + + if (a == NULL) + return (0); + + if ((c != ELF_C_SET && c != ELF_C_CLR) || + (flags & ~ELF_F_DIRTY) != 0) { + LIBELF_SET_ERROR(ARGUMENT, 0); + return (0); + } + + if (c == ELF_C_SET) + r = a->ar_flags |= flags; + else + r = a->ar_flags &= ~flags; + + return (r & LIBELF_F_API_MASK); +} + +unsigned int +elf_flagdata(Elf_Data *d, Elf_Cmd c, unsigned int flags) +{ + unsigned int r; + + if (d == NULL) + return (0); + + if ((c != ELF_C_SET && c != ELF_C_CLR) || + (flags & ~ELF_F_DIRTY) != 0) { + LIBELF_SET_ERROR(ARGUMENT, 0); + return (0); + } + + if (c == ELF_C_SET) + r = d->d_flags |= flags; + else + r = d->d_flags &= ~flags; + + return (r & LIBELF_F_API_MASK); +} + +unsigned int +elf_flagehdr(Elf *e, Elf_Cmd c, unsigned int flags) +{ + int ec; + void *ehdr; + + if (e == NULL) + return (0); + + if ((c != ELF_C_SET && c != ELF_C_CLR) || + (e->e_kind != ELF_K_ELF) || (flags & ~ELF_F_DIRTY) != 0 || + ((ec = e->e_class) != ELFCLASS32 && ec != ELFCLASS64)) { + LIBELF_SET_ERROR(ARGUMENT, 0); + return (0); + } + + if (ec == ELFCLASS32) + ehdr = e->e_u.e_elf.e_ehdr.e_ehdr32; + else + ehdr = e->e_u.e_elf.e_ehdr.e_ehdr64; + + if (ehdr == NULL) { + LIBELF_SET_ERROR(SEQUENCE, 0); + return (0); + } + + return (elf_flagelf(e, c, flags)); +} + +unsigned int +elf_flagelf(Elf *e, Elf_Cmd c, unsigned int flags) +{ + int r; + + if (e == NULL) + return (0); + + if ((c != ELF_C_SET && c != ELF_C_CLR) || + (e->e_kind != ELF_K_ELF) || + (flags & ~(ELF_F_ARCHIVE | ELF_F_ARCHIVE_SYSV | + ELF_F_DIRTY | ELF_F_LAYOUT)) != 0) { + LIBELF_SET_ERROR(ARGUMENT, 0); + return (0); + } + + if ((flags & ELF_F_ARCHIVE_SYSV) && (flags & ELF_F_ARCHIVE) == 0) { + LIBELF_SET_ERROR(ARGUMENT, 0); + return (0); + } + + if ((flags & ELF_F_ARCHIVE) && e->e_cmd != ELF_C_WRITE) { + LIBELF_SET_ERROR(MODE, 0); + return (0); + } + + if (c == ELF_C_SET) + r = e->e_flags |= flags; + else + r = e->e_flags &= ~flags; + return (r & LIBELF_F_API_MASK); +} + +unsigned int +elf_flagphdr(Elf *e, Elf_Cmd c, unsigned int flags) +{ + int ec; + void *phdr; + + if (e == NULL) + return (0); + + if ((c != ELF_C_SET && c != ELF_C_CLR) || + (e->e_kind != ELF_K_ELF) || (flags & ~ELF_F_DIRTY) != 0 || + ((ec = e->e_class) != ELFCLASS32 && ec != ELFCLASS64)) { + LIBELF_SET_ERROR(ARGUMENT, 0); + return (0); + } + + if (ec == ELFCLASS32) + phdr = e->e_u.e_elf.e_phdr.e_phdr32; + else + phdr = e->e_u.e_elf.e_phdr.e_phdr64; + + if (phdr == NULL) { + LIBELF_SET_ERROR(SEQUENCE, 0); + return (0); + } + + return (elf_flagelf(e, c, flags)); +} + +unsigned int +elf_flagscn(Elf_Scn *s, Elf_Cmd c, unsigned int flags) +{ + int r; + + if (s == NULL) + return (0); + + if ((c != ELF_C_SET && c != ELF_C_CLR) || + (flags & ~ELF_F_DIRTY) != 0) { + LIBELF_SET_ERROR(ARGUMENT, 0); + return (0); + } + + if (c == ELF_C_SET) + r = s->s_flags |= flags; + else + r = s->s_flags &= ~flags; + return (r & LIBELF_F_API_MASK); +} + +unsigned int +elf_flagshdr(Elf_Scn *s, Elf_Cmd c, unsigned int flags) +{ + return (elf_flagscn(s, c, flags)); +} diff --git a/rocclr/compiler/lib/loaders/elf/utils/libelf/elf_getarhdr.c b/rocclr/compiler/lib/loaders/elf/utils/libelf/elf_getarhdr.c new file mode 100644 index 0000000000..43ceafda85 --- /dev/null +++ b/rocclr/compiler/lib/loaders/elf/utils/libelf/elf_getarhdr.c @@ -0,0 +1,47 @@ +/*- + * Copyright (c) 2006,2008,2010 Joseph Koshy + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include + +#include + +#include "_libelf.h" + +LIBELF_VCSID("$Id: elf_getarhdr.c 1341 2011-01-01 04:28:29Z jkoshy $"); + +Elf_Arhdr * +elf_getarhdr(Elf *e) +{ + if (e == NULL) { + LIBELF_SET_ERROR(ARGUMENT, 0); + return (NULL); + } + + if (e->e_flags & LIBELF_F_AR_HEADER) + return (e->e_hdr.e_arhdr); + + return (_libelf_ar_gethdr(e)); +} diff --git a/rocclr/compiler/lib/loaders/elf/utils/libelf/elf_getarsym.c b/rocclr/compiler/lib/loaders/elf/utils/libelf/elf_getarsym.c new file mode 100644 index 0000000000..1852262bdb --- /dev/null +++ b/rocclr/compiler/lib/loaders/elf/utils/libelf/elf_getarsym.c @@ -0,0 +1,58 @@ +/*- + * Copyright (c) 2006,2008 Joseph Koshy + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include + +#include + +#include "_libelf.h" + +LIBELF_VCSID("$Id: elf_getarsym.c 1360 2011-01-08 08:27:41Z jkoshy $"); + +Elf_Arsym * +elf_getarsym(Elf *ar, size_t *ptr) +{ + size_t n; + Elf_Arsym *symtab; + + n = 0; + symtab = NULL; + + if (ar == NULL || ar->e_kind != ELF_K_AR) + LIBELF_SET_ERROR(ARGUMENT, 0); + else if ((symtab = ar->e_u.e_ar.e_symtab) != NULL) + n = ar->e_u.e_ar.e_symtabsz; + else if (ar->e_u.e_ar.e_rawsymtab) + symtab = (ar->e_flags & LIBELF_F_AR_VARIANT_SVR4) ? + _libelf_ar_process_svr4_symtab(ar, &n) : + _libelf_ar_process_bsd_symtab(ar, &n); + else + LIBELF_SET_ERROR(ARCHIVE, 0); + + if (ptr) + *ptr = n; + return (symtab); +} diff --git a/rocclr/compiler/lib/loaders/elf/utils/libelf/elf_getbase.c b/rocclr/compiler/lib/loaders/elf/utils/libelf/elf_getbase.c new file mode 100644 index 0000000000..30058ca70c --- /dev/null +++ b/rocclr/compiler/lib/loaders/elf/utils/libelf/elf_getbase.c @@ -0,0 +1,48 @@ +/*- + * Copyright (c) 2006,2008 Joseph Koshy + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include + +#include + +#include "_libelf.h" + +LIBELF_VCSID("$Id: elf_getbase.c 977 2010-06-06 11:50:31Z jkoshy $"); + +off_t +elf_getbase(Elf *e) +{ + if (e == NULL) { + LIBELF_SET_ERROR(ARGUMENT, 0); + return ((off_t) -1); + } + + if (e->e_parent == NULL) + return ((off_t) 0); + + return ((off_t) ((uintptr_t) e->e_rawfile - + (uintptr_t) e->e_parent->e_rawfile)); +} diff --git a/rocclr/compiler/lib/loaders/elf/utils/libelf/elf_getident.c b/rocclr/compiler/lib/loaders/elf/utils/libelf/elf_getident.c new file mode 100644 index 0000000000..c17f3a56ef --- /dev/null +++ b/rocclr/compiler/lib/loaders/elf/utils/libelf/elf_getident.c @@ -0,0 +1,68 @@ +/*- + * Copyright (c) 2006,2008 Joseph Koshy + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include + +#include +#include +#include + +#include "_libelf.h" + +LIBELF_VCSID("$Id: elf_getident.c 189 2008-07-20 10:38:08Z jkoshy $"); + +char * +elf_getident(Elf *e, size_t *sz) +{ + + if (e == NULL) { + LIBELF_SET_ERROR(ARGUMENT, 0); + goto error; + } + + if (e->e_cmd == ELF_C_WRITE && e->e_rawfile == NULL) { + LIBELF_SET_ERROR(SEQUENCE, 0); + goto error; + } + + assert(e->e_kind != ELF_K_AR || e->e_cmd == ELF_C_READ); + + if (sz) { + if (e->e_kind == ELF_K_AR) + *sz = SARMAG; + else if (e->e_kind == ELF_K_ELF) + *sz = EI_NIDENT; + else + *sz = e->e_rawsize; + } + + return ((char *) e->e_rawfile); + + error: + if (sz) + *sz = 0; + return (NULL); +} diff --git a/rocclr/compiler/lib/loaders/elf/utils/libelf/elf_hash.c b/rocclr/compiler/lib/loaders/elf/utils/libelf/elf_hash.c new file mode 100644 index 0000000000..12c764d54a --- /dev/null +++ b/rocclr/compiler/lib/loaders/elf/utils/libelf/elf_hash.c @@ -0,0 +1,56 @@ +/*- + * Copyright (c) 2006,2008 Joseph Koshy + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include + +#include + +#include "_libelf_config.h" + +LIBELF_VCSID("$Id: elf_hash.c 189 2008-07-20 10:38:08Z jkoshy $"); + +/* + * This elf_hash function is defined by the System V ABI. + */ + +unsigned long +elf_hash(const char *name) +{ + unsigned long h, t; + const unsigned char *s; + + s = (const unsigned char *) name; + h = t = 0; + + for (; *s != '\0'; h = h & ~t) { + h = (h << 4) + *s++; + t = h & 0xF0000000UL; + if (t) + h ^= t >> 24; + } + + return (h); +} diff --git a/rocclr/compiler/lib/loaders/elf/utils/libelf/elf_kind.c b/rocclr/compiler/lib/loaders/elf/utils/libelf/elf_kind.c new file mode 100644 index 0000000000..0b4251a23e --- /dev/null +++ b/rocclr/compiler/lib/loaders/elf/utils/libelf/elf_kind.c @@ -0,0 +1,44 @@ +/*- + * Copyright (c) 2006,2008 Joseph Koshy + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include + +#include + +#include "_libelf.h" + +LIBELF_VCSID("$Id: elf_kind.c 189 2008-07-20 10:38:08Z jkoshy $"); + +Elf_Kind +elf_kind(Elf *e) +{ + if (e == NULL) + return (ELF_K_NONE); + if (e->e_kind == ELF_K_AR || + e->e_kind == ELF_K_ELF) + return (e->e_kind); + return (ELF_K_NONE); +} diff --git a/rocclr/compiler/lib/loaders/elf/utils/libelf/elf_memory.c b/rocclr/compiler/lib/loaders/elf/utils/libelf/elf_memory.c new file mode 100644 index 0000000000..e310b0107a --- /dev/null +++ b/rocclr/compiler/lib/loaders/elf/utils/libelf/elf_memory.c @@ -0,0 +1,92 @@ +/*- + * Copyright (c) 2006,2008 Joseph Koshy + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include + +#include +#include +#include + +#include "_libelf.h" + +LIBELF_VCSID("$Id: elf_memory.c 189 2008-07-20 10:38:08Z jkoshy $"); + +Elf * +elf_memory(char *image, size_t sz, Elf_Mem *mem) +{ + Elf *e; + + if (LIBELF_PRIVATE(version) == EV_NONE) { + LIBELF_SET_ERROR(SEQUENCE, 0); + return (NULL); + } + + if (image == NULL || sz == 0) { + LIBELF_SET_ERROR(ARGUMENT, 0); + return (NULL); + } + + if ((e = _libelf_allocate_elf(mem)) == NULL) + return (NULL); + + e->e_cmd = ELF_C_READ; + e->e_rawfile = image; + e->e_rawsize = sz; + +#undef LIBELF_IS_ELF +#define LIBELF_IS_ELF(P) ((P)[EI_MAG0] == ELFMAG0 && \ + (P)[EI_MAG1] == ELFMAG1 && (P)[EI_MAG2] == ELFMAG2 && \ + (P)[EI_MAG3] == ELFMAG3) + + if (sz > EI_NIDENT && LIBELF_IS_ELF(image)) { + _libelf_init_elf(e, ELF_K_ELF); + e->e_class = image[EI_CLASS]; + e->e_byteorder = image[EI_DATA]; + e->e_version = image[EI_VERSION]; + + if (e->e_version > EV_CURRENT) { + e = _libelf_release_elf(e); + LIBELF_SET_ERROR(VERSION, 0); + return (NULL); + } + + if ((e->e_byteorder != ELFDATA2LSB && e->e_byteorder != + ELFDATA2MSB) || (e->e_class != ELFCLASS32 && e->e_class != + ELFCLASS64)) { + e = _libelf_release_elf(e); + LIBELF_SET_ERROR(HEADER, 0); + return (NULL); + } + + } else if (sz >= SARMAG && + strncmp(image, ARMAG, (size_t) SARMAG) == 0) { + _libelf_init_elf(e, ELF_K_AR); + e = _libelf_ar_open(e); + } else + _libelf_init_elf(e, ELF_K_NONE); + + return (e); +} diff --git a/rocclr/compiler/lib/loaders/elf/utils/libelf/elf_next.c b/rocclr/compiler/lib/loaders/elf/utils/libelf/elf_next.c new file mode 100644 index 0000000000..d6ca552e5f --- /dev/null +++ b/rocclr/compiler/lib/loaders/elf/utils/libelf/elf_next.c @@ -0,0 +1,62 @@ +/*- + * Copyright (c) 2006,2008 Joseph Koshy + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include + +#include +#include +#include + +#include "_libelf.h" + +LIBELF_VCSID("$Id: elf_next.c 1678 2011-07-28 04:36:34Z jkoshy $"); + +Elf_Cmd +elf_next(Elf *e) +{ + off_t next; + Elf *parent; + + if (e == NULL) + return (ELF_C_NULL); + + if ((parent = e->e_parent) == NULL) { + LIBELF_SET_ERROR(ARGUMENT, 0); + return (ELF_C_NULL); + } + + assert (parent->e_kind == ELF_K_AR); + assert (parent->e_cmd == ELF_C_READ); + assert(e->e_rawfile > parent->e_rawfile); + + next = e->e_rawfile - parent->e_rawfile + e->e_rawsize; + next = (next + 1) & ~1; /* round up to an even boundary */ + + parent->e_u.e_ar.e_next = (next >= (off_t) parent->e_rawsize) ? + (off_t) 0 : next; + + return (ELF_C_READ); +} diff --git a/rocclr/compiler/lib/loaders/elf/utils/libelf/elf_phnum.c b/rocclr/compiler/lib/loaders/elf/utils/libelf/elf_phnum.c new file mode 100644 index 0000000000..d63c490b24 --- /dev/null +++ b/rocclr/compiler/lib/loaders/elf/utils/libelf/elf_phnum.c @@ -0,0 +1,67 @@ +/*- + * Copyright (c) 2006,2008 Joseph Koshy + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include + +#include +#include + +#include "_libelf.h" + +LIBELF_VCSID("$Id: elf_phnum.c 466 2009-08-04 17:17:42Z jkoshy $"); + +static int +_libelf_getphdrnum(Elf *e, size_t *phnum) +{ + void *eh; + int ec; + + if (e == NULL || e->e_kind != ELF_K_ELF || + ((ec = e->e_class) != ELFCLASS32 && ec != ELFCLASS64)) { + LIBELF_SET_ERROR(ARGUMENT, 0); + return (-1); + } + + if ((eh = _libelf_ehdr(e, ec, 0)) == NULL) + return (-1); + + *phnum = e->e_u.e_elf.e_nphdr; + + return (0); +} + +int +elf_getphdrnum(Elf *e, size_t *phnum) +{ + return (_libelf_getphdrnum(e, phnum)); +} + +/* Deprecated API */ +int +elf_getphnum(Elf *e, size_t *phnum) +{ + return (_libelf_getphdrnum(e, phnum) >= 0); +} diff --git a/rocclr/compiler/lib/loaders/elf/utils/libelf/elf_rand.c b/rocclr/compiler/lib/loaders/elf/utils/libelf/elf_rand.c new file mode 100644 index 0000000000..2e7328a53e --- /dev/null +++ b/rocclr/compiler/lib/loaders/elf/utils/libelf/elf_rand.c @@ -0,0 +1,59 @@ +/*- + * Copyright (c) 2006,2008 Joseph Koshy + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include + +#include +#include + +#include "_libelf.h" + +LIBELF_VCSID("$Id: elf_rand.c 189 2008-07-20 10:38:08Z jkoshy $"); + +off_t +elf_rand(Elf *ar, off_t offset) +{ + struct ar_hdr *arh; + + if (ar == NULL || ar->e_kind != ELF_K_AR || + (offset & 1) || offset < SARMAG || + offset + sizeof(struct ar_hdr) >= ar->e_rawsize) { + LIBELF_SET_ERROR(ARGUMENT, 0); + return 0; + } + + arh = (struct ar_hdr *) (ar->e_rawfile + offset); + + /* a too simple sanity check */ + if (arh->ar_fmag[0] != '`' || arh->ar_fmag[1] != '\n') { + LIBELF_SET_ERROR(ARCHIVE, 0); + return 0; + } + + ar->e_u.e_ar.e_next = offset; + + return (offset); +} diff --git a/rocclr/compiler/lib/loaders/elf/utils/libelf/elf_rawfile.c b/rocclr/compiler/lib/loaders/elf/utils/libelf/elf_rawfile.c new file mode 100644 index 0000000000..22a9f95d23 --- /dev/null +++ b/rocclr/compiler/lib/loaders/elf/utils/libelf/elf_rawfile.c @@ -0,0 +1,53 @@ +/*- + * Copyright (c) 2006,2008 Joseph Koshy + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include + +#include + +#include "_libelf.h" + +LIBELF_VCSID("$Id: elf_rawfile.c 189 2008-07-20 10:38:08Z jkoshy $"); + +char * +elf_rawfile(Elf *e, size_t *sz) +{ + char *ptr; + size_t size; + + size = e ? e->e_rawsize : 0; + ptr = NULL; + + if (e == NULL) + LIBELF_SET_ERROR(ARGUMENT, 0); + else if ((ptr = e->e_rawfile) == NULL && e->e_cmd == ELF_C_WRITE) + LIBELF_SET_ERROR(SEQUENCE, 0); + + if (sz) + *sz = size; + + return (ptr); +} diff --git a/rocclr/compiler/lib/loaders/elf/utils/libelf/elf_scn.c b/rocclr/compiler/lib/loaders/elf/utils/libelf/elf_scn.c new file mode 100644 index 0000000000..2dbaea1ecd --- /dev/null +++ b/rocclr/compiler/lib/loaders/elf/utils/libelf/elf_scn.c @@ -0,0 +1,251 @@ +/*- + * Copyright (c) 2006,2008-2010 Joseph Koshy + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include +#include + +#include +#include +#include +#include +#include +#include + +#include "_libelf.h" + +LIBELF_VCSID("$Id: elf_scn.c 1077 2010-08-09 15:37:40Z jkoshy $"); + +/* + * Load an ELF section table and create a list of Elf_Scn structures. + */ +int +_libelf_load_section_headers(Elf *e, void *ehdr) +{ + int ec, swapbytes; + size_t fsz, i, shnum; + uint64_t shoff; + char *src; + Elf32_Ehdr *eh32; + Elf64_Ehdr *eh64; + Elf_Scn *scn; + int (*xlator)(char *_d, size_t _dsz, char *_s, size_t _c, int _swap); + + assert(e != NULL); + assert(ehdr != NULL); + assert((e->e_flags & LIBELF_F_SHDRS_LOADED) == 0); + +#define CHECK_EHDR(E,EH) do { \ + if (fsz != (EH)->e_shentsize || \ + (e->e_rawfile && shoff + fsz * shnum > e->e_rawsize)) { \ + LIBELF_SET_ERROR(HEADER, 0); \ + return (0); \ + } \ + } while (0) + + ec = e->e_class; + fsz = _libelf_fsize(ELF_T_SHDR, ec, e->e_version, (size_t) 1); + assert(fsz > 0); + + shnum = e->e_u.e_elf.e_nscn; + + if (ec == ELFCLASS32) { + eh32 = (Elf32_Ehdr *) ehdr; + shoff = (uint64_t) eh32->e_shoff; + CHECK_EHDR(e, eh32); + } else { + eh64 = (Elf64_Ehdr *) ehdr; + shoff = eh64->e_shoff; + CHECK_EHDR(e, eh64); + } + + xlator = _libelf_get_translator(ELF_T_SHDR, ELF_TOMEMORY, ec); + + swapbytes = e->e_byteorder != LIBELF_PRIVATE(byteorder); + + // If we aren't editing a rawfile, then we don't need to + // load any sections. + if (!e->e_rawfile) { + return 1; + } + src = e->e_rawfile + shoff; + + /* + * If the file is using extended numbering then section #0 + * would have already been read in. + */ + + i = 0; + if (!STAILQ_EMPTY(&e->e_u.e_elf.e_scn)) { + assert(STAILQ_FIRST(&e->e_u.e_elf.e_scn) == + STAILQ_LAST(&e->e_u.e_elf.e_scn, _Elf_Scn, s_next)); + + i = 1; + src += fsz; + } + + for (; i < shnum; i++, src += fsz) { + if ((scn = _libelf_allocate_scn(e, i)) == NULL) + return (0); + + (*xlator)((char *) &scn->s_shdr, sizeof(scn->s_shdr), src, + (size_t) 1, swapbytes); + + if (ec == ELFCLASS32) { + scn->s_offset = + scn->s_shdr.s_shdr32.sh_offset; + scn->s_size = scn->s_shdr.s_shdr32.sh_size; + } else { + scn->s_offset = + scn->s_shdr.s_shdr64.sh_offset; + scn->s_size = scn->s_shdr.s_shdr64.sh_size; + } + // If we have a true read/write elf, we cannot trust the + // raw offset and we need to pull in the data also when + // the section headers are loaded. + if (e->e_cmd != ELF_C_RDWR) { + scn->s_rawoff = scn->s_offset; + } else { + elf_getdata(scn, NULL); + } + } + + e->e_flags |= LIBELF_F_SHDRS_LOADED; + + return (1); +} + + +Elf_Scn * +elf_getscn(Elf *e, size_t index) +{ + int ec; + void *ehdr; + Elf_Scn *s; + + if (e == NULL || e->e_kind != ELF_K_ELF || + ((ec = e->e_class) != ELFCLASS32 && ec != ELFCLASS64)) { + LIBELF_SET_ERROR(ARGUMENT, 0); + return (NULL); + } + + if ((ehdr = _libelf_ehdr(e, ec, 0)) == NULL) + return (NULL); + + // If we aren't a read/write from a file or a read only elf, + // make sure that the headers are loaded. If we are a read/write + // from a file, we should have the headers loaded already. + if ((e->e_cmd == ELF_C_READ + || (e->e_cmd == ELF_C_RDWR && e->e_fd == -1)) && + (e->e_flags & LIBELF_F_SHDRS_LOADED) == 0 && + _libelf_load_section_headers(e, ehdr) == 0) + return (NULL); + + STAILQ_FOREACH(s, &e->e_u.e_elf.e_scn, s_next) + if (s->s_ndx == index) + return (s); + + LIBELF_SET_ERROR(ARGUMENT, 0); + return (NULL); +} + +size_t +elf_ndxscn(Elf_Scn *s) +{ + if (s == NULL) { + LIBELF_SET_ERROR(ARGUMENT, 0); + return (SHN_UNDEF); + } + return (s->s_ndx); +} + +Elf_Scn * +elf_newscn(Elf *e) +{ + int ec; + void *ehdr; + Elf_Scn *scn; + + if (e == NULL || e->e_kind != ELF_K_ELF) { + LIBELF_SET_ERROR(ARGUMENT, 0); + return (NULL); + } + + if ((ec = e->e_class) != ELFCLASS32 && ec != ELFCLASS64) { + LIBELF_SET_ERROR(CLASS, 0); + return (NULL); + } + + if ((ehdr = _libelf_ehdr(e, ec, 0)) == NULL) + return (NULL); + + /* + * The application may be asking for a new section descriptor + * on an ELF object opened with ELF_C_RDWR or ELF_C_READ. We + * need to bring in the existing section information before + * appending a new one to the list. + * + * Per the ELF(3) API, an application is allowed to open a + * file using ELF_C_READ, mess with its internal structure and + * use elf_update(...,ELF_C_NULL) to compute its new layout. + */ + if ((e->e_cmd == ELF_C_READ + || (e->e_cmd == ELF_C_RDWR && e->e_fd == -1)) && + (e->e_flags & LIBELF_F_SHDRS_LOADED) == 0 && + _libelf_load_section_headers(e, ehdr) == 0) + return (NULL); + + if (STAILQ_EMPTY(&e->e_u.e_elf.e_scn)) { + assert(e->e_u.e_elf.e_nscn == 0); + if ((scn = _libelf_allocate_scn(e, (size_t) SHN_UNDEF)) == + NULL) + return (NULL); + e->e_u.e_elf.e_nscn++; + } + + assert(e->e_u.e_elf.e_nscn > 0); + + if ((scn = _libelf_allocate_scn(e, e->e_u.e_elf.e_nscn)) == NULL) + return (NULL); + + e->e_u.e_elf.e_nscn++; + + (void) elf_flagscn(scn, ELF_C_SET, ELF_F_DIRTY); + + return (scn); +} + +Elf_Scn * +elf_nextscn(Elf *e, Elf_Scn *s) +{ + if (e == NULL || (e->e_kind != ELF_K_ELF) || + (s && s->s_elf != e)) { + LIBELF_SET_ERROR(ARGUMENT, 0); + return (NULL); + } + + return (s == NULL ? elf_getscn(e, (size_t) 1) : + STAILQ_NEXT(s, s_next)); +} diff --git a/rocclr/compiler/lib/loaders/elf/utils/libelf/elf_shnum.c b/rocclr/compiler/lib/loaders/elf/utils/libelf/elf_shnum.c new file mode 100644 index 0000000000..515027a083 --- /dev/null +++ b/rocclr/compiler/lib/loaders/elf/utils/libelf/elf_shnum.c @@ -0,0 +1,67 @@ +/*- + * Copyright (c) 2006,2008 Joseph Koshy + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include + +#include +#include + +#include "_libelf.h" + +LIBELF_VCSID("$Id: elf_shnum.c 466 2009-08-04 17:17:42Z jkoshy $"); + +static int +_libelf_getshdrnum(Elf *e, size_t *shnum) +{ + void *eh; + int ec; + + if (e == NULL || e->e_kind != ELF_K_ELF || + ((ec = e->e_class) != ELFCLASS32 && ec != ELFCLASS64)) { + LIBELF_SET_ERROR(ARGUMENT, 0); + return (-1); + } + + if ((eh = _libelf_ehdr(e, ec, 0)) == NULL) + return (-1); + + *shnum = e->e_u.e_elf.e_nscn; + + return (0); +} + +int +elf_getshdrnum(Elf *e, size_t *shnum) +{ + return (_libelf_getshdrnum(e, shnum)); +} + +/* Deprecated API. */ +int +elf_getshnum(Elf *e, size_t *shnum) +{ + return (_libelf_getshdrnum(e, shnum) >= 0); +} diff --git a/rocclr/compiler/lib/loaders/elf/utils/libelf/elf_shstrndx.c b/rocclr/compiler/lib/loaders/elf/utils/libelf/elf_shstrndx.c new file mode 100644 index 0000000000..bac14b48f1 --- /dev/null +++ b/rocclr/compiler/lib/loaders/elf/utils/libelf/elf_shstrndx.c @@ -0,0 +1,82 @@ +/*- + * Copyright (c) 2006,2008 Joseph Koshy + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include + +#include +#include + +#include "_libelf.h" + +LIBELF_VCSID("$Id: elf_shstrndx.c 466 2009-08-04 17:17:42Z jkoshy $"); + +static int +_libelf_getshdrstrndx(Elf *e, size_t *strndx) +{ + void *eh; + int ec; + + if (e == NULL || e->e_kind != ELF_K_ELF || + ((ec = e->e_class) != ELFCLASS32 && ec != ELFCLASS64)) { + LIBELF_SET_ERROR(ARGUMENT, 0); + return (-1); + } + + if ((eh = _libelf_ehdr(e, ec, 0)) == NULL) + return (-1); + + *strndx = e->e_u.e_elf.e_strndx; + + return (0); +} + +int +elf_getshdrstrndx(Elf *e, size_t *strndx) +{ + return (_libelf_getshdrstrndx(e, strndx)); +} + +int +elf_getshstrndx(Elf *e, size_t *strndx) /* Deprecated API. */ +{ + return (_libelf_getshdrstrndx(e, strndx) >= 0); +} + +int +elf_setshstrndx(Elf *e, size_t strndx) +{ + void *eh; + int ec; + + if (e == NULL || e->e_kind != ELF_K_ELF || + ((ec = e->e_class) != ELFCLASS32 && ec != ELFCLASS64) || + ((eh = _libelf_ehdr(e, ec, 0)) == NULL)) { + LIBELF_SET_ERROR(ARGUMENT, 0); + return (0); + } + + return (_libelf_setshstrndx(e, eh, ec, strndx)); +} diff --git a/rocclr/compiler/lib/loaders/elf/utils/libelf/elf_strptr.c b/rocclr/compiler/lib/loaders/elf/utils/libelf/elf_strptr.c new file mode 100644 index 0000000000..2f50a20200 --- /dev/null +++ b/rocclr/compiler/lib/loaders/elf/utils/libelf/elf_strptr.c @@ -0,0 +1,134 @@ +/*- + * Copyright (c) 2006,2008 Joseph Koshy + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include +#include + +#include "_libelf.h" + +#ifdef ANDROID +#include "roundup.h" +#endif + +LIBELF_VCSID("$Id: elf_strptr.c 189 2008-07-20 10:38:08Z jkoshy $"); + +/* + * Convert an ELF section#,offset pair to a string pointer. + */ + +char * +elf_strptr(Elf *e, size_t scndx, size_t offset) +{ + Elf_Scn *s; + Elf_Data *d; + size_t alignment, count; + GElf_Shdr shdr; + + if (e == NULL || e->e_kind != ELF_K_ELF) { + LIBELF_SET_ERROR(ARGUMENT, 0); + return (NULL); + } + + if ((s = elf_getscn(e, scndx)) == NULL || + gelf_getshdr(s, &shdr) == NULL) + return (NULL); + + if (shdr.sh_type != SHT_STRTAB || + offset >= shdr.sh_size) { + LIBELF_SET_ERROR(ARGUMENT, 0); + return (NULL); + } + + d = NULL; + if (e->e_flags & ELF_F_LAYOUT) { + + /* + * The application is taking responsibility for the + * ELF object's layout, so we can directly translate + * an offset to a `char *' address using the `d_off' + * members of Elf_Data descriptors. + */ + while ((d = elf_getdata(s, d)) != NULL) { + + if (d->d_buf == 0 || d->d_size == 0) + continue; + + if (d->d_type != ELF_T_BYTE) { + LIBELF_SET_ERROR(DATA, 0); + return (NULL); + } + + if (offset >= d->d_off && + offset < d->d_off + d->d_size) + return ((char *) d->d_buf + offset - d->d_off); + } + } else { + /* + * Otherwise, the `d_off' members are not useable and + * we need to compute offsets ourselves, taking into + * account 'holes' in coverage of the section introduced + * by alignment requirements. + */ + count = (size_t) 0; /* cumulative count of bytes seen */ + while ((d = elf_getdata(s, d)) != NULL && count <= offset) { + + if (d->d_buf == NULL || d->d_size == 0) + continue; + + if (d->d_type != ELF_T_BYTE) { + LIBELF_SET_ERROR(DATA, 0); + return (NULL); + } + + if ((alignment = d->d_align) > 1) { + if ((alignment & (alignment - 1)) != 0) { + LIBELF_SET_ERROR(DATA, 0); + return (NULL); + } + count = roundup2(count, alignment); + } + + if (offset < count) { + /* offset starts in the 'hole' */ + LIBELF_SET_ERROR(ARGUMENT, 0); + return (NULL); + } + + if (offset < count + d->d_size) { + if (d->d_buf != NULL) + return ((char *) d->d_buf + + offset - count); + LIBELF_SET_ERROR(DATA, 0); + return (NULL); + } + + count += d->d_size; + } + } + + LIBELF_SET_ERROR(ARGUMENT, 0); + return (NULL); +} diff --git a/rocclr/compiler/lib/loaders/elf/utils/libelf/elf_update.c b/rocclr/compiler/lib/loaders/elf/utils/libelf/elf_update.c new file mode 100644 index 0000000000..d93f61046a --- /dev/null +++ b/rocclr/compiler/lib/loaders/elf/utils/libelf/elf_update.c @@ -0,0 +1,1263 @@ +/*- + * Copyright (c) 2006-2011 Joseph Koshy + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ +#if !defined(WIN32) +#include +#include "queue.h" +#endif +#include + +#include +#include +#include +#include +#include +#include +#if !defined(WIN32) +#include +#else +#include +#ifndef PROT_READ +#define PROT_READ FILE_MAP_READ +#endif +#ifndef MAP_PRIVATE +#define MAP_PRIVATE FILE_MAP_COPY +#endif +#ifndef MAP_FAILED +#define MAP_FAILED NULL +#endif +#endif + +#include "_libelf.h" + +#ifdef ANDROID +#include "roundup.h" +#endif + +LIBELF_VCSID("$Id: elf_update.c 1922 2011-09-23 08:04:33Z jkoshy $"); + +/* + * Layout strategy: + * + * - Case 1: ELF_F_LAYOUT is asserted + * In this case the application has full control over where the + * section header table, program header table, and section data + * will reside. The library only perform error checks. + * + * - Case 2: ELF_F_LAYOUT is not asserted + * + * The library will do the object layout using the following + * ordering: + * - The executable header is placed first, are required by the + * ELF specification. + * - The program header table is placed immediately following the + * executable header. + * - Section data, if any, is placed after the program header + * table, aligned appropriately. + * - The section header table, if needed, is placed last. + * + * There are two sub-cases to be taken care of: + * + * - Case 2a: e->e_cmd == ELF_C_READ or ELF_C_RDWR + * + * In this sub-case, the underlying ELF object may already have + * content in it, which the application may have modified. The + * library will retrieve content from the existing object as + * needed. + * + * - Case 2b: e->e_cmd == ELF_C_WRITE + * + * The ELF object is being created afresh in this sub-case; + * there is no pre-existing content in the underlying ELF + * object. + */ + +/* + * The types of extents in an ELF object. + */ +enum elf_extent { + ELF_EXTENT_EHDR, + ELF_EXTENT_PHDR, + ELF_EXTENT_SECTION, + ELF_EXTENT_SHDR +}; + +/* + * A extent descriptor, used when laying out an ELF object. + */ +struct _Elf_Extent { + ELF_SLIST_ENTRY(_Elf_Extent) ex_next; + uint64_t ex_start; /* Start of the region. */ + uint64_t ex_size; /* The size of the region. */ + enum elf_extent ex_type; /* Type of region. */ + void *ex_desc; /* Associated descriptor. */ +}; + +ELF_SLIST_HEAD(_Elf_Extent_List, _Elf_Extent); + +/* + * Compute the extents of a section, by looking at the data + * descriptors associated with it. The function returns 1 + * if successful, or zero if an error was detected. + */ +static int +_libelf_compute_section_extents(Elf *e, Elf_Scn *s, off_t rc) +{ + int ec; + size_t fsz, msz; + Elf_Data *d; + Elf32_Shdr *shdr32; + Elf64_Shdr *shdr64; + uint32_t sh_type; + uint64_t d_align; + unsigned int elftype; + uint64_t scn_size, scn_alignment; + uint64_t sh_align, sh_entsize, sh_offset, sh_size; + + ec = e->e_class; + + shdr32 = &s->s_shdr.s_shdr32; + shdr64 = &s->s_shdr.s_shdr64; + if (ec == ELFCLASS32) { + sh_type = shdr32->sh_type; + sh_align = (uint64_t) shdr32->sh_addralign; + sh_entsize = (uint64_t) shdr32->sh_entsize; + sh_offset = (uint64_t) shdr32->sh_offset; + sh_size = (uint64_t) shdr32->sh_size; + } else { + sh_type = shdr64->sh_type; + sh_align = shdr64->sh_addralign; + sh_entsize = shdr64->sh_entsize; + sh_offset = shdr64->sh_offset; + sh_size = shdr64->sh_size; + } + + assert(sh_type != SHT_NULL && sh_type != SHT_NOBITS); + + elftype = _libelf_xlate_shtype(sh_type); + if (elftype > ELF_T_LAST) { + LIBELF_SET_ERROR(SECTION, 0); + return (0); + } + + if (sh_align == 0) + sh_align = _libelf_falign(elftype, ec); + + /* + * Compute the section's size and alignment using the data + * descriptors associated with the section. + */ + if (STAILQ_EMPTY(&s->s_data)) { + /* + * The section's content (if any) has not been read in + * yet. If section is not dirty marked dirty, we can + * reuse the values in the 'sh_size' and 'sh_offset' + * fields of the section header. + */ + if ((s->s_flags & ELF_F_DIRTY) == 0) { + /* + * If the library is doing the layout, then we + * compute the new start offset for the + * section based on the current offset and the + * section's alignment needs. + * + * If the application is doing the layout, we + * can use the value in the 'sh_offset' field + * in the section header directly. + */ + if (e->e_flags & ELF_F_LAYOUT) + goto updatedescriptor; + else + goto computeoffset; + } + + /* + * Otherwise, we need to bring in the section's data + * from the underlying ELF object. + */ + if (e->e_cmd != ELF_C_WRITE && elf_getdata(s, NULL) == NULL) + return (0); + } + + /* + * Loop through the section's data descriptors. + */ + scn_size = 0L; + scn_alignment = 0; + STAILQ_FOREACH(d, &s->s_data, d_next) { + + /* + * The data buffer's type is known. + */ + if (d->d_type >= ELF_T_NUM) { + LIBELF_SET_ERROR(DATA, 0); + return (0); + } + + /* + * The data buffer's version is supported. + */ + if (d->d_version != e->e_version) { + LIBELF_SET_ERROR(VERSION, 0); + return (0); + } + + /* + * The buffer's alignment is non-zero and a power of + * two. + */ + if ((d_align = d->d_align) == 0 || + (d_align & (d_align - 1))) { + LIBELF_SET_ERROR(DATA, 0); + return (0); + } + + /* + * The buffer's size should be a multiple of the + * memory size of the underlying type. + */ + msz = _libelf_msize(d->d_type, ec, e->e_version); + if (d->d_size % msz) { + LIBELF_SET_ERROR(DATA, 0); + return (0); + } + + /* + * If the application is controlling layout, then the + * d_offset field should be compatible with the + * buffer's specified alignment. + */ + if ((e->e_flags & ELF_F_LAYOUT) && + (d->d_off & (d_align - 1))) { + LIBELF_SET_ERROR(LAYOUT, 0); + return (0); + } + + /* + * Compute the section's size. + */ + if (e->e_flags & ELF_F_LAYOUT) { + if ((uint64_t) d->d_off + d->d_size > scn_size) + scn_size = d->d_off + d->d_size; + } else { + scn_size = roundup2(scn_size, d->d_align); + d->d_off = scn_size; + fsz = _libelf_fsize(d->d_type, ec, d->d_version, + d->d_size / msz); + scn_size += fsz; + } + + /* + * The section's alignment is the maximum alignment + * needed for its data buffers. + */ + if (d_align > scn_alignment) + scn_alignment = d_align; + } + + + /* + * If the application is requesting full control over the + * layout of the section, check the section's specified size, + * offsets and alignment for sanity. + */ + if (e->e_flags & ELF_F_LAYOUT) { + if (scn_alignment > sh_align || sh_offset % sh_align || + sh_size < scn_size) { + LIBELF_SET_ERROR(LAYOUT, 0); + return (0); + } + goto updatedescriptor; + } + + /* + * Otherwise, compute the values in the section header. + * + * The section alignment is the maximum alignment for any of + * its contained data descriptors. + */ + if (scn_alignment > sh_align) + sh_align = scn_alignment; + + /* + * If the section entry size is zero, try and fill in an + * appropriate entry size. Per the elf(5) manual page + * sections without fixed-size entries should have their + * 'sh_entsize' field set to zero. + */ + if (sh_entsize == 0 && + (sh_entsize = _libelf_fsize(elftype, ec, e->e_version, + (size_t) 1)) == 1) + sh_entsize = 0; + + sh_size = scn_size; + +computeoffset: + /* + * Compute the new offset for the section based on + * the section's alignment needs. + */ + sh_offset = roundup(rc, sh_align); + + /* + * Update the section header. + */ + if (ec == ELFCLASS32) { + shdr32->sh_addralign = (uint32_t) sh_align; + shdr32->sh_entsize = (uint32_t) sh_entsize; + shdr32->sh_offset = (uint32_t) sh_offset; + shdr32->sh_size = (uint32_t) sh_size; + } else { + shdr64->sh_addralign = sh_align; + shdr64->sh_entsize = sh_entsize; + shdr64->sh_offset = sh_offset; + shdr64->sh_size = sh_size; + } + +updatedescriptor: + /* + * Update the section descriptor. + */ + s->s_size = sh_size; + s->s_offset = sh_offset; + + return (1); +} + +/* + * Free a list of extent descriptors. + */ + +static void +_libelf_release_extents(struct _Elf_Extent_List *extents) +{ + struct _Elf_Extent *ex; + + while ((ex = ELF_SLIST_FIRST(extents)) != NULL) { + ELF_SLIST_REMOVE_HEAD(extents, ex_next); + free(ex); + } +} + +/* + * Check if an extent 's' defined by [start..start+size) is free. + * This routine assumes that the given extent list is sorted in order + * of ascending extent offsets. + */ + +static int +_libelf_extent_is_unused(struct _Elf_Extent_List *extents, + const uint64_t start, const uint64_t size, struct _Elf_Extent **prevt) +{ + uint64_t tmax, tmin; + struct _Elf_Extent *t, *pt; + const uint64_t smax = start + size; + + /* First, look for overlaps with existing extents. */ + pt = NULL; + ELF_SLIST_FOREACH(t, extents, ex_next) { + tmin = t->ex_start; + tmax = tmin + t->ex_size; + + if (tmax <= start) { + /* + * 't' lies entirely before 's': ...| t |...| s |... + */ + pt = t; + continue; + } else if (smax <= tmin) { + /* + * 's' lies entirely before 't', and after 'pt': + * ...| pt |...| s |...| t |... + */ + assert(pt == NULL || + pt->ex_start + pt->ex_size <= start); + break; + } else + /* 's' and 't' overlap. */ + return (0); + } + + if (prevt) + *prevt = pt; + return (1); +} + +/* + * Insert an extent into the list of extents. + */ + +static int +_libelf_insert_extent(struct _Elf_Extent_List *extents, int type, + uint64_t start, uint64_t size, void *desc) +{ + struct _Elf_Extent *ex, *prevt; + + assert(type >= ELF_EXTENT_EHDR && type <= ELF_EXTENT_SHDR); + + prevt = NULL; + + /* + * If the requested range overlaps with an existing extent, + * signal an error. + */ + if (!_libelf_extent_is_unused(extents, start, size, &prevt)) { + LIBELF_SET_ERROR(LAYOUT, 0); + return (0); + } + + /* Allocate and fill in a new extent descriptor. */ + if ((ex = malloc(sizeof(struct _Elf_Extent))) == NULL) { + LIBELF_SET_ERROR(RESOURCE, errno); + return (0); + } + ex->ex_start = start; + ex->ex_size = size; + ex->ex_desc = desc; + ex->ex_type = type; + + /* Insert the region descriptor into the list. */ + if (prevt) + ELF_SLIST_INSERT_AFTER(prevt, ex, ex_next); + else + ELF_SLIST_INSERT_HEAD(extents, ex, ex_next); + return (1); +} + +/* + * Recompute section layout. + */ + +static off_t +_libelf_resync_sections(Elf *e, off_t rc, struct _Elf_Extent_List *extents) +{ + int ec; + Elf_Scn *s; + size_t sh_type; + + ec = e->e_class; + + /* + * Make a pass through sections, computing the extent of each + * section. + */ + STAILQ_FOREACH(s, &e->e_u.e_elf.e_scn, s_next) { + if (ec == ELFCLASS32) + sh_type = s->s_shdr.s_shdr32.sh_type; + else + sh_type = s->s_shdr.s_shdr64.sh_type; + + if (sh_type == SHT_NOBITS || sh_type == SHT_NULL) + continue; + + if (_libelf_compute_section_extents(e, s, rc) == 0) + return ((off_t) -1); + + if (s->s_size == 0) + continue; + + if (!_libelf_insert_extent(extents, ELF_EXTENT_SECTION, + s->s_offset, s->s_size, s)) + return ((off_t) -1); + + if ((size_t) rc < s->s_offset + s->s_size) + rc = s->s_offset + s->s_size; + } + + return (rc); +} + +/* + * Recompute the layout of the ELF object and update the internal data + * structures associated with the ELF descriptor. + * + * Returns the size in bytes the ELF object would occupy in its file + * representation. + * + * After a successful call to this function, the following structures + * are updated: + * + * - The ELF header is updated. + * - All extents in the ELF object are sorted in order of ascending + * addresses. Sections have their section header table entries + * updated. An error is signalled if an overlap was detected among + * extents. + * - Data descriptors associated with sections are checked for valid + * types, offsets and alignment. + * + * After a resync_elf() successfully returns, the ELF descriptor is + * ready for being handed over to _libelf_write_elf(). + */ + +static off_t +_libelf_resync_elf(Elf *e, struct _Elf_Extent_List *extents) +{ + int ec, eh_class, eh_type; + unsigned int eh_byteorder, eh_version; + size_t align, fsz; + size_t phnum, shnum; + off_t rc, phoff, shoff; + void *ehdr, *phdr; + Elf32_Ehdr *eh32; + Elf64_Ehdr *eh64; + + rc = 0; + + ec = e->e_class; + + assert(ec == ELFCLASS32 || ec == ELFCLASS64); + + /* + * Prepare the EHDR. + */ + if ((ehdr = _libelf_ehdr(e, ec, 0)) == NULL) + return ((off_t) -1); + + eh32 = ehdr; + eh64 = ehdr; + + if (ec == ELFCLASS32) { + eh_byteorder = eh32->e_ident[EI_DATA]; + eh_class = eh32->e_ident[EI_CLASS]; + phoff = (uint64_t) eh32->e_phoff; + shoff = (uint64_t) eh32->e_shoff; + eh_type = eh32->e_type; + eh_version = eh32->e_version; + } else { + eh_byteorder = eh64->e_ident[EI_DATA]; + eh_class = eh64->e_ident[EI_CLASS]; + phoff = eh64->e_phoff; + shoff = eh64->e_shoff; + eh_type = eh64->e_type; + eh_version = eh64->e_version; + } + + if (eh_version == EV_NONE) + eh_version = EV_CURRENT; + + if (eh_version != e->e_version) { /* always EV_CURRENT */ + LIBELF_SET_ERROR(VERSION, 0); + return ((off_t) -1); + } + + if (eh_class != e->e_class) { + LIBELF_SET_ERROR(CLASS, 0); + return ((off_t) -1); + } + + if ((e->e_cmd == ELF_C_READ + || (e->e_cmd == ELF_C_RDWR && e->e_rawfile)) + && eh_byteorder != e->e_byteorder) { + LIBELF_SET_ERROR(HEADER, 0); + return ((off_t) -1); + } + + shnum = e->e_u.e_elf.e_nscn; + phnum = e->e_u.e_elf.e_nphdr; + + e->e_byteorder = eh_byteorder; + +#define INITIALIZE_EHDR(E,EC,V) do { \ + (E)->e_ident[EI_MAG0] = ELFMAG0; \ + (E)->e_ident[EI_MAG1] = ELFMAG1; \ + (E)->e_ident[EI_MAG2] = ELFMAG2; \ + (E)->e_ident[EI_MAG3] = ELFMAG3; \ + (E)->e_ident[EI_CLASS] = (EC); \ + (E)->e_ident[EI_VERSION] = (V); \ + (E)->e_ehsize = _libelf_fsize(ELF_T_EHDR, (EC), (V), \ + (size_t) 1); \ + (E)->e_phentsize = (phnum == 0) ? 0 : _libelf_fsize( \ + ELF_T_PHDR, (EC), (V), (size_t) 1); \ + (E)->e_shentsize = _libelf_fsize(ELF_T_SHDR, (EC), (V), \ + (size_t) 1); \ + } while (0) + + if (ec == ELFCLASS32) + INITIALIZE_EHDR(eh32, ec, eh_version); + else + INITIALIZE_EHDR(eh64, ec, eh_version); + + (void) elf_flagehdr(e, ELF_C_SET, ELF_F_DIRTY); + + rc += _libelf_fsize(ELF_T_EHDR, ec, eh_version, (size_t) 1); + + if (!_libelf_insert_extent(extents, ELF_EXTENT_EHDR, 0, rc, ehdr)) + return ((off_t) -1); + + /* + * Compute the layout the program header table, if one is + * present. The program header table needs to be aligned to a + * `natural' boundary. + */ + if (phnum) { + fsz = _libelf_fsize(ELF_T_PHDR, ec, eh_version, phnum); + align = _libelf_falign(ELF_T_PHDR, ec); + + if (e->e_flags & ELF_F_LAYOUT) { + /* + * Check offsets for sanity. + */ + if (rc > phoff) { + LIBELF_SET_ERROR(LAYOUT, 0); + return ((off_t) -1); + } + + if (phoff % align) { + LIBELF_SET_ERROR(LAYOUT, 0); + return ((off_t) -1); + } + + } else + phoff = roundup(rc, align); + + rc = phoff + fsz; + + phdr = _libelf_getphdr(e, ec); + + if (!_libelf_insert_extent(extents, ELF_EXTENT_PHDR, phoff, + fsz, phdr)) + return ((off_t) -1); + } else + phoff = 0; + + /* + * Compute the layout of the sections associated with the + * file. + */ + /* + * If we are a read only elf that has not had its + * headers loaded, or a read/write elf that is not + * based on a file descripter and had its headers + * loaded, then lets load the headers. + * If the loading of the headers fails, return -1. + */ + if ((e->e_cmd == ELF_C_READ + || (e->e_cmd == ELF_C_RDWR && e->e_rawfile + && e->e_fd == -1)) && + (e->e_flags & LIBELF_F_SHDRS_LOADED) == 0 && + _libelf_load_section_headers(e, ehdr) == 0) + return ((off_t) -1); + + if ((rc = _libelf_resync_sections(e, rc, extents)) < 0) + return ((off_t) -1); + + /* + * Compute the space taken up by the section header table, if + * one is needed. + * + * If ELF_F_LAYOUT has been asserted, the application may have + * placed the section header table in between existing + * sections, so the net size of the file need not increase due + * to the presence of the section header table. + * + * If the library is responsible for laying out the object, + * the section header table is placed after section data. + */ + if (shnum) { + fsz = _libelf_fsize(ELF_T_SHDR, ec, eh_version, shnum); + align = _libelf_falign(ELF_T_SHDR, ec); + + if (e->e_flags & ELF_F_LAYOUT) { + if (shoff % align) { + LIBELF_SET_ERROR(LAYOUT, 0); + return ((off_t) -1); + } + } else + shoff = roundup(rc, align); + + if (shoff + fsz > (size_t) rc) + rc = shoff + fsz; + + if (!_libelf_insert_extent(extents, ELF_EXTENT_SHDR, shoff, + fsz, NULL)) + return ((off_t) -1); + } else + shoff = 0; + + /* + * Set the fields of the Executable Header that could potentially use + * extended numbering. + */ + _libelf_setphnum(e, ehdr, ec, phnum); + _libelf_setshnum(e, ehdr, ec, shnum); + + /* + * Update the `e_phoff' and `e_shoff' fields if the library is + * doing the layout. + */ + if ((e->e_flags & ELF_F_LAYOUT) == 0) { + if (ec == ELFCLASS32) { + eh32->e_phoff = (uint32_t) phoff; + eh32->e_shoff = (uint32_t) shoff; + } else { + eh64->e_phoff = (uint64_t) phoff; + eh64->e_shoff = (uint64_t) shoff; + } + } + + return (rc); +} + +/* + * Write out the contents of an ELF section. + */ + +static size_t +_libelf_write_scn(Elf *e, char *nf, struct _Elf_Extent *ex) +{ + int ec; + size_t fsz, msz, nobjects, rc; + uint32_t sh_type; + uint64_t sh_off, sh_size; + int elftype; + Elf_Scn *s; + Elf_Data *d, dst; + + assert(ex->ex_type == ELF_EXTENT_SECTION); + + s = ex->ex_desc; + rc = ex->ex_start; + + if ((ec = e->e_class) == ELFCLASS32) { + sh_type = s->s_shdr.s_shdr32.sh_type; + sh_size = (uint64_t) s->s_shdr.s_shdr32.sh_size; + } else { + sh_type = s->s_shdr.s_shdr64.sh_type; + sh_size = s->s_shdr.s_shdr64.sh_size; + } + + /* + * Ignore sections that do not allocate space in the file. + */ + if (sh_type == SHT_NOBITS || sh_type == SHT_NULL || sh_size == 0) + return (rc); + + elftype = _libelf_xlate_shtype(sh_type); + assert(elftype >= ELF_T_FIRST && elftype <= ELF_T_LAST); + + sh_off = s->s_offset; + assert(sh_off % _libelf_falign(elftype, ec) == 0); + + /* + * If the section has a `rawdata' descriptor, and the section + * contents have not been modified, use its contents directly. + * The `s_rawoff' member contains the offset into the original + * file, while `s_offset' contains its new location in the + * destination. + */ + + /* If we are a read/write elf, we cannot trust the rawdata. */ + if (e->e_cmd != ELF_C_RDWR) { + if (STAILQ_EMPTY(&s->s_data)) { + + if ((d = elf_rawdata(s, NULL)) == NULL) + return ((off_t) -1); + + STAILQ_FOREACH(d, &s->s_rawdata, d_next) { + if ((uint64_t) rc < sh_off + d->d_off) + (void) memset(nf + rc, + LIBELF_PRIVATE(fillchar), sh_off + + d->d_off - rc); + rc = sh_off + d->d_off; + + assert(d->d_buf != NULL); + assert(d->d_type == ELF_T_BYTE); + assert(d->d_version == e->e_version); + + (void) memcpy(nf + rc, + e->e_rawfile + s->s_rawoff + d->d_off, d->d_size); + + rc += d->d_size; + } + + return (rc); + } + } + + /* + * Iterate over the set of data descriptors for this section. + * The prior call to _libelf_resync_elf() would have setup the + * descriptors for this step. + */ + + dst.d_version = e->e_version; + + STAILQ_FOREACH(d, &s->s_data, d_next) { + + msz = _libelf_msize(d->d_type, ec, e->e_version); + + if ((uint64_t) rc < sh_off + d->d_off) + (void) memset(nf + rc, + LIBELF_PRIVATE(fillchar), sh_off + d->d_off - rc); + + rc = sh_off + d->d_off; + + assert(d->d_buf != NULL); + assert(d->d_version == e->e_version); + assert(d->d_size % msz == 0); + + nobjects = d->d_size / msz; + + fsz = _libelf_fsize(d->d_type, ec, e->e_version, nobjects); + + dst.d_buf = nf + rc; + dst.d_size = fsz; + + if (_libelf_xlate(&dst, d, e->e_byteorder, ec, ELF_TOFILE) == + NULL) + return ((off_t) -1); + + rc += fsz; + } + + return ((off_t) rc); +} + +/* + * Write out an ELF Executable Header. + */ + +static off_t +_libelf_write_ehdr(Elf *e, char *nf, struct _Elf_Extent *ex) +{ + int ec; + void *ehdr; + size_t fsz, msz; + Elf_Data dst, src; + + assert(ex->ex_type == ELF_EXTENT_EHDR); + assert(ex->ex_start == 0); /* Ehdr always comes first. */ + + ec = e->e_class; + + ehdr = _libelf_ehdr(e, ec, 0); + assert(ehdr != NULL); + + fsz = _libelf_fsize(ELF_T_EHDR, ec, e->e_version, (size_t) 1); + msz = _libelf_msize(ELF_T_EHDR, ec, e->e_version); + + (void) memset(&dst, 0, sizeof(dst)); + (void) memset(&src, 0, sizeof(src)); + + src.d_buf = ehdr; + src.d_size = msz; + src.d_type = ELF_T_EHDR; + src.d_version = dst.d_version = e->e_version; + + dst.d_buf = nf; + dst.d_size = fsz; + + if (_libelf_xlate(&dst, &src, e->e_byteorder, ec, ELF_TOFILE) == + NULL) + return ((off_t) -1); + + return ((off_t) fsz); +} + +/* + * Write out an ELF program header table. + */ + +static off_t +_libelf_write_phdr(Elf *e, char *nf, struct _Elf_Extent *ex) +{ + int ec; + void *ehdr; + Elf32_Ehdr *eh32; + Elf64_Ehdr *eh64; + Elf_Data dst, src; + size_t fsz, phnum; + uint64_t phoff; + + assert(ex->ex_type == ELF_EXTENT_PHDR); + + ec = e->e_class; + ehdr = _libelf_ehdr(e, ec, 0); + phnum = e->e_u.e_elf.e_nphdr; + + assert(phnum > 0); + + if (ec == ELFCLASS32) { + eh32 = (Elf32_Ehdr *) ehdr; + phoff = (uint64_t) eh32->e_phoff; + } else { + eh64 = (Elf64_Ehdr *) ehdr; + phoff = eh64->e_phoff; + } + + assert(phoff > 0); + assert(ex->ex_start == phoff); + assert(phoff % _libelf_falign(ELF_T_PHDR, ec) == 0); + + (void) memset(&dst, 0, sizeof(dst)); + (void) memset(&src, 0, sizeof(src)); + + fsz = _libelf_fsize(ELF_T_PHDR, ec, e->e_version, phnum); + assert(fsz > 0); + + src.d_buf = _libelf_getphdr(e, ec); + src.d_version = dst.d_version = e->e_version; + src.d_type = ELF_T_PHDR; + src.d_size = phnum * _libelf_msize(ELF_T_PHDR, ec, + e->e_version); + + dst.d_size = fsz; + dst.d_buf = nf + ex->ex_start; + + if (_libelf_xlate(&dst, &src, e->e_byteorder, ec, ELF_TOFILE) == + NULL) + return ((off_t) -1); + + return (phoff + fsz); +} + +/* + * Write out an ELF section header table. + */ + +static off_t +_libelf_write_shdr(Elf *e, char *nf, struct _Elf_Extent *ex) +{ + int ec; + void *ehdr; + Elf_Scn *scn; + uint64_t shoff; + Elf32_Ehdr *eh32; + Elf64_Ehdr *eh64; + size_t fsz, nscn; + Elf_Data dst, src; + + assert(ex->ex_type == ELF_EXTENT_SHDR); + + ec = e->e_class; + ehdr = _libelf_ehdr(e, ec, 0); + nscn = e->e_u.e_elf.e_nscn; + + if (ec == ELFCLASS32) { + eh32 = (Elf32_Ehdr *) ehdr; + shoff = (uint64_t) eh32->e_shoff; + } else { + eh64 = (Elf64_Ehdr *) ehdr; + shoff = eh64->e_shoff; + } + + assert(nscn > 0); + assert(shoff % _libelf_falign(ELF_T_SHDR, ec) == 0); + assert(ex->ex_start == shoff); + + (void) memset(&dst, 0, sizeof(dst)); + (void) memset(&src, 0, sizeof(src)); + + src.d_type = ELF_T_SHDR; + src.d_size = _libelf_msize(ELF_T_SHDR, ec, e->e_version); + src.d_version = dst.d_version = e->e_version; + + fsz = _libelf_fsize(ELF_T_SHDR, ec, e->e_version, (size_t) 1); + + STAILQ_FOREACH(scn, &e->e_u.e_elf.e_scn, s_next) { + if (ec == ELFCLASS32) + src.d_buf = &scn->s_shdr.s_shdr32; + else + src.d_buf = &scn->s_shdr.s_shdr64; + + dst.d_size = fsz; + dst.d_buf = nf + ex->ex_start + scn->s_ndx * fsz; + + if (_libelf_xlate(&dst, &src, e->e_byteorder, ec, + ELF_TOFILE) == NULL) + return ((off_t) -1); + } + + return (ex->ex_start + nscn * fsz); +} + +/* + * Update the elf file image. + * + * The original file could have been mapped in with an ELF_C_RDWR + * command and the application could have added new content or + * re-arranged its sections before calling elf_update(). Consequently + * its not safe to work `in place' on the original file. So we + * malloc() the required space for the updated ELF object and build + * the object there and write it out to the underlying file at the + * end. Note that the application may have opened the underlying file + * in ELF_C_RDWR and only retrieved/modified a few sections. We take + * care to avoid translating file sections unnecessarily. + * + * Gaps in the coverage of the file by the file's sections will be + * filled with the fill character set by elf_fill(3). + */ + +static off_t +_libelf_update_elf(Elf *e, off_t newsize, struct _Elf_Extent_List *extents) +{ + off_t nrc, rc; + char *newfile; + struct _Elf_Extent *ex; + + assert(e->e_kind == ELF_K_ELF); + // There are two types of ELF_C_RDWR files, one that is based in + // memory and has a raw file and one that is based on a file + // descriptor and does not have a raw_file. Both are equally + // valid, so we don't special case here. + assert(e->e_cmd == ELF_C_RDWR || (e->e_cmd == ELF_C_WRITE && e->e_fd >= 0)); + + if ((newfile = e->e_mem.alloc((size_t) newsize)) == NULL) { + LIBELF_SET_ERROR(RESOURCE, errno); + return ((off_t) -1); + } + + nrc = rc = 0; + ELF_SLIST_FOREACH(ex, extents, ex_next) { + + /* Fill inter-extent gaps. */ + if (ex->ex_start > (size_t) rc) + (void) memset(newfile + rc, LIBELF_PRIVATE(fillchar), + ex->ex_start - rc); + + switch (ex->ex_type) { + case ELF_EXTENT_EHDR: + if ((nrc = _libelf_write_ehdr(e, newfile, ex)) < 0) + goto error; + break; + + case ELF_EXTENT_PHDR: + if ((nrc = _libelf_write_phdr(e, newfile, ex)) < 0) + goto error; + break; + + case ELF_EXTENT_SECTION: + if ((nrc = _libelf_write_scn(e, newfile, ex)) < 0) + goto error; + break; + + case ELF_EXTENT_SHDR: + if ((nrc = _libelf_write_shdr(e, newfile, ex)) < 0) + goto error; + break; + + default: + assert(0); + break; + } + + assert(ex->ex_start + ex->ex_size == (size_t) nrc); + assert(rc < nrc); + + rc = nrc; + } + + assert(rc == newsize); + + /* + * For regular files, throw away existing file content and + * unmap any existing mappings. + */ + if ((e->e_flags & LIBELF_F_SPECIAL_FILE) == 0 && e->e_fd >= 0) { +#if !defined(WIN32) +#define FTRUNC(A, B) ftruncate(A, (off_t)B) +#else +#define FTRUNC(A, B) _chsize(A, B) +#endif + if (FTRUNC(e->e_fd, 0) < 0 || + lseek(e->e_fd, (off_t) 0, SEEK_SET)) { + LIBELF_SET_ERROR(IO, errno); + goto error; + } + if (e->e_flags & LIBELF_F_RAWFILE_MMAP) { + assert(e->e_rawfile != NULL); + assert(e->e_cmd == ELF_C_RDWR); + if (munmap(e->e_rawfile, e->e_rawsize) < 0) { + LIBELF_SET_ERROR(IO, errno); + goto error; + } + } + } + + /* + * Write out the new contents. + */ + if (e->e_fd >= 0 && write(e->e_fd, newfile, (size_t) newsize) != newsize) { + LIBELF_SET_ERROR(IO, errno); + goto error; + } + + /* + * For files opened in ELF_C_RDWR mode, set up the new 'raw' + * contents. + */ + if (e->e_cmd == ELF_C_RDWR) { + if (e->e_rawfile) { + if (e->e_flags & LIBELF_F_RAWFILE_MMAP && e->e_fd >= 0) { + if ((e->e_rawfile = mmap(NULL, (size_t) newsize, + PROT_READ, MAP_PRIVATE, e->e_fd, (off_t) 0)) == + MAP_FAILED) { + LIBELF_SET_ERROR(IO, errno); + goto error; + } + } else if (e->e_flags & LIBELF_F_RAWFILE_MALLOC) { + e->e_mem.dealloc(e->e_rawfile); + e->e_rawfile = newfile; + newfile = NULL; + } + } else { + e->e_rawfile = newfile; + newfile = NULL; + e->e_flags |= LIBELF_F_RAWFILE_MALLOC; + } + /* Record the new size of the file. */ + e->e_rawsize = newsize; + } else { + /* File opened in ELF_C_WRITE mode. */ + assert(e->e_rawfile == NULL); + } + + /* Free the temporary buffer. */ + if (newfile) + e->e_mem.dealloc(newfile); + + return (rc); + +error: + e->e_mem.dealloc(newfile); + + return ((off_t) -1); +} + +/* + * Write out the file image. + * + * The original file could have been mapped in with an ELF_C_RDWR + * command and the application could have added new content or + * re-arranged its sections before calling elf_update(). Consequently + * its not safe to work `in place' on the original file. So we + * malloc() the required space for the updated ELF object and build + * the object there and write it out to the underlying file at the + * end. Note that the application may have opened the underlying file + * in ELF_C_RDWR and only retrieved/modified a few sections. We take + * care to avoid translating file sections unnecessarily. + * + * Gaps in the coverage of the file by the file's sections will be + * filled with the fill character set by elf_fill(3). + */ + +static off_t +_libelf_write_elf(Elf *e, off_t newsize, struct _Elf_Extent_List *extents) +{ + off_t rc; + Elf_Scn *scn, *tscn; + + rc = _libelf_update_elf(e, newsize, extents); + if (rc == (off_t)-1) + return (rc); + + /* + * Reset flags, remove existing section descriptors and + * {E,P}HDR pointers so that a subsequent elf_get{e,p}hdr() + * and elf_getscn() will function correctly. + */ + + e->e_flags &= ~ELF_F_DIRTY; + + STAILQ_FOREACH_SAFE(scn, &e->e_u.e_elf.e_scn, s_next, tscn) + _libelf_release_scn(scn); + + if (e->e_class == ELFCLASS32) { + e->e_mem.dealloc(e->e_u.e_elf.e_ehdr.e_ehdr32); + if (e->e_u.e_elf.e_phdr.e_phdr32) + e->e_mem.dealloc(e->e_u.e_elf.e_phdr.e_phdr32); + + e->e_u.e_elf.e_ehdr.e_ehdr32 = NULL; + e->e_u.e_elf.e_phdr.e_phdr32 = NULL; + } else { + e->e_mem.dealloc(e->e_u.e_elf.e_ehdr.e_ehdr64); + if (e->e_u.e_elf.e_phdr.e_phdr64) + e->e_mem.dealloc(e->e_u.e_elf.e_phdr.e_phdr64); + + e->e_u.e_elf.e_ehdr.e_ehdr64 = NULL; + e->e_u.e_elf.e_phdr.e_phdr64 = NULL; + } + + return (rc); +} + +/* + * Update an ELF object. + */ + +off_t +elf_update(Elf *e, Elf_Cmd c) +{ + int ec; + off_t rc; + struct _Elf_Extent_List extents; + + rc = (off_t) -1; + + if (e == NULL || e->e_kind != ELF_K_ELF || + (c != ELF_C_NULL && c != ELF_C_WRITE + && c != ELF_C_RDWR)) { + LIBELF_SET_ERROR(ARGUMENT, 0); + return (rc); + } + + if ((ec = e->e_class) != ELFCLASS32 && ec != ELFCLASS64) { + LIBELF_SET_ERROR(CLASS, 0); + return (rc); + } + + if (e->e_version == EV_NONE) + e->e_version = EV_CURRENT; + + if (c == ELF_C_WRITE && e->e_cmd == ELF_C_READ) { + LIBELF_SET_ERROR(MODE, 0); + return (rc); + } + + ELF_SLIST_INIT(&extents); + + if ((rc = _libelf_resync_elf(e, &extents)) < 0) + goto done; + + if (c == ELF_C_NULL) + goto done; + + if (c == ELF_C_WRITE && e->e_fd < 0) { + rc = (off_t) -1; + LIBELF_SET_ERROR(SEQUENCE, 0); + goto done; + } + + if (c == ELF_C_RDWR) { + rc = _libelf_update_elf(e, rc, &extents); + } else { + rc = _libelf_write_elf(e, rc, &extents); + } + +done: + _libelf_release_extents(&extents); + return (rc); +} + diff --git a/rocclr/compiler/lib/loaders/elf/utils/libelf/elf_version.c b/rocclr/compiler/lib/loaders/elf/utils/libelf/elf_version.c new file mode 100644 index 0000000000..48950f407d --- /dev/null +++ b/rocclr/compiler/lib/loaders/elf/utils/libelf/elf_version.c @@ -0,0 +1,52 @@ +/*- + * Copyright (c) 2006,2008 Joseph Koshy + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include + +#include + +#include "_libelf.h" + +LIBELF_VCSID("$Id: elf_version.c 189 2008-07-20 10:38:08Z jkoshy $"); + +unsigned int +elf_version(unsigned int v) +{ + unsigned int old; + + if ((old = LIBELF_PRIVATE(version)) == EV_NONE) + old = EV_CURRENT; + + if (v == EV_NONE) + return old; + if (v > EV_CURRENT) { + LIBELF_SET_ERROR(VERSION, 0); + return EV_NONE; + } + + LIBELF_PRIVATE(version) = v; + return (old); +} diff --git a/rocclr/compiler/lib/loaders/elf/utils/libelf/gelf.h b/rocclr/compiler/lib/loaders/elf/utils/libelf/gelf.h new file mode 100644 index 0000000000..152973824f --- /dev/null +++ b/rocclr/compiler/lib/loaders/elf/utils/libelf/gelf.h @@ -0,0 +1,114 @@ +/*- + * Copyright (c) 2006,2008 Joseph Koshy + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * $Id: gelf.h 1168 2010-09-04 01:03:25Z jkoshy $ + */ + +#ifndef _GELF_H_ +#define _GELF_H_ + +#include + +#include + +typedef Elf64_Addr GElf_Addr; /* Addresses */ +typedef Elf64_Half GElf_Half; /* Half words (16 bit) */ +typedef Elf64_Off GElf_Off; /* Offsets */ +typedef Elf64_Sword GElf_Sword; /* Signed words (32 bit) */ +typedef Elf64_Sxword GElf_Sxword; /* Signed long words (64 bit) */ +typedef Elf64_Word GElf_Word; /* Unsigned words (32 bit) */ +typedef Elf64_Xword GElf_Xword; /* Unsigned long words (64 bit) */ + +typedef Elf64_Dyn GElf_Dyn; /* ".dynamic" section entries */ +typedef Elf64_Ehdr GElf_Ehdr; /* ELF header */ +typedef Elf64_Phdr GElf_Phdr; /* Program header */ +typedef Elf64_Shdr GElf_Shdr; /* Section header */ +typedef Elf64_Sym GElf_Sym; /* Symbol table entries */ +typedef Elf64_Rel GElf_Rel; /* Relocation entries */ +typedef Elf64_Rela GElf_Rela; /* Relocation entries with addend */ + +#if (defined(__FreeBSD_version) && __FreeBSD_version >= 700025) || \ + (defined(__NetBSD_Version) && __NetBSD_Version > 400000003) +typedef Elf64_Cap GElf_Cap; /* SW/HW capabilities */ +typedef Elf64_Move GElf_Move; /* Move entries */ +typedef Elf64_Syminfo GElf_Syminfo; /* Symbol information */ +#endif + +#define GELF_M_INFO ELF64_M_INFO +#define GELF_M_SIZE ELF64_M_SIZE +#define GELF_M_SYM ELF64_M_SYM + +#define GELF_R_INFO ELF64_R_INFO +#define GELF_R_SYM ELF64_R_SYM +#define GELF_R_TYPE ELF64_R_TYPE +#define GELF_R_TYPE_DATA ELF64_R_TYPE_DATA +#define GELF_R_TYPE_ID ELF64_R_TYPE_ID +#define GELF_R_TYPE_INFO ELF64_R_TYPE_INFO + +#define GELF_ST_BIND ELF64_ST_BIND +#define GELF_ST_INFO ELF64_ST_INFO +#define GELF_ST_TYPE ELF64_ST_TYPE +#define GELF_ST_VISIBILITY ELF64_ST_VISIBILITY + +__BEGIN_DECLS +long gelf_checksum(Elf *_elf); +size_t gelf_fsize(Elf *_elf, Elf_Type _type, size_t _count, + unsigned int _version); +int gelf_getclass(Elf *_elf); +GElf_Dyn *gelf_getdyn(Elf_Data *_data, int _index, GElf_Dyn *_dst); +GElf_Ehdr *gelf_getehdr(Elf *_elf, GElf_Ehdr *_dst); +GElf_Phdr *gelf_getphdr(Elf *_elf, int _index, GElf_Phdr *_dst); +GElf_Rel *gelf_getrel(Elf_Data *_src, int _index, GElf_Rel *_dst); +GElf_Rela *gelf_getrela(Elf_Data *_src, int _index, GElf_Rela *_dst); +GElf_Shdr *gelf_getshdr(Elf_Scn *_scn, GElf_Shdr *_dst); +GElf_Sym *gelf_getsym(Elf_Data *_src, int _index, GElf_Sym *_dst); +GElf_Sym *gelf_getsymshndx(Elf_Data *_src, Elf_Data *_shindexsrc, + int _index, GElf_Sym *_dst, Elf32_Word *_shindexdst); +void * gelf_newehdr(Elf *_elf, int _class); +void * gelf_newphdr(Elf *_elf, size_t _phnum); +int gelf_update_dyn(Elf_Data *_dst, int _index, GElf_Dyn *_src); +int gelf_update_ehdr(Elf *_elf, GElf_Ehdr *_src); +int gelf_update_phdr(Elf *_elf, int _index, GElf_Phdr *_src); +int gelf_update_rel(Elf_Data *_dst, int _index, GElf_Rel *_src); +int gelf_update_rela(Elf_Data *_dst, int _index, GElf_Rela *_src); +int gelf_update_shdr(Elf_Scn *_dst, GElf_Shdr *_src); +int gelf_update_sym(Elf_Data *_dst, int _index, GElf_Sym *_src); +int gelf_update_symshndx(Elf_Data *_symdst, Elf_Data *_shindexdst, + int _index, GElf_Sym *_symsrc, Elf32_Word _shindexsrc); +Elf_Data *gelf_xlatetof(Elf *_elf, Elf_Data *_dst, const Elf_Data *_src, unsigned int _encode); +Elf_Data *gelf_xlatetom(Elf *_elf, Elf_Data *_dst, const Elf_Data *_src, unsigned int _encode); + +#if (defined(__FreeBSD_version) && __FreeBSD_version >= 700025) || \ + (defined(__NetBSD_Version) && __NetBSD_Version > 400000003) +GElf_Cap *gelf_getcap(Elf_Data *_data, int _index, GElf_Cap *_cap); +GElf_Move *gelf_getmove(Elf_Data *_src, int _index, GElf_Move *_dst); +GElf_Syminfo *gelf_getsyminfo(Elf_Data *_src, int _index, GElf_Syminfo *_dst); +int gelf_update_cap(Elf_Data *_dst, int _index, GElf_Cap *_src); +int gelf_update_move(Elf_Data *_dst, int _index, GElf_Move *_src); +int gelf_update_syminfo(Elf_Data *_dst, int _index, GElf_Syminfo *_src); +#endif +__END_DECLS + +#endif /* _GELF_H_ */ diff --git a/rocclr/compiler/lib/loaders/elf/utils/libelf/gelf_cap.c b/rocclr/compiler/lib/loaders/elf/utils/libelf/gelf_cap.c new file mode 100644 index 0000000000..68d34ca67d --- /dev/null +++ b/rocclr/compiler/lib/loaders/elf/utils/libelf/gelf_cap.c @@ -0,0 +1,148 @@ +/*- + * Copyright (c) 2006,2008 Joseph Koshy + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include + +#include +#include + +#include "_libelf.h" + +LIBELF_VCSID("$Id: gelf_cap.c 1166 2010-09-04 00:54:36Z jkoshy $"); + +#if LIBELF_CONFIG_CAP + +GElf_Cap * +gelf_getcap(Elf_Data *d, int ndx, GElf_Cap *dst) +{ + int ec; + Elf *e; + Elf_Scn *scn; + Elf32_Cap *cap32; + Elf64_Cap *cap64; + size_t msz; + uint32_t sh_type; + + if (d == NULL || ndx < 0 || dst == NULL || + (scn = d->d_scn) == NULL || + (e = scn->s_elf) == NULL) { + LIBELF_SET_ERROR(ARGUMENT, 0); + return (NULL); + } + + ec = e->e_class; + assert(ec == ELFCLASS32 || ec == ELFCLASS64); + + if (ec == ELFCLASS32) + sh_type = scn->s_shdr.s_shdr32.sh_type; + else + sh_type = scn->s_shdr.s_shdr64.sh_type; + + if (_libelf_xlate_shtype(sh_type) != ELF_T_CAP) { + LIBELF_SET_ERROR(ARGUMENT, 0); + return (NULL); + } + + msz = _libelf_msize(ELF_T_CAP, ec, e->e_version); + + assert(msz > 0); + + if (msz * ndx >= d->d_size) { + LIBELF_SET_ERROR(ARGUMENT, 0); + return (NULL); + } + + if (ec == ELFCLASS32) { + + cap32 = (Elf32_Cap *) d->d_buf + ndx; + + dst->c_tag = cap32->c_tag; + dst->c_un.c_val = (Elf64_Xword) cap32->c_un.c_val; + + } else { + + cap64 = (Elf64_Cap *) d->d_buf + ndx; + + *dst = *cap64; + } + + return (dst); +} + +int +gelf_update_cap(Elf_Data *d, int ndx, GElf_Cap *gc) +{ + int ec; + Elf *e; + Elf_Scn *scn; + Elf32_Cap *cap32; + Elf64_Cap *cap64; + size_t msz; + uint32_t sh_type; + + if (d == NULL || ndx < 0 || gc == NULL || + (scn = d->d_scn) == NULL || + (e = scn->s_elf) == NULL) { + LIBELF_SET_ERROR(ARGUMENT, 0); + return (0); + } + + ec = e->e_class; + assert(ec == ELFCLASS32 || ec == ELFCLASS64); + + if (ec == ELFCLASS32) + sh_type = scn->s_shdr.s_shdr32.sh_type; + else + sh_type = scn->s_shdr.s_shdr64.sh_type; + + if (_libelf_xlate_shtype(sh_type) != ELF_T_CAP) { + LIBELF_SET_ERROR(ARGUMENT, 0); + return (0); + } + + msz = _libelf_msize(ELF_T_CAP, ec, e->e_version); + assert(msz > 0); + + if (msz * ndx >= d->d_size) { + LIBELF_SET_ERROR(ARGUMENT, 0); + return (0); + } + + if (ec == ELFCLASS32) { + cap32 = (Elf32_Cap *) d->d_buf + ndx; + + LIBELF_COPY_U32(cap32, gc, c_tag); + LIBELF_COPY_U32(cap32, gc, c_un.c_val); + } else { + cap64 = (Elf64_Cap *) d->d_buf + ndx; + + *cap64 = *gc; + } + + return (1); +} + +#endif /* LIBELF_CONFIG_CAP */ diff --git a/rocclr/compiler/lib/loaders/elf/utils/libelf/gelf_checksum.c b/rocclr/compiler/lib/loaders/elf/utils/libelf/gelf_checksum.c new file mode 100644 index 0000000000..30fbb9784f --- /dev/null +++ b/rocclr/compiler/lib/loaders/elf/utils/libelf/gelf_checksum.c @@ -0,0 +1,58 @@ +/*- + * Copyright (c) 2006,2008 Joseph Koshy + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include + +#include +#include + +#include "_libelf.h" + +LIBELF_VCSID("$Id: gelf_checksum.c 189 2008-07-20 10:38:08Z jkoshy $"); + +long +elf32_checksum(Elf *e) +{ + return (_libelf_checksum(e, ELFCLASS32)); +} + +long +elf64_checksum(Elf *e) +{ + return (_libelf_checksum(e, ELFCLASS64)); +} + +long +gelf_checksum(Elf *e) +{ + int ec; + if (e == NULL || + ((ec = e->e_class) != ELFCLASS32 && ec != ELFCLASS64)) { + LIBELF_SET_ERROR(ARGUMENT, 0); + return (0L); + } + return (_libelf_checksum(e, ec)); +} diff --git a/rocclr/compiler/lib/loaders/elf/utils/libelf/gelf_dyn.c b/rocclr/compiler/lib/loaders/elf/utils/libelf/gelf_dyn.c new file mode 100644 index 0000000000..6a2885cd8f --- /dev/null +++ b/rocclr/compiler/lib/loaders/elf/utils/libelf/gelf_dyn.c @@ -0,0 +1,143 @@ +/*- + * Copyright (c) 2006,2008 Joseph Koshy + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include + +#include +#include + +#include "_libelf.h" + +LIBELF_VCSID("$Id: gelf_dyn.c 189 2008-07-20 10:38:08Z jkoshy $"); + +GElf_Dyn * +gelf_getdyn(Elf_Data *d, int ndx, GElf_Dyn *dst) +{ + int ec; + Elf *e; + Elf_Scn *scn; + Elf32_Dyn *dyn32; + Elf64_Dyn *dyn64; + size_t msz; + uint32_t sh_type; + + if (d == NULL || ndx < 0 || dst == NULL || + (scn = d->d_scn) == NULL || + (e = scn->s_elf) == NULL) { + LIBELF_SET_ERROR(ARGUMENT, 0); + return (NULL); + } + + ec = e->e_class; + assert(ec == ELFCLASS32 || ec == ELFCLASS64); + + if (ec == ELFCLASS32) + sh_type = scn->s_shdr.s_shdr32.sh_type; + else + sh_type = scn->s_shdr.s_shdr64.sh_type; + + if (_libelf_xlate_shtype(sh_type) != ELF_T_DYN) { + LIBELF_SET_ERROR(ARGUMENT, 0); + return (NULL); + } + + msz = _libelf_msize(ELF_T_DYN, ec, e->e_version); + + assert(msz > 0); + + if (msz * ndx >= d->d_size) { + LIBELF_SET_ERROR(ARGUMENT, 0); + return (NULL); + } + + if (ec == ELFCLASS32) { + dyn32 = (Elf32_Dyn *) d->d_buf + ndx; + + dst->d_tag = dyn32->d_tag; + dst->d_un.d_val = (Elf64_Xword) dyn32->d_un.d_val; + + } else { + + dyn64 = (Elf64_Dyn *) d->d_buf + ndx; + + *dst = *dyn64; + } + + return (dst); +} + +int +gelf_update_dyn(Elf_Data *d, int ndx, GElf_Dyn *ds) +{ + int ec; + Elf *e; + Elf_Scn *scn; + Elf32_Dyn *dyn32; + Elf64_Dyn *dyn64; + size_t msz; + uint32_t sh_type; + + if (d == NULL || ndx < 0 || ds == NULL || + (scn = d->d_scn) == NULL || + (e = scn->s_elf) == NULL) { + LIBELF_SET_ERROR(ARGUMENT, 0); + return (0); + } + + ec = e->e_class; + assert(ec == ELFCLASS32 || ec == ELFCLASS64); + + if (ec == ELFCLASS32) + sh_type = scn->s_shdr.s_shdr32.sh_type; + else + sh_type = scn->s_shdr.s_shdr64.sh_type; + + if (_libelf_xlate_shtype(sh_type) != ELF_T_DYN) { + LIBELF_SET_ERROR(ARGUMENT, 0); + return (0); + } + + msz = _libelf_msize(ELF_T_DYN, ec, e->e_version); + assert(msz > 0); + + if (msz * ndx >= d->d_size) { + LIBELF_SET_ERROR(ARGUMENT, 0); + return (0); + } + + if (ec == ELFCLASS32) { + dyn32 = (Elf32_Dyn *) d->d_buf + ndx; + + LIBELF_COPY_S32(dyn32, ds, d_tag); + LIBELF_COPY_U32(dyn32, ds, d_un.d_val); + } else { + dyn64 = (Elf64_Dyn *) d->d_buf + ndx; + + *dyn64 = *ds; + } + + return (1); +} diff --git a/rocclr/compiler/lib/loaders/elf/utils/libelf/gelf_ehdr.c b/rocclr/compiler/lib/loaders/elf/utils/libelf/gelf_ehdr.c new file mode 100644 index 0000000000..37ccce8c6a --- /dev/null +++ b/rocclr/compiler/lib/loaders/elf/utils/libelf/gelf_ehdr.c @@ -0,0 +1,167 @@ +/*- + * Copyright (c) 2006,2008 Joseph Koshy + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include + +#include +#include +#include +#include + +#include "_libelf.h" + +LIBELF_VCSID("$Id: gelf_ehdr.c 1678 2011-07-28 04:36:34Z jkoshy $"); + +Elf32_Ehdr * +elf32_getehdr(Elf *e) +{ + return (_libelf_ehdr(e, ELFCLASS32, 0)); +} + +Elf64_Ehdr * +elf64_getehdr(Elf *e) +{ + return (_libelf_ehdr(e, ELFCLASS64, 0)); +} + +GElf_Ehdr * +gelf_getehdr(Elf *e, GElf_Ehdr *d) +{ + int ec; + Elf32_Ehdr *eh32; + Elf64_Ehdr *eh64; + + if (d == NULL || e == NULL || + ((ec = e->e_class) != ELFCLASS32 && ec != ELFCLASS64)) { + LIBELF_SET_ERROR(ARGUMENT, 0); + return (NULL); + } + + if (ec == ELFCLASS32) { + if ((eh32 = _libelf_ehdr(e, ELFCLASS32, 0)) == NULL) + return (NULL); + + (void) memcpy(d->e_ident, eh32->e_ident, + sizeof(eh32->e_ident)); + d->e_type = eh32->e_type; + d->e_machine = eh32->e_machine; + d->e_version = eh32->e_version; + d->e_entry = eh32->e_entry; + d->e_phoff = eh32->e_phoff; + d->e_shoff = eh32->e_shoff; + d->e_flags = eh32->e_flags; + d->e_ehsize = eh32->e_ehsize; + d->e_phentsize = eh32->e_phentsize; + d->e_phnum = eh32->e_phnum; + d->e_shentsize = eh32->e_shentsize; + d->e_shnum = eh32->e_shnum; + d->e_shstrndx = eh32->e_shstrndx; + + return (d); + } + + assert(ec == ELFCLASS64); + + if ((eh64 = _libelf_ehdr(e, ELFCLASS64, 0)) == NULL) + return (NULL); + *d = *eh64; + + return (d); +} + +Elf32_Ehdr * +elf32_newehdr(Elf *e) +{ + return (_libelf_ehdr(e, ELFCLASS32, 1)); +} + +Elf64_Ehdr * +elf64_newehdr(Elf *e) +{ + return (_libelf_ehdr(e, ELFCLASS64, 1)); +} + +void * +gelf_newehdr(Elf *e, int ec) +{ + if (e != NULL && + (ec == ELFCLASS32 || ec == ELFCLASS64)) + return (_libelf_ehdr(e, ec, 1)); + + LIBELF_SET_ERROR(ARGUMENT, 0); + return (NULL); +} + +int +gelf_update_ehdr(Elf *e, GElf_Ehdr *s) +{ + int ec; + void *ehdr; + Elf32_Ehdr *eh32; + Elf64_Ehdr *eh64; + + if (s== NULL || e == NULL || e->e_kind != ELF_K_ELF || + ((ec = e->e_class) != ELFCLASS32 && ec != ELFCLASS64)) { + LIBELF_SET_ERROR(ARGUMENT, 0); + return (0); + } + + if (e->e_cmd == ELF_C_READ) { + LIBELF_SET_ERROR(MODE, 0); + return (0); + } + + if ((ehdr = _libelf_ehdr(e, ec, 0)) == NULL) + return (0); + + (void) elf_flagehdr(e, ELF_C_SET, ELF_F_DIRTY); + + if (ec == ELFCLASS64) { + eh64 = (Elf64_Ehdr *) ehdr; + *eh64 = *s; + return (1); + } + + eh32 = (Elf32_Ehdr *) ehdr; + + (void) memcpy(eh32->e_ident, s->e_ident, sizeof(eh32->e_ident)); + + eh32->e_type = s->e_type; + eh32->e_machine = s->e_machine; + eh32->e_version = s->e_version; + LIBELF_COPY_U32(eh32, s, e_entry); + LIBELF_COPY_U32(eh32, s, e_phoff); + LIBELF_COPY_U32(eh32, s, e_shoff); + eh32->e_flags = s->e_flags; + eh32->e_ehsize = s->e_ehsize; + eh32->e_phentsize = s->e_phentsize; + eh32->e_phnum = s->e_phnum; + eh32->e_shentsize = s->e_shentsize; + eh32->e_shnum = s->e_shnum; + eh32->e_shstrndx = s->e_shstrndx; + + return (1); +} diff --git a/rocclr/compiler/lib/loaders/elf/utils/libelf/gelf_fsize.c b/rocclr/compiler/lib/loaders/elf/utils/libelf/gelf_fsize.c new file mode 100644 index 0000000000..0e38d1462e --- /dev/null +++ b/rocclr/compiler/lib/loaders/elf/utils/libelf/gelf_fsize.c @@ -0,0 +1,62 @@ +/*- + * Copyright (c) 2006,2008 Joseph Koshy + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include + +#include +#include + +#include "_libelf.h" + +LIBELF_VCSID("$Id: gelf_fsize.c 189 2008-07-20 10:38:08Z jkoshy $"); + +size_t +elf32_fsize(Elf_Type t, size_t c, unsigned int v) +{ + return (_libelf_fsize(t, ELFCLASS32, v, c)); +} + +size_t +elf64_fsize(Elf_Type t, size_t c, unsigned int v) +{ + return (_libelf_fsize(t, ELFCLASS64, v, c)); +} + +size_t +gelf_fsize(Elf *e, Elf_Type t, size_t c, unsigned int v) +{ + + if (e == NULL) { + LIBELF_SET_ERROR(ARGUMENT, 0); + return (0); + } + + if (e->e_class == ELFCLASS32 || e->e_class == ELFCLASS64) + return (_libelf_fsize(t, e->e_class, v, c)); + + LIBELF_SET_ERROR(ARGUMENT, 0); + return (0); +} diff --git a/rocclr/compiler/lib/loaders/elf/utils/libelf/gelf_getclass.c b/rocclr/compiler/lib/loaders/elf/utils/libelf/gelf_getclass.c new file mode 100644 index 0000000000..349a9cd535 --- /dev/null +++ b/rocclr/compiler/lib/loaders/elf/utils/libelf/gelf_getclass.c @@ -0,0 +1,39 @@ +/*- + * Copyright (c) 2006,2008 Joseph Koshy + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include + +#include + +#include "_libelf.h" + +LIBELF_VCSID("$Id: gelf_getclass.c 189 2008-07-20 10:38:08Z jkoshy $"); + +int +gelf_getclass(Elf *e) +{ + return (e != NULL ? e->e_class : ELFCLASSNONE); +} diff --git a/rocclr/compiler/lib/loaders/elf/utils/libelf/gelf_move.c b/rocclr/compiler/lib/loaders/elf/utils/libelf/gelf_move.c new file mode 100644 index 0000000000..359b021ddc --- /dev/null +++ b/rocclr/compiler/lib/loaders/elf/utils/libelf/gelf_move.c @@ -0,0 +1,154 @@ +/*- + * Copyright (c) 2006,2008 Joseph Koshy + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include + +#include +#include + +#include "_libelf.h" + +LIBELF_VCSID("$Id: gelf_move.c 1166 2010-09-04 00:54:36Z jkoshy $"); + +#if LIBELF_CONFIG_MOVE + +GElf_Move * +gelf_getmove(Elf_Data *d, int ndx, GElf_Move *dst) +{ + int ec; + Elf *e; + Elf_Scn *scn; + Elf32_Move *move32; + Elf64_Move *move64; + size_t msz; + uint32_t sh_type; + + if (d == NULL || ndx < 0 || dst == NULL || + (scn = d->d_scn) == NULL || + (e = scn->s_elf) == NULL) { + LIBELF_SET_ERROR(ARGUMENT, 0); + return (NULL); + } + + ec = e->e_class; + assert(ec == ELFCLASS32 || ec == ELFCLASS64); + + if (ec == ELFCLASS32) + sh_type = scn->s_shdr.s_shdr32.sh_type; + else + sh_type = scn->s_shdr.s_shdr64.sh_type; + + if (_libelf_xlate_shtype(sh_type) != ELF_T_MOVE) { + LIBELF_SET_ERROR(ARGUMENT, 0); + return (NULL); + } + + msz = _libelf_msize(ELF_T_MOVE, ec, e->e_version); + + assert(msz > 0); + + if (msz * ndx >= d->d_size) { + LIBELF_SET_ERROR(ARGUMENT, 0); + return (NULL); + } + + if (ec == ELFCLASS32) { + + move32 = (Elf32_Move *) d->d_buf + ndx; + + dst->m_value = move32->m_value; + dst->m_info = (Elf64_Xword) move32->m_info; + dst->m_poffset = (Elf64_Xword) move32->m_poffset; + dst->m_repeat = move32->m_repeat; + dst->m_stride = move32->m_stride; + } else { + + move64 = (Elf64_Move *) d->d_buf + ndx; + + *dst = *move64; + } + + return (dst); +} + +int +gelf_update_move(Elf_Data *d, int ndx, GElf_Move *gm) +{ + int ec; + Elf *e; + Elf_Scn *scn; + Elf32_Move *move32; + Elf64_Move *move64; + size_t msz; + uint32_t sh_type; + + if (d == NULL || ndx < 0 || gm == NULL || + (scn = d->d_scn) == NULL || + (e = scn->s_elf) == NULL) { + LIBELF_SET_ERROR(ARGUMENT, 0); + return (0); + } + + ec = e->e_class; + assert(ec == ELFCLASS32 || ec == ELFCLASS64); + + if (ec == ELFCLASS32) + sh_type = scn->s_shdr.s_shdr32.sh_type; + else + sh_type = scn->s_shdr.s_shdr64.sh_type; + + if (_libelf_xlate_shtype(sh_type) != ELF_T_MOVE) { + LIBELF_SET_ERROR(ARGUMENT, 0); + return (0); + } + + msz = _libelf_msize(ELF_T_MOVE, ec, e->e_version); + assert(msz > 0); + + if (msz * ndx >= d->d_size) { + LIBELF_SET_ERROR(ARGUMENT, 0); + return (0); + } + + if (ec == ELFCLASS32) { + move32 = (Elf32_Move *) d->d_buf + ndx; + + move32->m_value = gm->m_value; + LIBELF_COPY_U32(move32, gm, m_info); + LIBELF_COPY_U32(move32, gm, m_poffset); + move32->m_repeat = gm->m_repeat; + move32->m_stride = gm->m_stride; + + } else { + move64 = (Elf64_Move *) d->d_buf + ndx; + + *move64 = *gm; + } + + return (1); +} + +#endif /* LIBELF_CONFIG_MOVE */ diff --git a/rocclr/compiler/lib/loaders/elf/utils/libelf/gelf_phdr.c b/rocclr/compiler/lib/loaders/elf/utils/libelf/gelf_phdr.c new file mode 100644 index 0000000000..47000d84ec --- /dev/null +++ b/rocclr/compiler/lib/loaders/elf/utils/libelf/gelf_phdr.c @@ -0,0 +1,177 @@ +/*- + * Copyright (c) 2006,2008 Joseph Koshy + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include + +#include +#include + +#include "_libelf.h" + +LIBELF_VCSID("$Id: gelf_phdr.c 189 2008-07-20 10:38:08Z jkoshy $"); + +Elf32_Phdr * +elf32_getphdr(Elf *e) +{ + return (_libelf_getphdr(e, ELFCLASS32)); +} + +Elf64_Phdr * +elf64_getphdr(Elf *e) +{ + return (_libelf_getphdr(e, ELFCLASS64)); +} + +GElf_Phdr * +gelf_getphdr(Elf *e, int index, GElf_Phdr *d) +{ + int ec; + Elf32_Ehdr *eh32; + Elf64_Ehdr *eh64; + Elf32_Phdr *ep32; + Elf64_Phdr *ep64; + + if (d == NULL || e == NULL || + ((ec = e->e_class) != ELFCLASS32 && ec != ELFCLASS64) || + (e->e_kind != ELF_K_ELF) || index < 0) { + LIBELF_SET_ERROR(ARGUMENT, 0); + return (NULL); + } + + if (ec == ELFCLASS32) { + if ((eh32 = _libelf_ehdr(e, ELFCLASS32, 0)) == NULL || + ((ep32 = _libelf_getphdr(e, ELFCLASS32)) == NULL)) + return (NULL); + + if (index >= eh32->e_phnum) { + LIBELF_SET_ERROR(ARGUMENT, 0); + return (NULL); + } + + ep32 += index; + + d->p_type = ep32->p_type; + d->p_offset = ep32->p_offset; + d->p_vaddr = (Elf64_Addr) ep32->p_vaddr; + d->p_paddr = (Elf64_Addr) ep32->p_paddr; + d->p_filesz = (Elf64_Xword) ep32->p_filesz; + d->p_memsz = (Elf64_Xword) ep32->p_memsz; + d->p_flags = ep32->p_flags; + d->p_align = (Elf64_Xword) ep32->p_align; + + } else { + if ((eh64 = _libelf_ehdr(e, ELFCLASS64, 0)) == NULL || + (ep64 = _libelf_getphdr(e, ELFCLASS64)) == NULL) + return (NULL); + + if (index >= eh64->e_phnum) { + LIBELF_SET_ERROR(ARGUMENT, 0); + return (NULL); + } + + ep64 += index; + + *d = *ep64; + } + + return (d); +} + +Elf32_Phdr * +elf32_newphdr(Elf *e, size_t count) +{ + return (_libelf_newphdr(e, ELFCLASS32, count)); +} + +Elf64_Phdr * +elf64_newphdr(Elf *e, size_t count) +{ + return (_libelf_newphdr(e, ELFCLASS64, count)); +} + +void * +gelf_newphdr(Elf *e, size_t count) +{ + if (e == NULL) { + LIBELF_SET_ERROR(ARGUMENT, 0); + return (NULL); + } + return (_libelf_newphdr(e, e->e_class, count)); +} + +int +gelf_update_phdr(Elf *e, int ndx, GElf_Phdr *s) +{ + int ec, phnum; + void *ehdr; + Elf32_Phdr *ph32; + Elf64_Phdr *ph64; + + if (s == NULL || e == NULL || e->e_kind != ELF_K_ELF || + ((ec = e->e_class) != ELFCLASS32 && ec != ELFCLASS64)) { + LIBELF_SET_ERROR(ARGUMENT, 0); + return (0); + } + + if (e->e_cmd == ELF_C_READ) { + LIBELF_SET_ERROR(MODE, 0); + return (0); + } + + if ((ehdr = _libelf_ehdr(e, ec, 0)) == NULL) + return (0); + + if (ec == ELFCLASS32) + phnum = ((Elf32_Ehdr *) ehdr)->e_phnum; + else + phnum = ((Elf64_Ehdr *) ehdr)->e_phnum; + + if (ndx < 0 || ndx > phnum) { + LIBELF_SET_ERROR(ARGUMENT, 0); + return (0); + } + + (void) elf_flagphdr(e, ELF_C_SET, ELF_F_DIRTY); + + if (ec == ELFCLASS64) { + ph64 = e->e_u.e_elf.e_phdr.e_phdr64 + ndx; + *ph64 = *s; + return (1); + } + + ph32 = e->e_u.e_elf.e_phdr.e_phdr32 + ndx; + + ph32->p_type = s->p_type; + ph32->p_flags = s->p_flags; + LIBELF_COPY_U32(ph32, s, p_offset); + LIBELF_COPY_U32(ph32, s, p_vaddr); + LIBELF_COPY_U32(ph32, s, p_paddr); + LIBELF_COPY_U32(ph32, s, p_filesz); + LIBELF_COPY_U32(ph32, s, p_memsz); + LIBELF_COPY_U32(ph32, s, p_align); + + return (1); +} diff --git a/rocclr/compiler/lib/loaders/elf/utils/libelf/gelf_rel.c b/rocclr/compiler/lib/loaders/elf/utils/libelf/gelf_rel.c new file mode 100644 index 0000000000..7d0b6af7fe --- /dev/null +++ b/rocclr/compiler/lib/loaders/elf/utils/libelf/gelf_rel.c @@ -0,0 +1,152 @@ +/*- + * Copyright (c) 2006,2008 Joseph Koshy + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include + +#include +#include + +#include "_libelf.h" + +LIBELF_VCSID("$Id: gelf_rel.c 189 2008-07-20 10:38:08Z jkoshy $"); + +GElf_Rel * +gelf_getrel(Elf_Data *d, int ndx, GElf_Rel *dst) +{ + int ec; + Elf *e; + Elf_Scn *scn; + Elf32_Rel *rel32; + Elf64_Rel *rel64; + size_t msz; + uint32_t sh_type; + + if (d == NULL || ndx < 0 || dst == NULL || + (scn = d->d_scn) == NULL || + (e = scn->s_elf) == NULL) { + LIBELF_SET_ERROR(ARGUMENT, 0); + return (NULL); + } + + ec = e->e_class; + assert(ec == ELFCLASS32 || ec == ELFCLASS64); + + if (ec == ELFCLASS32) + sh_type = scn->s_shdr.s_shdr32.sh_type; + else + sh_type = scn->s_shdr.s_shdr64.sh_type; + + if (_libelf_xlate_shtype(sh_type) != ELF_T_REL) { + LIBELF_SET_ERROR(ARGUMENT, 0); + return (NULL); + } + + msz = _libelf_msize(ELF_T_REL, ec, e->e_version); + + assert(msz > 0); + + if (msz * ndx >= d->d_size) { + LIBELF_SET_ERROR(ARGUMENT, 0); + return (NULL); + } + + if (ec == ELFCLASS32) { + rel32 = (Elf32_Rel *) d->d_buf + ndx; + + dst->r_offset = (Elf64_Addr) rel32->r_offset; + dst->r_info = ELF64_R_INFO( + (Elf64_Xword) ELF32_R_SYM(rel32->r_info), + ELF32_R_TYPE(rel32->r_info)); + + } else { + + rel64 = (Elf64_Rel *) d->d_buf + ndx; + + *dst = *rel64; + } + + return (dst); +} + +int +gelf_update_rel(Elf_Data *d, int ndx, GElf_Rel *dr) +{ + int ec; + Elf *e; + Elf_Scn *scn; + Elf32_Rel *rel32; + Elf64_Rel *rel64; + size_t msz; + uint32_t sh_type; + + if (d == NULL || ndx < 0 || dr == NULL || + (scn = d->d_scn) == NULL || + (e = scn->s_elf) == NULL) { + LIBELF_SET_ERROR(ARGUMENT, 0); + return (0); + } + + ec = e->e_class; + assert(ec == ELFCLASS32 || ec == ELFCLASS64); + + if (ec == ELFCLASS32) + sh_type = scn->s_shdr.s_shdr32.sh_type; + else + sh_type = scn->s_shdr.s_shdr64.sh_type; + + if (_libelf_xlate_shtype(sh_type) != ELF_T_REL) { + LIBELF_SET_ERROR(ARGUMENT, 0); + return (0); + } + + msz = _libelf_msize(ELF_T_REL, ec, e->e_version); + assert(msz > 0); + + if (msz * ndx >= d->d_size) { + LIBELF_SET_ERROR(ARGUMENT, 0); + return (0); + } + + if (ec == ELFCLASS32) { + rel32 = (Elf32_Rel *) d->d_buf + ndx; + + LIBELF_COPY_U32(rel32, dr, r_offset); + + if (ELF64_R_SYM(dr->r_info) > ELF32_R_SYM(~0UL) || + ELF64_R_TYPE(dr->r_info) > ELF32_R_TYPE(~0U)) { + LIBELF_SET_ERROR(RANGE, 0); + return (0); + } + rel32->r_info = ELF32_R_INFO(ELF64_R_SYM(dr->r_info), + ELF64_R_TYPE(dr->r_info)); + } else { + rel64 = (Elf64_Rel *) d->d_buf + ndx; + + *rel64 = *dr; + } + + return (1); +} diff --git a/rocclr/compiler/lib/loaders/elf/utils/libelf/gelf_rela.c b/rocclr/compiler/lib/loaders/elf/utils/libelf/gelf_rela.c new file mode 100644 index 0000000000..722c1add83 --- /dev/null +++ b/rocclr/compiler/lib/loaders/elf/utils/libelf/gelf_rela.c @@ -0,0 +1,155 @@ +/*- + * Copyright (c) 2006,2008 Joseph Koshy + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include + +#include +#include + +#include "_libelf.h" + +LIBELF_VCSID("$Id: gelf_rela.c 189 2008-07-20 10:38:08Z jkoshy $"); + +GElf_Rela * +gelf_getrela(Elf_Data *d, int ndx, GElf_Rela *dst) +{ + int ec; + Elf *e; + Elf_Scn *scn; + Elf32_Rela *rela32; + Elf64_Rela *rela64; + size_t msz; + uint32_t sh_type; + + if (d == NULL || ndx < 0 || dst == NULL || + (scn = d->d_scn) == NULL || + (e = scn->s_elf) == NULL) { + LIBELF_SET_ERROR(ARGUMENT, 0); + return (NULL); + } + + ec = e->e_class; + assert(ec == ELFCLASS32 || ec == ELFCLASS64); + + if (ec == ELFCLASS32) + sh_type = scn->s_shdr.s_shdr32.sh_type; + else + sh_type = scn->s_shdr.s_shdr64.sh_type; + + if (_libelf_xlate_shtype(sh_type) != ELF_T_RELA) { + LIBELF_SET_ERROR(ARGUMENT, 0); + return (NULL); + } + + msz = _libelf_msize(ELF_T_RELA, ec, e->e_version); + + assert(msz > 0); + + if (msz * ndx >= d->d_size) { + LIBELF_SET_ERROR(ARGUMENT, 0); + return (NULL); + } + + if (ec == ELFCLASS32) { + rela32 = (Elf32_Rela *) d->d_buf + ndx; + + dst->r_offset = (Elf64_Addr) rela32->r_offset; + dst->r_info = ELF64_R_INFO( + (Elf64_Xword) ELF32_R_SYM(rela32->r_info), + ELF32_R_TYPE(rela32->r_info)); + dst->r_addend = (Elf64_Sxword) rela32->r_addend; + + } else { + + rela64 = (Elf64_Rela *) d->d_buf + ndx; + + *dst = *rela64; + } + + return (dst); +} + +int +gelf_update_rela(Elf_Data *d, int ndx, GElf_Rela *dr) +{ + int ec; + Elf *e; + Elf_Scn *scn; + Elf32_Rela *rela32; + Elf64_Rela *rela64; + size_t msz; + uint32_t sh_type; + + if (d == NULL || ndx < 0 || dr == NULL || + (scn = d->d_scn) == NULL || + (e = scn->s_elf) == NULL) { + LIBELF_SET_ERROR(ARGUMENT, 0); + return (0); + } + + ec = e->e_class; + assert(ec == ELFCLASS32 || ec == ELFCLASS64); + + if (ec == ELFCLASS32) + sh_type = scn->s_shdr.s_shdr32.sh_type; + else + sh_type = scn->s_shdr.s_shdr64.sh_type; + + if (_libelf_xlate_shtype(sh_type) != ELF_T_RELA) { + LIBELF_SET_ERROR(ARGUMENT, 0); + return (0); + } + + msz = _libelf_msize(ELF_T_RELA, ec, e->e_version); + assert(msz > 0); + + if (msz * ndx >= d->d_size) { + LIBELF_SET_ERROR(ARGUMENT, 0); + return (0); + } + + if (ec == ELFCLASS32) { + rela32 = (Elf32_Rela *) d->d_buf + ndx; + + LIBELF_COPY_U32(rela32, dr, r_offset); + + if (ELF64_R_SYM(dr->r_info) > ELF32_R_SYM(~0UL) || + ELF64_R_TYPE(dr->r_info) > ELF32_R_TYPE(~0U)) { + LIBELF_SET_ERROR(RANGE, 0); + return (0); + } + rela32->r_info = ELF32_R_INFO(ELF64_R_SYM(dr->r_info), + ELF64_R_TYPE(dr->r_info)); + + LIBELF_COPY_S32(rela32, dr, r_addend); + } else { + rela64 = (Elf64_Rela *) d->d_buf + ndx; + + *rela64 = *dr; + } + + return (1); +} diff --git a/rocclr/compiler/lib/loaders/elf/utils/libelf/gelf_shdr.c b/rocclr/compiler/lib/loaders/elf/utils/libelf/gelf_shdr.c new file mode 100644 index 0000000000..47e56e9d51 --- /dev/null +++ b/rocclr/compiler/lib/loaders/elf/utils/libelf/gelf_shdr.c @@ -0,0 +1,130 @@ +/*- + * Copyright (c) 2006,2008 Joseph Koshy + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include + +#include +#include +#include + +#include "_libelf.h" + +LIBELF_VCSID("$Id: gelf_shdr.c 189 2008-07-20 10:38:08Z jkoshy $"); + +Elf32_Shdr * +elf32_getshdr(Elf_Scn *s) +{ + return (_libelf_getshdr(s, ELFCLASS32)); +} + +Elf64_Shdr * +elf64_getshdr(Elf_Scn *s) +{ + return (_libelf_getshdr(s, ELFCLASS64)); +} + +GElf_Shdr * +gelf_getshdr(Elf_Scn *s, GElf_Shdr *d) +{ + int ec; + void *sh; + Elf32_Shdr *sh32; + Elf64_Shdr *sh64; + + if (d == NULL) { + LIBELF_SET_ERROR(ARGUMENT, 0); + return (NULL); + } + + if ((sh = _libelf_getshdr(s, ELFCLASSNONE)) == NULL) + return (NULL); + + ec = s->s_elf->e_class; + assert(ec == ELFCLASS32 || ec == ELFCLASS64); + + if (ec == ELFCLASS32) { + sh32 = (Elf32_Shdr *) sh; + + d->sh_name = sh32->sh_name; + d->sh_type = sh32->sh_type; + d->sh_flags = (Elf64_Xword) sh32->sh_flags; + d->sh_addr = (Elf64_Addr) sh32->sh_addr; + d->sh_offset = (Elf64_Off) sh32->sh_offset; + d->sh_size = (Elf64_Xword) sh32->sh_size; + d->sh_link = sh32->sh_link; + d->sh_info = sh32->sh_info; + d->sh_addralign = (Elf64_Xword) sh32->sh_addralign; + d->sh_entsize = (Elf64_Xword) sh32->sh_entsize; + } else { + sh64 = (Elf64_Shdr *) sh; + *d = *sh64; + } + + return (d); +} + +int +gelf_update_shdr(Elf_Scn *scn, GElf_Shdr *s) +{ + int ec; + Elf *e; + Elf32_Shdr *sh32; + + + if (s == NULL || scn == NULL || (e = scn->s_elf) == NULL || + e->e_kind != ELF_K_ELF || + ((ec = e->e_class) != ELFCLASS32 && ec != ELFCLASS64)) { + LIBELF_SET_ERROR(ARGUMENT, 0); + return (0); + } + + if (e->e_cmd == ELF_C_READ) { + LIBELF_SET_ERROR(MODE, 0); + return (0); + } + + (void) elf_flagscn(scn, ELF_C_SET, ELF_F_DIRTY); + + if (ec == ELFCLASS64) { + scn->s_shdr.s_shdr64 = *s; + return (1); + } + + sh32 = &scn->s_shdr.s_shdr32; + + sh32->sh_name = s->sh_name; + sh32->sh_type = s->sh_type; + LIBELF_COPY_U32(sh32, s, sh_flags); + LIBELF_COPY_U32(sh32, s, sh_addr); + LIBELF_COPY_U32(sh32, s, sh_offset); + LIBELF_COPY_U32(sh32, s, sh_size); + sh32->sh_link = s->sh_link; + sh32->sh_info = s->sh_info; + LIBELF_COPY_U32(sh32, s, sh_addralign); + LIBELF_COPY_U32(sh32, s, sh_entsize); + + return (1); +} diff --git a/rocclr/compiler/lib/loaders/elf/utils/libelf/gelf_sym.c b/rocclr/compiler/lib/loaders/elf/utils/libelf/gelf_sym.c new file mode 100644 index 0000000000..3f84a178eb --- /dev/null +++ b/rocclr/compiler/lib/loaders/elf/utils/libelf/gelf_sym.c @@ -0,0 +1,153 @@ +/*- + * Copyright (c) 2006,2008 Joseph Koshy + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include + +#include +#include + +#include "_libelf.h" + +LIBELF_VCSID("$Id: gelf_sym.c 189 2008-07-20 10:38:08Z jkoshy $"); + +GElf_Sym * +gelf_getsym(Elf_Data *d, int ndx, GElf_Sym *dst) +{ + int ec; + Elf *e; + Elf_Scn *scn; + Elf32_Sym *sym32; + Elf64_Sym *sym64; + size_t msz; + uint32_t sh_type; + + if (d == NULL || ndx < 0 || dst == NULL || + (scn = d->d_scn) == NULL || + (e = scn->s_elf) == NULL) { + LIBELF_SET_ERROR(ARGUMENT, 0); + return (NULL); + } + + ec = e->e_class; + assert(ec == ELFCLASS32 || ec == ELFCLASS64); + + if (ec == ELFCLASS32) + sh_type = scn->s_shdr.s_shdr32.sh_type; + else + sh_type = scn->s_shdr.s_shdr64.sh_type; + + if (_libelf_xlate_shtype(sh_type) != ELF_T_SYM) { + LIBELF_SET_ERROR(ARGUMENT, 0); + return (NULL); + } + + msz = _libelf_msize(ELF_T_SYM, ec, e->e_version); + + assert(msz > 0); + + if (msz * ndx >= d->d_size) { + LIBELF_SET_ERROR(ARGUMENT, 0); + return (NULL); + } + + if (ec == ELFCLASS32) { + + sym32 = (Elf32_Sym *) d->d_buf + ndx; + + dst->st_name = sym32->st_name; + dst->st_value = (Elf64_Addr) sym32->st_value; + dst->st_size = (Elf64_Xword) sym32->st_size; + dst->st_info = ELF64_ST_INFO(ELF32_ST_BIND(sym32->st_info), + ELF32_ST_TYPE(sym32->st_info)); + dst->st_other = sym32->st_other; + dst->st_shndx = sym32->st_shndx; + } else { + + sym64 = (Elf64_Sym *) d->d_buf + ndx; + + *dst = *sym64; + } + + return (dst); +} + +int +gelf_update_sym(Elf_Data *d, int ndx, GElf_Sym *gs) +{ + int ec; + Elf *e; + Elf_Scn *scn; + Elf32_Sym *sym32; + Elf64_Sym *sym64; + size_t msz; + uint32_t sh_type; + + if (d == NULL || ndx < 0 || gs == NULL || + (scn = d->d_scn) == NULL || + (e = scn->s_elf) == NULL) { + LIBELF_SET_ERROR(ARGUMENT, 0); + return (0); + } + + ec = e->e_class; + assert(ec == ELFCLASS32 || ec == ELFCLASS64); + + if (ec == ELFCLASS32) + sh_type = scn->s_shdr.s_shdr32.sh_type; + else + sh_type = scn->s_shdr.s_shdr64.sh_type; + + if (_libelf_xlate_shtype(sh_type) != ELF_T_SYM) { + LIBELF_SET_ERROR(ARGUMENT, 0); + return (0); + } + + msz = _libelf_msize(ELF_T_SYM, ec, e->e_version); + assert(msz > 0); + + if (msz * ndx >= d->d_size) { + LIBELF_SET_ERROR(ARGUMENT, 0); + return (0); + } + + if (ec == ELFCLASS32) { + sym32 = (Elf32_Sym *) d->d_buf + ndx; + + sym32->st_name = gs->st_name; + sym32->st_info = gs->st_info; + sym32->st_other = gs->st_other; + sym32->st_shndx = gs->st_shndx; + + LIBELF_COPY_U32(sym32, gs, st_value); + LIBELF_COPY_U32(sym32, gs, st_size); + } else { + sym64 = (Elf64_Sym *) d->d_buf + ndx; + + *sym64 = *gs; + } + + return (1); +} diff --git a/rocclr/compiler/lib/loaders/elf/utils/libelf/gelf_syminfo.c b/rocclr/compiler/lib/loaders/elf/utils/libelf/gelf_syminfo.c new file mode 100644 index 0000000000..09b3744be8 --- /dev/null +++ b/rocclr/compiler/lib/loaders/elf/utils/libelf/gelf_syminfo.c @@ -0,0 +1,147 @@ +/*- + * Copyright (c) 2006,2008 Joseph Koshy + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include + +#include +#include + +#include "_libelf.h" + +LIBELF_VCSID("$Id: gelf_syminfo.c 1166 2010-09-04 00:54:36Z jkoshy $"); + +#if LIBELF_CONFIG_SYMINFO +GElf_Syminfo * +gelf_getsyminfo(Elf_Data *d, int ndx, GElf_Syminfo *dst) +{ + int ec; + Elf *e; + Elf_Scn *scn; + Elf32_Syminfo *syminfo32; + Elf64_Syminfo *syminfo64; + size_t msz; + uint32_t sh_type; + + if (d == NULL || ndx < 0 || dst == NULL || + (scn = d->d_scn) == NULL || + (e = scn->s_elf) == NULL) { + LIBELF_SET_ERROR(ARGUMENT, 0); + return (NULL); + } + + ec = e->e_class; + assert(ec == ELFCLASS32 || ec == ELFCLASS64); + + if (ec == ELFCLASS32) + sh_type = scn->s_shdr.s_shdr32.sh_type; + else + sh_type = scn->s_shdr.s_shdr64.sh_type; + + if (_libelf_xlate_shtype(sh_type) != ELF_T_SYMINFO) { + LIBELF_SET_ERROR(ARGUMENT, 0); + return (NULL); + } + + msz = _libelf_msize(ELF_T_SYMINFO, ec, e->e_version); + + assert(msz > 0); + + if (msz * ndx >= d->d_size) { + LIBELF_SET_ERROR(ARGUMENT, 0); + return (NULL); + } + + if (ec == ELFCLASS32) { + + syminfo32 = (Elf32_Syminfo *) d->d_buf + ndx; + + dst->si_boundto = syminfo32->si_boundto; + dst->si_flags = syminfo32->si_flags; + + } else { + + syminfo64 = (Elf64_Syminfo *) d->d_buf + ndx; + + *dst = *syminfo64; + } + + return (dst); +} + +int +gelf_update_syminfo(Elf_Data *d, int ndx, GElf_Syminfo *gs) +{ + int ec; + Elf *e; + Elf_Scn *scn; + Elf32_Syminfo *syminfo32; + Elf64_Syminfo *syminfo64; + size_t msz; + uint32_t sh_type; + + if (d == NULL || ndx < 0 || gs == NULL || + (scn = d->d_scn) == NULL || + (e = scn->s_elf) == NULL) { + LIBELF_SET_ERROR(ARGUMENT, 0); + return (0); + } + + ec = e->e_class; + assert(ec == ELFCLASS32 || ec == ELFCLASS64); + + if (ec == ELFCLASS32) + sh_type = scn->s_shdr.s_shdr32.sh_type; + else + sh_type = scn->s_shdr.s_shdr64.sh_type; + + if (_libelf_xlate_shtype(sh_type) != ELF_T_SYMINFO) { + LIBELF_SET_ERROR(ARGUMENT, 0); + return (0); + } + + msz = _libelf_msize(ELF_T_SYMINFO, ec, e->e_version); + assert(msz > 0); + + if (msz * ndx >= d->d_size) { + LIBELF_SET_ERROR(ARGUMENT, 0); + return (0); + } + + if (ec == ELFCLASS32) { + syminfo32 = (Elf32_Syminfo *) d->d_buf + ndx; + + syminfo32->si_boundto = gs->si_boundto; + syminfo32->si_flags = gs->si_flags; + + } else { + syminfo64 = (Elf64_Syminfo *) d->d_buf + ndx; + + *syminfo64 = *gs; + } + + return (1); +} +#endif /* LIBELF_CONFIG_SYMINFO */ diff --git a/rocclr/compiler/lib/loaders/elf/utils/libelf/gelf_symshndx.c b/rocclr/compiler/lib/loaders/elf/utils/libelf/gelf_symshndx.c new file mode 100644 index 0000000000..ab3549cdfd --- /dev/null +++ b/rocclr/compiler/lib/loaders/elf/utils/libelf/gelf_symshndx.c @@ -0,0 +1,128 @@ +/*- + * Copyright (c) 2006,2008 Joseph Koshy + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include + +#include +#include + +#include "_libelf.h" + +LIBELF_VCSID("$Id: gelf_symshndx.c 189 2008-07-20 10:38:08Z jkoshy $"); + +GElf_Sym * +gelf_getsymshndx(Elf_Data *d, Elf_Data *id, int ndx, GElf_Sym *dst, + Elf32_Word *shindex) +{ + int ec; + Elf *e; + Elf_Scn *scn; + size_t msz; + uint32_t sh_type; + + if (gelf_getsym(d, ndx, dst) == 0) + return (NULL); + + if (id == NULL || (scn = id->d_scn) == NULL || + (e = scn->s_elf) == NULL || (e != d->d_scn->s_elf) || + shindex == NULL) { + LIBELF_SET_ERROR(ARGUMENT, 0); + return (NULL); + } + + ec = e->e_class; + assert(ec == ELFCLASS32 || ec == ELFCLASS64); + + if (ec == ELFCLASS32) + sh_type = scn->s_shdr.s_shdr32.sh_type; + else + sh_type = scn->s_shdr.s_shdr64.sh_type; + + if (_libelf_xlate_shtype(sh_type) != ELF_T_WORD || + id->d_type != ELF_T_WORD) { + LIBELF_SET_ERROR(ARGUMENT, 0); + return (NULL); + } + + msz = _libelf_msize(ELF_T_WORD, ec, e->e_version); + + assert(msz > 0); + + if (msz * ndx >= id->d_size) { + LIBELF_SET_ERROR(ARGUMENT, 0); + return (NULL); + } + + *shindex = ((Elf32_Word *) id->d_buf)[ndx]; + + return (dst); +} + +int +gelf_update_symshndx(Elf_Data *d, Elf_Data *id, int ndx, GElf_Sym *gs, + Elf32_Word xindex) +{ + int ec; + Elf *e; + Elf_Scn *scn; + size_t msz; + uint32_t sh_type; + + if (gelf_update_sym(d, ndx, gs) == 0) + return (0); + + if (id == NULL || (scn = id->d_scn) == NULL || + (e = scn->s_elf) == NULL || (e != d->d_scn->s_elf)) { + LIBELF_SET_ERROR(ARGUMENT, 0); + return (0); + } + + ec = e->e_class; + assert(ec == ELFCLASS32 || ec == ELFCLASS64); + + if (ec == ELFCLASS32) + sh_type = scn->s_shdr.s_shdr32.sh_type; + else + sh_type = scn->s_shdr.s_shdr64.sh_type; + + if (_libelf_xlate_shtype(sh_type) != ELF_T_WORD || + d->d_type != ELF_T_WORD) { + LIBELF_SET_ERROR(ARGUMENT, 0); + return (0); + } + + msz = _libelf_msize(ELF_T_WORD, ec, e->e_version); + assert(msz > 0); + + if (msz * ndx >= id->d_size) { + LIBELF_SET_ERROR(ARGUMENT, 0); + return (0); + } + + *(((Elf32_Word *) id->d_buf) + ndx) = xindex; + + return (1); +} diff --git a/rocclr/compiler/lib/loaders/elf/utils/libelf/gelf_xlate.c b/rocclr/compiler/lib/loaders/elf/utils/libelf/gelf_xlate.c new file mode 100644 index 0000000000..6cdf70536a --- /dev/null +++ b/rocclr/compiler/lib/loaders/elf/utils/libelf/gelf_xlate.c @@ -0,0 +1,81 @@ +/*- + * Copyright (c) 2006,2008 Joseph Koshy + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include + +#include +#include +#include + +#include "_libelf.h" + +LIBELF_VCSID("$Id: gelf_xlate.c 1678 2011-07-28 04:36:34Z jkoshy $"); + +Elf_Data * +elf32_xlatetof(Elf_Data *dst, const Elf_Data *src, unsigned int encoding) +{ + return _libelf_xlate(dst, src, encoding, ELFCLASS32, ELF_TOFILE); +} + +Elf_Data * +elf64_xlatetof(Elf_Data *dst, const Elf_Data *src, unsigned int encoding) +{ + return _libelf_xlate(dst, src, encoding, ELFCLASS64, ELF_TOFILE); +} + +Elf_Data * +elf32_xlatetom(Elf_Data *dst, const Elf_Data *src, unsigned int encoding) +{ + return _libelf_xlate(dst, src, encoding, ELFCLASS32, ELF_TOMEMORY); +} + +Elf_Data * +elf64_xlatetom(Elf_Data *dst, const Elf_Data *src, unsigned int encoding) +{ + return _libelf_xlate(dst, src, encoding, ELFCLASS64, ELF_TOMEMORY); +} + +Elf_Data * +gelf_xlatetom(Elf *e, Elf_Data *dst, const Elf_Data *src, + unsigned int encoding) +{ + if (e != NULL) + return (_libelf_xlate(dst, src, encoding, e->e_class, + ELF_TOMEMORY)); + LIBELF_SET_ERROR(ARGUMENT, 0); + return (NULL); +} + +Elf_Data * +gelf_xlatetof(Elf *e, Elf_Data *dst, const Elf_Data *src, + unsigned int encoding) +{ + if (e != NULL) + return (_libelf_xlate(dst, src, encoding, e->e_class, + ELF_TOFILE)); + LIBELF_SET_ERROR(ARGUMENT, 0); + return (NULL); +} diff --git a/rocclr/compiler/lib/loaders/elf/utils/libelf/libelf.h b/rocclr/compiler/lib/loaders/elf/utils/libelf/libelf.h new file mode 100644 index 0000000000..c7ac45e6d7 --- /dev/null +++ b/rocclr/compiler/lib/loaders/elf/utils/libelf/libelf.h @@ -0,0 +1,265 @@ +/*- + * Copyright (c) 2006,2008-2010 Joseph Koshy + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * $Id: libelf.h 1345 2011-01-01 11:17:52Z jkoshy $ + */ + +#ifndef _LIBELF_H_ +#define _LIBELF_H_ + +#include +#include +#include +#ifdef ANDROID +#include "sys/types.h" +#endif +#ifndef EM_HSAIL +#define EM_HSAIL 0xAF5A +#endif + +/* Library private data structures */ +typedef struct _Elf Elf; +typedef struct _Elf_Scn Elf_Scn; +typedef struct _Elf_Mem Elf_Mem; + +/* File types */ +typedef enum { + ELF_K_NONE = 0, + ELF_K_AR, /* `ar' archives */ + ELF_K_COFF, /* COFF files (unsupported) */ + ELF_K_ELF, /* ELF files */ + ELF_K_NUM +} Elf_Kind; + +#define ELF_K_FIRST ELF_K_NONE +#define ELF_K_LAST ELF_K_NUM + +/* Data types */ +typedef enum { + ELF_T_ADDR, + ELF_T_BYTE, + ELF_T_CAP, + ELF_T_DYN, + ELF_T_EHDR, + ELF_T_HALF, + ELF_T_LWORD, + ELF_T_MOVE, + ELF_T_MOVEP, + ELF_T_NOTE, + ELF_T_OFF, + ELF_T_PHDR, + ELF_T_REL, + ELF_T_RELA, + ELF_T_SHDR, + ELF_T_SWORD, + ELF_T_SXWORD, + ELF_T_SYMINFO, + ELF_T_SYM, + ELF_T_VDEF, + ELF_T_VNEED, + ELF_T_WORD, + ELF_T_XWORD, + ELF_T_GNUHASH, /* GNU style hash tables. */ + ELF_T_NUM +} Elf_Type; + +#define ELF_T_FIRST ELF_T_ADDR +#define ELF_T_LAST ELF_T_GNUHASH + +/* Commands */ +typedef enum { + ELF_C_NULL = 0, + ELF_C_CLR, + ELF_C_FDDONE, + ELF_C_FDREAD, + ELF_C_RDWR, + ELF_C_READ, + ELF_C_SET, + ELF_C_WRITE, + ELF_C_NUM +} Elf_Cmd; + +#define ELF_C_FIRST ELF_C_NULL +#define ELF_C_LAST ELF_C_NUM + +/* + * An `Elf_Data' structure describes data in an + * ELF section. + */ +typedef struct _Elf_Data { + /* + * `Public' members that are part of the ELF(3) API. + */ + uint64_t d_align; + void *d_buf; + uint64_t d_off; + uint64_t d_size; + Elf_Type d_type; + unsigned int d_version; + + /* + * Members that are not part of the public API. + */ + Elf_Scn *d_scn; /* containing section */ + unsigned int d_flags; + STAILQ_ENTRY(_Elf_Data) d_next; +} Elf_Data; + +/* + * An `Elf_Arhdr' structure describes an archive + * header. + */ +typedef struct { + time_t ar_date; + char *ar_name; /* archive member name */ + gid_t ar_gid; + mode_t ar_mode; + char *ar_rawname; /* 'raw' member name */ + size_t ar_size; + uid_t ar_uid; + + /* + * Members that are not part of the public API. + */ + int ar_flags; +} Elf_Arhdr; + +/* + * An `Elf_Arsym' describes an entry in the archive + * symbol table. + */ +typedef struct { + off_t as_off; /* byte offset to member's header */ + unsigned long as_hash; /* elf_hash() value for name */ + char *as_name; /* null terminated symbol name */ +} Elf_Arsym; + +/* + * Error numbers. + */ + +enum Elf_Error { + ELF_E_NONE, /* No error */ + ELF_E_ARCHIVE, /* Malformed ar(1) archive */ + ELF_E_ARGUMENT, /* Invalid argument */ + ELF_E_CLASS, /* Mismatched ELF class */ + ELF_E_DATA, /* Invalid data descriptor */ + ELF_E_HEADER, /* Missing or malformed ELF header */ + ELF_E_IO, /* I/O error */ + ELF_E_LAYOUT, /* Layout constraint violation */ + ELF_E_MODE, /* Wrong mode for ELF descriptor */ + ELF_E_RANGE, /* Value out of range */ + ELF_E_RESOURCE, /* Resource exhaustion */ + ELF_E_SECTION, /* Invalid section descriptor */ + ELF_E_SEQUENCE, /* API calls out of sequence */ + ELF_E_UNIMPL, /* Feature is unimplemented */ + ELF_E_VERSION, /* Unknown API version */ + ELF_E_NUM /* Max error number */ +}; + +/* + * Flags defined by the API. + */ + +#define ELF_F_LAYOUT 0x001U /* application will layout the file */ +#define ELF_F_DIRTY 0x002U /* a section or ELF file is dirty */ + +/* ELF(3) API extensions. */ +#define ELF_F_ARCHIVE 0x100U /* archive creation */ +#define ELF_F_ARCHIVE_SYSV 0x200U /* SYSV style archive */ + +__BEGIN_DECLS +Elf *elf_begin(int _fd, Elf_Cmd _cmd, Elf *_elf, Elf_Mem *_mem); +int elf_cntl(Elf *_elf, Elf_Cmd _cmd); +int elf_end(Elf *_elf); +const char *elf_errmsg(int _error); +int elf_errno(void); +void elf_fill(int _fill); +unsigned int elf_flagarhdr(Elf_Arhdr *_arh, Elf_Cmd _cmd, + unsigned int _flags); +unsigned int elf_flagdata(Elf_Data *_data, Elf_Cmd _cmd, + unsigned int _flags); +unsigned int elf_flagehdr(Elf *_elf, Elf_Cmd _cmd, unsigned int _flags); +unsigned int elf_flagelf(Elf *_elf, Elf_Cmd _cmd, unsigned int _flags); +unsigned int elf_flagphdr(Elf *_elf, Elf_Cmd _cmd, unsigned int _flags); +unsigned int elf_flagscn(Elf_Scn *_scn, Elf_Cmd _cmd, unsigned int _flags); +unsigned int elf_flagshdr(Elf_Scn *_scn, Elf_Cmd _cmd, unsigned int _flags); +Elf_Arhdr *elf_getarhdr(Elf *_elf); +Elf_Arsym *elf_getarsym(Elf *_elf, size_t *_ptr); +off_t elf_getbase(Elf *_elf); +Elf_Data *elf_getdata(Elf_Scn *, Elf_Data *); +void elf_removedata(Elf_Scn *s, Elf_Data *d); +char *elf_getident(Elf *_elf, size_t *_ptr); +int elf_getphdrnum(Elf *_elf, size_t *_dst); +int elf_getphnum(Elf *_elf, size_t *_dst); /* Deprecated */ +Elf_Scn *elf_getscn(Elf *_elf, size_t _index); +int elf_getshdrnum(Elf *_elf, size_t *_dst); +int elf_getshnum(Elf *_elf, size_t *_dst); /* Deprecated */ +int elf_getshdrstrndx(Elf *_elf, size_t *_dst); +int elf_getshstrndx(Elf *_elf, size_t *_dst); /* Deprecated */ +unsigned long elf_hash(const char *_name); +Elf_Kind elf_kind(Elf *_elf); +Elf *elf_memory(char *_image, size_t _size, Elf_Mem *mem); +size_t elf_ndxscn(Elf_Scn *_scn); +Elf_Data *elf_newdata(Elf_Scn *_scn); +Elf_Scn *elf_newscn(Elf *_elf); +Elf_Scn *elf_nextscn(Elf *_elf, Elf_Scn *_scn); +Elf_Cmd elf_next(Elf *_elf); +off_t elf_rand(Elf *_elf, off_t _off); +Elf_Data *elf_rawdata(Elf_Scn *_scn, Elf_Data *_data); +char *elf_rawfile(Elf *_elf, size_t *_size); +int elf_setshstrndx(Elf *_elf, size_t _shnum); +char *elf_strptr(Elf *_elf, size_t _section, size_t _offset); +off_t elf_update(Elf *_elf, Elf_Cmd _cmd); +unsigned int elf_version(unsigned int _version); + +long elf32_checksum(Elf *_elf); +size_t elf32_fsize(Elf_Type _type, size_t _count, + unsigned int _version); +Elf32_Ehdr *elf32_getehdr(Elf *_elf); +Elf32_Phdr *elf32_getphdr(Elf *_elf); +Elf32_Shdr *elf32_getshdr(Elf_Scn *_scn); +Elf32_Ehdr *elf32_newehdr(Elf *_elf); +Elf32_Phdr *elf32_newphdr(Elf *_elf, size_t _count); +Elf_Data *elf32_xlatetof(Elf_Data *_dst, const Elf_Data *_src, + unsigned int _enc); +Elf_Data *elf32_xlatetom(Elf_Data *_dst, const Elf_Data *_src, + unsigned int _enc); + +long elf64_checksum(Elf *_elf); +size_t elf64_fsize(Elf_Type _type, size_t _count, + unsigned int _version); +Elf64_Ehdr *elf64_getehdr(Elf *_elf); +Elf64_Phdr *elf64_getphdr(Elf *_elf); +Elf64_Shdr *elf64_getshdr(Elf_Scn *_scn); +Elf64_Ehdr *elf64_newehdr(Elf *_elf); +Elf64_Phdr *elf64_newphdr(Elf *_elf, size_t _count); +Elf_Data *elf64_xlatetof(Elf_Data *_dst, const Elf_Data *_src, + unsigned int _enc); +Elf_Data *elf64_xlatetom(Elf_Data *_dst, const Elf_Data *_src, + unsigned int _enc); +__END_DECLS + +#endif /* _LIBELF_H_ */ diff --git a/rocclr/compiler/lib/loaders/elf/utils/libelf/libelf_align.c b/rocclr/compiler/lib/loaders/elf/utils/libelf/libelf_align.c new file mode 100644 index 0000000000..bd14a137eb --- /dev/null +++ b/rocclr/compiler/lib/loaders/elf/utils/libelf/libelf_align.c @@ -0,0 +1,185 @@ +/*- + * Copyright (c) 2006,2008 Joseph Koshy + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include + +#include + +#include + +#include "_libelf.h" + +LIBELF_VCSID("$Id: libelf_align.c 1169 2010-09-04 01:06:31Z jkoshy $"); + +struct align { + int a32; + int a64; +}; + +#ifdef __GNUC__ +#define MALIGN(N) { \ + .a32 = __alignof__(Elf32_##N), \ + .a64 = __alignof__(Elf64_##N) \ + } +#define MALIGN64(V) { \ + .a32 = 0, \ + .a64 = __alignof__(Elf64_##V) \ + } +#define MALIGN_WORD() { \ + .a32 = __alignof__(int32_t), \ + .a64 = __alignof__(int64_t) \ + } +#elif !defined(_MSC_VER) +#error Need the __alignof__ builtin. +#endif +#define UNSUPPORTED() { \ + .a32 = 0, \ + .a64 = 0 \ + } + +static struct align malign[ELF_T_NUM] = { +#if defined(__GNUC__) + [ELF_T_ADDR] = MALIGN(Addr), + [ELF_T_BYTE] = { .a32 = 1, .a64 = 1 }, +#if LIBELF_CONFIG_CAP + [ELF_T_CAP] = MALIGN(Cap), +#endif + [ELF_T_DYN] = MALIGN(Dyn), + [ELF_T_EHDR] = MALIGN(Ehdr), + [ELF_T_HALF] = MALIGN(Half), +#if LIBELF_CONFIG_LWORD + [ELF_T_LWORD] = MALIGN(Lword), +#endif +#if LIBELF_CONFIG_MOVE + [ELF_T_MOVE] = MALIGN(Move), +#endif + [ELF_T_MOVEP] = UNSUPPORTED(), +#if LIBELF_CONFIG_NOTE + [ELF_T_NOTE] = MALIGN(Nhdr), +#endif + [ELF_T_OFF] = MALIGN(Off), + [ELF_T_PHDR] = MALIGN(Phdr), + [ELF_T_REL] = MALIGN(Rel), + [ELF_T_RELA] = MALIGN(Rela), + [ELF_T_SHDR] = MALIGN(Shdr), + [ELF_T_SWORD] = MALIGN(Sword), + [ELF_T_SXWORD] = MALIGN64(Sxword), + [ELF_T_SYM] = MALIGN(Sym), +#if LIBELF_CONFIG_SYMINFO + [ELF_T_SYMINFO] = MALIGN(Syminfo), +#endif +#if LIBELF_CONFIG_VDEF + [ELF_T_VDEF] = MALIGN(Verdef), +#endif +#if LIBELF_CONFIG_VNEED + [ELF_T_VNEED] = MALIGN(Verneed), +#endif + [ELF_T_WORD] = MALIGN(Word), + [ELF_T_XWORD] = MALIGN64(Xword), + [ELF_T_GNUHASH] = MALIGN_WORD() +#elif defined(_MSC_VER) + { 4, 8 }, { 1, 1 }, { 4, 8 }, { 4, 8 }, { 4, 8 }, + { 2, 2 }, { 8, 8 }, { 8, 8 }, { 0, 0 }, { 4, 4 }, + { 4, 8 }, { 4, 8 }, { 4, 8 }, { 4, 8 }, { 4, 8 }, + { 4, 4 }, { 0, 8 }, { 4, 8 }, { 2, 2 }, { 4, 4 }, + { 4, 4 }, { 4, 4 }, { 0, 8 }, { 4, 8 } +#else +#error +#endif +}; + +int +_libelf_malign(Elf_Type t, int elfclass) +{ + if (t >= ELF_T_NUM || (int) t < 0) + return (0); + + return (elfclass == ELFCLASS32 ? malign[t].a32 : + malign[t].a64); +} + +#define FALIGN(A32,A64) { .a32 = (A32), .a64 = (A64) } + +static struct align falign[ELF_T_NUM] = { +#if defined(__GNUC__) + [ELF_T_ADDR] = FALIGN(4,8), + [ELF_T_BYTE] = FALIGN(1,1), +#if LIBELF_CONFIG_CAP + [ELF_T_CAP] = FALIGN(4,8), +#endif + [ELF_T_DYN] = FALIGN(4,8), + [ELF_T_EHDR] = FALIGN(4,8), + [ELF_T_HALF] = FALIGN(2,2), +#if LIBELF_CONFIG_LWORD + [ELF_T_LWORD] = FALIGN(8,8), +#endif +#if LIBELF_CONFIG_MOVE + [ELF_T_MOVE] = FALIGN(8,8), +#endif + [ELF_T_MOVEP] = UNSUPPORTED(), +#if LIBELF_CONFIG_NOTE + [ELF_T_NOTE] = FALIGN(4,4), +#endif + [ELF_T_OFF] = FALIGN(4,8), + [ELF_T_PHDR] = FALIGN(4,8), + [ELF_T_REL] = FALIGN(4,8), + [ELF_T_RELA] = FALIGN(4,8), + [ELF_T_SHDR] = FALIGN(4,8), + [ELF_T_SWORD] = FALIGN(4,4), + [ELF_T_SXWORD] = FALIGN(0,8), + [ELF_T_SYM] = FALIGN(4,8), +#if LIBELF_CONFIG_SYMINFO + [ELF_T_SYMINFO] = FALIGN(2,2), +#endif +#if LIBELF_CONFIG_VDEF + [ELF_T_VDEF] = FALIGN(4,4), +#endif +#if LIBELF_CONFIG_VNEED + [ELF_T_VNEED] = FALIGN(4,4), +#endif + [ELF_T_WORD] = FALIGN(4,4), + [ELF_T_XWORD] = FALIGN(0,8), + [ELF_T_GNUHASH] = FALIGN(4,8) +#elif defined(_MSC_VER) + { 4, 8 }, { 1, 1 }, { 4, 8 }, { 4, 8 }, { 4, 8 }, + { 2, 2 }, { 8, 8 }, { 8, 8 }, { 0, 0 }, { 4, 4 }, + { 4, 8 }, { 4, 8 }, { 4, 8 }, { 4, 8 }, { 4, 8 }, + { 4, 4 }, { 0, 8 }, { 4, 8 }, { 2, 2 }, { 4, 4 }, + { 4, 4 }, { 4, 4 }, { 0, 8 }, { 4, 8 } +#else +#error +#endif +}; + +int +_libelf_falign(Elf_Type t, int elfclass) +{ + if (t >= ELF_T_NUM || (int) t < 0) + return (0); + + return (elfclass == ELFCLASS32 ? falign[t].a32 : + falign[t].a64); +} diff --git a/rocclr/compiler/lib/loaders/elf/utils/libelf/libelf_allocate.c b/rocclr/compiler/lib/loaders/elf/utils/libelf/libelf_allocate.c new file mode 100644 index 0000000000..254f00e698 --- /dev/null +++ b/rocclr/compiler/lib/loaders/elf/utils/libelf/libelf_allocate.c @@ -0,0 +1,229 @@ +/*- + * Copyright (c) 2006,2008,2010 Joseph Koshy + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +/* + * Internal APIs + */ + +#include + +#if !defined(WIN32) +#include +#endif + +#include +#include +#include +#include +#include + +#include "_libelf.h" + +LIBELF_VCSID("$Id: libelf_allocate.c 1341 2011-01-01 04:28:29Z jkoshy $"); + +Elf * +_libelf_allocate_elf(Elf_Mem *mem) +{ + Elf *e; + + if (mem != NULL) { + if ((e = mem->alloc(sizeof(*e))) == NULL) { + LIBELF_SET_ERROR(RESOURCE, errno); + return NULL; + } + e->e_mem.dealloc = mem->dealloc; + e->e_mem.alloc = mem->alloc; + } else { + if ((e = malloc(sizeof(*e))) == NULL) { + LIBELF_SET_ERROR(RESOURCE, errno); + return NULL; + } + e->e_mem.dealloc = &free; + e->e_mem.alloc = &malloc; + } + + e->e_activations = 1; + e->e_hdr.e_rawhdr = NULL; + e->e_byteorder = ELFDATANONE; + e->e_class = ELFCLASSNONE; + e->e_cmd = ELF_C_NULL; + e->e_fd = -1; + e->e_flags = 0; + e->e_kind = ELF_K_NONE; + e->e_parent = NULL; + e->e_rawfile = NULL; + e->e_rawsize = 0; + e->e_version = LIBELF_PRIVATE(version); + + (void) memset(&e->e_u, 0, sizeof(e->e_u)); + + return (e); +} + +void +_libelf_init_elf(Elf *e, Elf_Kind kind) +{ + assert(e != NULL); + assert(e->e_kind == ELF_K_NONE); + + e->e_kind = kind; + + switch (kind) { + case ELF_K_ELF: + STAILQ_INIT(&e->e_u.e_elf.e_scn); + break; + default: + break; + } +} + +#define FREE(E, P) do { \ + if (P) \ + E->e_mem.dealloc(P); \ + } while (0) + + +Elf * +_libelf_release_elf(Elf *e) +{ + Elf_Arhdr *arh; + + switch (e->e_kind) { + case ELF_K_AR: + FREE(e, e->e_u.e_ar.e_symtab); + break; + + case ELF_K_ELF: + switch (e->e_class) { + case ELFCLASS32: + FREE(e, e->e_u.e_elf.e_ehdr.e_ehdr32); + FREE(e, e->e_u.e_elf.e_phdr.e_phdr32); + break; + case ELFCLASS64: + FREE(e, e->e_u.e_elf.e_ehdr.e_ehdr64); + FREE(e, e->e_u.e_elf.e_phdr.e_phdr64); + break; + } + + assert(STAILQ_EMPTY(&e->e_u.e_elf.e_scn)); + + if (e->e_flags & LIBELF_F_AR_HEADER) { + arh = e->e_hdr.e_arhdr; + FREE(e, arh->ar_name); + FREE(e, arh->ar_rawname); + e->e_mem.dealloc(arh); + } + + break; + + default: + break; + } + + e->e_mem.dealloc(e); + + return (NULL); +} + +Elf_Data * +_libelf_allocate_data(Elf_Scn *s) +{ + Elf_Data *d; + + if ((d = s->s_elf->e_mem.alloc(sizeof(Elf_Data))) == NULL) { + LIBELF_SET_ERROR(RESOURCE, 0); + return (NULL); + } + memset(d, 0, sizeof(*d)); + + d->d_scn = s; + + return (d); +} + +Elf_Data * +_libelf_release_data(Elf_Data *d) +{ + + if (d->d_flags & LIBELF_F_DATA_MALLOCED) + d->d_scn->s_elf->e_mem.dealloc(d->d_buf); + + d->d_scn->s_elf->e_mem.dealloc(d); + + return (NULL); +} + +Elf_Scn * +_libelf_allocate_scn(Elf *e, size_t ndx) +{ + Elf_Scn *s; + + if ((s = e->e_mem.alloc(sizeof(Elf_Scn))) == NULL) { + LIBELF_SET_ERROR(RESOURCE, errno); + return (NULL); + } + memset(s, 0, sizeof(*s)); + + s->s_elf = e; + s->s_ndx = ndx; + + STAILQ_INIT(&s->s_data); + STAILQ_INIT(&s->s_rawdata); + + STAILQ_INSERT_TAIL(&e->e_u.e_elf.e_scn, s, s_next); + + return (s); +} + +Elf_Scn * +_libelf_release_scn(Elf_Scn *s) +{ + Elf *e; + Elf_Data *d, *td; + + assert(s != NULL); + + STAILQ_FOREACH_SAFE(d, &s->s_data, d_next, td) { + STAILQ_REMOVE(&s->s_data, d, _Elf_Data, d_next); + d = _libelf_release_data(d); + } + + STAILQ_FOREACH_SAFE(d, &s->s_rawdata, d_next, td) { + assert((d->d_flags & LIBELF_F_DATA_MALLOCED) == 0); + STAILQ_REMOVE(&s->s_rawdata, d, _Elf_Data, d_next); + d = _libelf_release_data(d); + } + + e = s->s_elf; + + assert(e != NULL); + + STAILQ_REMOVE(&e->e_u.e_elf.e_scn, s, _Elf_Scn, s_next); + + e->e_mem.dealloc(s); + + return (NULL); +} diff --git a/rocclr/compiler/lib/loaders/elf/utils/libelf/libelf_ar.c b/rocclr/compiler/lib/loaders/elf/utils/libelf/libelf_ar.c new file mode 100644 index 0000000000..30ebb0d7c3 --- /dev/null +++ b/rocclr/compiler/lib/loaders/elf/utils/libelf/libelf_ar.c @@ -0,0 +1,461 @@ +/*- + * Copyright (c) 2006,2008,2010 Joseph Koshy + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS `AS IS' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include + +#include +#include +#include +#include +#include + +#include "_libelf.h" +#include "_libelf_ar.h" + +LIBELF_VCSID("$Id: libelf_ar.c 1341 2011-01-01 04:28:29Z jkoshy $"); + +#define LIBELF_NALLOC_SIZE 16 + +/* + * `ar' archive handling. + * + * `ar' archives start with signature `ARMAG'. Each archive member is + * preceded by a header containing meta-data for the member. This + * header is described in (struct ar_hdr). The header always + * starts on an even address. File data is padded with "\n" + * characters to keep this invariant. + * + * Special considerations for `ar' archives: + * + * There are two variants of the `ar' archive format: traditional BSD + * and SVR4. These differ in the way long file names are treated, and + * in the layout of the archive symbol table. + * + * The `ar' header only has space for a 16 character file name. + * + * In the SVR4 format, file names are terminated with a '/', so this + * effectively leaves 15 characters for the actual file name. Longer + * file names stored in a separate 'string table' and referenced + * indirectly from the name field. The string table itself appears as + * an archive member with name "// ". An `indirect' file name in an + * `ar' header matches the pattern "/[0-9]*". The digits form a + * decimal number that corresponds to a byte offset into the string + * table where the actual file name of the object starts. Strings in + * the string table are padded to start on even addresses. + * + * In the BSD format, file names can be upto 16 characters. File + * names shorter than 16 characters are padded to 16 characters using + * (ASCII) space characters. File names with embedded spaces and file + * names longer than 16 characters are stored immediately after the + * archive header and the name field set to a special indirect name + * matching the pattern "#1/[0-9]+". The digits form a decimal number + * that corresponds to the actual length of the file name following + * the archive header. The content of the archive member immediately + * follows the file name, and the size field of the archive member + * holds the sum of the sizes of the member and of the appended file + * name. + * + * Archives may also have a symbol table (see ranlib(1)), mapping + * program symbols to object files inside the archive. + * + * In the SVR4 format, a symbol table uses a file name of "/ " in its + * archive header. The symbol table is structured as: + * - a 4-byte count of entries stored as a binary value, MSB first + * - 'n' 4-byte offsets, stored as binary values, MSB first + * - 'n' NUL-terminated strings, for ELF symbol names, stored unpadded. + * + * In the BSD format, the symbol table uses a file name of "__.SYMDEF". + * It is structured as two parts: + * - The first part is an array of "ranlib" structures preceded by + * the size of the array in bytes. Each "ranlib" structure + * describes one symbol. Each structure contains an offset into + * the string table for the symbol name, and a file offset into the + * archive for the member defining the symbol. + * - The second part is a string table containing NUL-terminated + * strings, preceded by the size of the string table in bytes. + * + * If the symbol table and string table are is present in an archive + * they must be the very first objects and in that order. + */ + + +/* + * Retrieve an archive header descriptor. + */ + +Elf_Arhdr * +_libelf_ar_gethdr(Elf *e) +{ + Elf *parent; + char *namelen; + Elf_Arhdr *eh; + size_t n, nlen; + struct ar_hdr *arh; + + if ((parent = e->e_parent) == NULL) { + LIBELF_SET_ERROR(ARGUMENT, 0); + return (NULL); + } + + assert((e->e_flags & LIBELF_F_AR_HEADER) == 0); + + arh = (struct ar_hdr *) (uintptr_t) e->e_hdr.e_rawhdr; + + assert((uintptr_t) arh >= (uintptr_t) parent->e_rawfile + SARMAG); + assert((uintptr_t) arh <= (uintptr_t) parent->e_rawfile + + parent->e_rawsize - sizeof(struct ar_hdr)); + + if ((eh = e->e_mem.alloc(sizeof(Elf_Arhdr))) == NULL) { + LIBELF_SET_ERROR(RESOURCE, 0); + return (NULL); + } + + e->e_hdr.e_arhdr = eh; + e->e_flags |= LIBELF_F_AR_HEADER; + + eh->ar_name = eh->ar_rawname = NULL; + + if ((eh->ar_name = _libelf_ar_get_translated_name(arh, parent)) == + NULL) + goto error; + + if (_libelf_ar_get_number(arh->ar_uid, sizeof(arh->ar_uid), 10, + &n) == 0) + goto error; + eh->ar_uid = (uid_t) n; + + if (_libelf_ar_get_number(arh->ar_gid, sizeof(arh->ar_gid), 10, + &n) == 0) + goto error; + eh->ar_gid = (gid_t) n; + + if (_libelf_ar_get_number(arh->ar_mode, sizeof(arh->ar_mode), 8, + &n) == 0) + goto error; + eh->ar_mode = (mode_t) n; + + if (_libelf_ar_get_number(arh->ar_size, sizeof(arh->ar_size), 10, + &n) == 0) + goto error; + + /* + * Get the true size of the member if extended naming is being used. + */ + if (IS_EXTENDED_BSD_NAME(arh->ar_name)) { + namelen = arh->ar_name + + LIBELF_AR_BSD_EXTENDED_NAME_PREFIX_SIZE; + if (_libelf_ar_get_number(namelen, sizeof(arh->ar_name) - + LIBELF_AR_BSD_EXTENDED_NAME_PREFIX_SIZE, 10, &nlen) == 0) + goto error; + n -= nlen; + } + + eh->ar_size = n; + + if ((eh->ar_rawname = _libelf_ar_get_raw_name(arh)) == NULL) + goto error; + + eh->ar_flags = 0; + + return (eh); + + error: + if (eh) { + if (eh->ar_name) + e->e_mem.dealloc(eh->ar_name); + if (eh->ar_rawname) + e->e_mem.dealloc(eh->ar_rawname); + e->e_mem.dealloc(eh); + } + + e->e_flags &= ~LIBELF_F_AR_HEADER; + e->e_hdr.e_rawhdr = (char *) arh; + + return (NULL); +} + +Elf * +_libelf_ar_open_member(int fd, Elf_Cmd c, Elf *elf, Elf_Mem* mem) +{ + Elf *e; + char *member, *namelen; + size_t nsz, sz; + off_t next; + struct ar_hdr *arh; + + assert(elf->e_kind == ELF_K_AR); + + next = elf->e_u.e_ar.e_next; + + /* + * `next' is only set to zero by elf_next() when the last + * member of an archive is processed. + */ + if (next == (off_t) 0) + return (NULL); + + assert((next & 1) == 0); + + arh = (struct ar_hdr *) (elf->e_rawfile + next); + + /* + * Retrieve the size of the member. + */ + if (_libelf_ar_get_number(arh->ar_size, sizeof(arh->ar_size), 10, + &sz) == 0) { + LIBELF_SET_ERROR(ARCHIVE, 0); + return (NULL); + } + + /* + * Adjust the size field for members in BSD archives using + * extended naming. + */ + if (IS_EXTENDED_BSD_NAME(arh->ar_name)) { + namelen = arh->ar_name + + LIBELF_AR_BSD_EXTENDED_NAME_PREFIX_SIZE; + if (_libelf_ar_get_number(namelen, sizeof(arh->ar_name) - + LIBELF_AR_BSD_EXTENDED_NAME_PREFIX_SIZE, 10, &nsz) == 0) { + LIBELF_SET_ERROR(ARCHIVE, 0); + return (NULL); + } + + member = (char *) (arh + 1) + nsz; + sz -= nsz; + } else + member = (char *) (arh + 1); + + + if ((e = elf_memory((char *) member, sz, mem)) == NULL) + return (NULL); + + e->e_fd = fd; + e->e_cmd = c; + e->e_hdr.e_rawhdr = (char *) arh; + + elf->e_u.e_ar.e_nchildren++; + e->e_parent = elf; + + return (e); +} + +/* + * A BSD-style ar(1) symbol table has the following layout: + * + * - A count of bytes used by the following array of 'ranlib' + * structures, stored as a 'long'. + * - An array of 'ranlib' structures. Each array element is + * two 'long's in size. + * - A count of bytes used for the following symbol table. + * - The symbol table itself. + */ + +/* + * A helper macro to read in a 'long' value from the archive. We use + * memcpy() since the source pointer may be misaligned with respect to + * the natural alignment for a C 'long'. + */ +#define GET_LONG(P, V)do { \ + memcpy(&(V), (P), sizeof(long)); \ + (P) += sizeof(long); \ + } while (0) + +Elf_Arsym * +_libelf_ar_process_bsd_symtab(Elf *e, size_t *count) +{ + Elf_Arsym *symtab, *sym; + unsigned char *end, *p, *p0, *s, *s0; + const unsigned int entrysize = 2 * sizeof(long); + long arraysize, fileoffset, n, nentries, stroffset, strtabsize; + + assert(e != NULL); + assert(count != NULL); + assert(e->e_u.e_ar.e_symtab == NULL); + + symtab = NULL; + + /* + * The BSD symbol table always contains the count fields even + * if there are no entries in it. + */ + if (e->e_u.e_ar.e_rawsymtabsz < 2 * sizeof(long)) + goto symtaberror; + + p = p0 = (unsigned char *) e->e_u.e_ar.e_rawsymtab; + end = p0 + e->e_u.e_ar.e_rawsymtabsz; + + /* + * Retrieve the size of the array of ranlib descriptors and + * check it for validity. + */ + GET_LONG(p, arraysize); + + if (p0 + arraysize >= end || (arraysize % entrysize != 0)) + goto symtaberror; + + /* + * Check the value of the string table size. + */ + s = p + arraysize; + GET_LONG(s, strtabsize); + + s0 = s; /* Start of string table. */ + if (s0 + strtabsize > end) + goto symtaberror; + + nentries = arraysize / entrysize; + + /* + * Allocate space for the returned Elf_Arsym array. + */ + if ((symtab = e->e_mem.alloc(sizeof(Elf_Arsym) * (nentries + 1))) == NULL) { + LIBELF_SET_ERROR(RESOURCE, 0); + return (NULL); + } + + /* Read in symbol table entries. */ + for (n = 0, sym = symtab; n < nentries; n++, sym++) { + GET_LONG(p, stroffset); + GET_LONG(p, fileoffset); + + s = s0 + stroffset; + + if (s >= end) + goto symtaberror; + + sym->as_off = fileoffset; + sym->as_hash = elf_hash((char *) s); + sym->as_name = (char *) s; + } + + /* Fill up the sentinel entry. */ + sym->as_name = NULL; + sym->as_hash = ~0UL; + sym->as_off = (off_t) 0; + + /* Remember the processed symbol table. */ + e->e_u.e_ar.e_symtab = symtab; + + *count = e->e_u.e_ar.e_symtabsz = nentries + 1; + + return (symtab); + +symtaberror: + if (symtab) + e->e_mem.dealloc(symtab); + LIBELF_SET_ERROR(ARCHIVE, 0); + return (NULL); +} + +/* + * An SVR4-style ar(1) symbol table has the following layout: + * + * - The first 4 bytes are a binary count of the number of entries in the + * symbol table, stored MSB-first. + * - Then there are 'n' 4-byte binary offsets, also stored MSB first. + * - Following this, there are 'n' null-terminated strings. + */ + +#define GET_WORD(P, V) do { \ + (V) = 0; \ + (V) = (P)[0]; (V) <<= 8; \ + (V) += (P)[1]; (V) <<= 8; \ + (V) += (P)[2]; (V) <<= 8; \ + (V) += (P)[3]; \ + } while (0) + +#define INTSZ 4 + + +Elf_Arsym * +_libelf_ar_process_svr4_symtab(Elf *e, size_t *count) +{ + size_t n, nentries, off; + Elf_Arsym *symtab, *sym; + char *p, *s, *end; + + assert(e != NULL); + assert(count != NULL); + assert(e->e_u.e_ar.e_symtab == NULL); + + symtab = NULL; + + if (e->e_u.e_ar.e_rawsymtabsz < INTSZ) + goto symtaberror; + + p = e->e_u.e_ar.e_rawsymtab; + end = p + e->e_u.e_ar.e_rawsymtabsz; + + GET_WORD(p, nentries); + p += INTSZ; + + if (nentries == 0 || p + nentries * INTSZ >= end) + goto symtaberror; + + /* Allocate space for a nentries + a sentinel. */ + if ((symtab = e->e_mem.alloc(sizeof(Elf_Arsym) * (nentries+1))) == NULL) { + LIBELF_SET_ERROR(RESOURCE, 0); + return (NULL); + } + + s = p + (nentries * INTSZ); /* start of the string table. */ + + for (n = nentries, sym = symtab; n > 0; n--) { + + if (s >= end) + goto symtaberror; + + off = 0; + + GET_WORD(p, off); + + sym->as_off = off; + sym->as_hash = elf_hash((char *) s); + sym->as_name = (char *) s; + + p += INTSZ; + sym++; + + for (; s < end && *s++ != '\0';) /* skip to next string */ + ; + } + + /* Fill up the sentinel entry. */ + sym->as_name = NULL; + sym->as_hash = ~0UL; + sym->as_off = (off_t) 0; + + *count = e->e_u.e_ar.e_symtabsz = nentries + 1; + e->e_u.e_ar.e_symtab = symtab; + + return (symtab); + +symtaberror: + if (symtab) + e->e_mem.dealloc(symtab); + LIBELF_SET_ERROR(ARCHIVE, 0); + return (NULL); +} diff --git a/rocclr/compiler/lib/loaders/elf/utils/libelf/libelf_ar_util.c b/rocclr/compiler/lib/loaders/elf/utils/libelf/libelf_ar_util.c new file mode 100644 index 0000000000..70681aacda --- /dev/null +++ b/rocclr/compiler/lib/loaders/elf/utils/libelf/libelf_ar_util.c @@ -0,0 +1,354 @@ +/*- + * Copyright (c) 2006,2009,2010 Joseph Koshy + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS `AS IS' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include + +#include +#include +#include +#include + +#include "_libelf.h" +#include "_libelf_ar.h" + +LIBELF_VCSID("$Id: libelf_ar_util.c 2066 2011-10-26 15:40:28Z jkoshy $"); + +/* + * Convert a string bounded by `start' and `start+sz' (exclusive) to a + * number in the specified base. + */ +int +_libelf_ar_get_number(const char *s, size_t sz, int base, size_t *ret) +{ + int c, v; + size_t r; + const char *e; + + assert(base <= 10); + + e = s + sz; + + /* skip leading blanks */ + for (;s < e && (c = *s) == ' '; s++) + ; + + r = 0L; + for (;s < e; s++) { + if ((c = *s) == ' ') + break; + if (c < '0' || c > '9') + return (0); + v = c - '0'; + if (v >= base) /* Illegal digit. */ + break; + r *= base; + r += v; + } + + *ret = r; + + return (1); +} + +/* + * Return the translated name for an archive member. + */ +char * +_libelf_ar_get_translated_name(const struct ar_hdr *arh, Elf *ar) +{ + char c, *s; + size_t len, offset; + const char *buf, *p, *q, *r; + const size_t bufsize = sizeof(arh->ar_name); + + assert(arh != NULL); + assert(ar->e_kind == ELF_K_AR); + assert((const char *) arh >= ar->e_rawfile && + (const char *) arh < ar->e_rawfile + ar->e_rawsize); + + buf = arh->ar_name; + + /* + * Check for extended naming. + * + * If the name matches the pattern "^/[0-9]+", it is an + * SVR4-style extended name. If the name matches the pattern + * "#1/[0-9]+", the entry uses BSD style extended naming. + */ + if (buf[0] == '/' && (c = buf[1]) >= '0' && c <= '9') { + /* + * The value in field ar_name is a decimal offset into + * the archive string table where the actual name + * resides. + */ + if (_libelf_ar_get_number(buf + 1, bufsize - 1, 10, + &offset) == 0) { + LIBELF_SET_ERROR(ARCHIVE, 0); + return (NULL); + } + + if (offset > ar->e_u.e_ar.e_rawstrtabsz) { + LIBELF_SET_ERROR(ARCHIVE, 0); + return (NULL); + } + + p = q = ar->e_u.e_ar.e_rawstrtab + offset; + r = ar->e_u.e_ar.e_rawstrtab + ar->e_u.e_ar.e_rawstrtabsz; + + for (; p < r && *p != '/'; p++) + ; + len = p - q + 1; /* space for the trailing NUL */ + + if ((s = ar->e_mem.alloc(len)) == NULL) { + LIBELF_SET_ERROR(RESOURCE, 0); + return (NULL); + } + + (void) strncpy(s, q, len - 1); + s[len - 1] = '\0'; + + return (s); + } else if (IS_EXTENDED_BSD_NAME(buf)) { + r = buf + LIBELF_AR_BSD_EXTENDED_NAME_PREFIX_SIZE; + + if (_libelf_ar_get_number(r, bufsize - + LIBELF_AR_BSD_EXTENDED_NAME_PREFIX_SIZE, 10, + &len) == 0) { + LIBELF_SET_ERROR(ARCHIVE, 0); + return (NULL); + } + + /* + * Allocate space for the file name plus a + * trailing NUL. + */ + if ((s = ar->e_mem.alloc(len + 1)) == NULL) { + LIBELF_SET_ERROR(RESOURCE, 0); + return (NULL); + } + + /* + * The file name follows the archive header. + */ + q = (const char *) (arh + 1); + + (void) strncpy(s, q, len); + s[len] = '\0'; + + return (s); + } + + /* + * A 'normal' name. + * + * Skip back over trailing blanks from the end of the field. + * In the SVR4 format, a '/' is used as a terminator for + * non-special names. + */ + for (q = buf + bufsize - 1; q >= buf && *q == ' '; --q) + ; + + if (q >= buf) { + if (*q == '/') { + /* + * SVR4 style names: ignore the trailing + * character '/', but only if the name is not + * one of the special names "/" and "//". + */ + if (q > buf + 1 || + (q == (buf + 1) && *buf != '/')) + q--; + } + + len = q - buf + 2; /* Add space for a trailing NUL. */ + } else { + /* The buffer only had blanks. */ + buf = ""; + len = 1; + } + + if ((s = ar->e_mem.alloc(len)) == NULL) { + LIBELF_SET_ERROR(RESOURCE, 0); + return (NULL); + } + + (void) strncpy(s, buf, len - 1); + s[len - 1] = '\0'; + + return (s); +} + +/* + * Return the raw name for an archive member, inclusive of any + * formatting characters. + */ +char * +_libelf_ar_get_raw_name(const struct ar_hdr *arh) +{ + char *rawname; + const size_t namesz = sizeof(arh->ar_name); + + if ((rawname = malloc(namesz + 1)) == NULL) { + LIBELF_SET_ERROR(RESOURCE, 0); + return (NULL); + } + + (void) strncpy(rawname, arh->ar_name, namesz); + rawname[namesz] = '\0'; + return (rawname); +} + +/* + * Open an 'ar' archive. + */ +Elf * +_libelf_ar_open(Elf *e) +{ + int scanahead; + char *s, *end; + size_t sz; + struct ar_hdr arh; + + e->e_kind = ELF_K_AR; + e->e_u.e_ar.e_nchildren = 0; + e->e_u.e_ar.e_next = (off_t) -1; + + /* + * Look for special members. + */ + + s = e->e_rawfile + SARMAG; + end = e->e_rawfile + e->e_rawsize; + + assert(e->e_rawsize > 0); + + /* + * We use heuristics to determine the flavor of the archive we + * are examining. + * + * SVR4 flavor archives use the name "/ " and "// " for + * special members. + * + * In BSD flavor archives the symbol table, if present, is the + * first archive with name "__.SYMDEF". + */ + +#define READ_AR_HEADER(S, ARH, SZ, END) \ + do { \ + if ((S) + sizeof((ARH)) > (END)) \ + goto error; \ + (void) memcpy(&(ARH), (S), sizeof((ARH))); \ + if ((ARH).ar_fmag[0] != '`' || (ARH).ar_fmag[1] != '\n') \ + goto error; \ + if (_libelf_ar_get_number((ARH).ar_size, \ + sizeof((ARH).ar_size), 10, &(SZ)) == 0) \ + goto error; \ + } while (0) + + READ_AR_HEADER(s, arh, sz, end); + + /* + * Handle special archive members for the SVR4 format. + */ + if (arh.ar_name[0] == '/') { + + assert(sz > 0); + + e->e_flags |= LIBELF_F_AR_VARIANT_SVR4; + + scanahead = 0; + + /* + * The symbol table (file name "/ ") always comes before the + * string table (file name "// "). + */ + if (arh.ar_name[1] == ' ') { + /* "/ " => symbol table. */ + scanahead = 1; /* The string table to follow. */ + + s += sizeof(arh); + e->e_u.e_ar.e_rawsymtab = s; + e->e_u.e_ar.e_rawsymtabsz = sz; + + sz = LIBELF_ADJUST_AR_SIZE(sz); + s += sz; + + } else if (arh.ar_name[1] == '/' && arh.ar_name[2] == ' ') { + /* "// " => string table for long file names. */ + s += sizeof(arh); + e->e_u.e_ar.e_rawstrtab = s; + e->e_u.e_ar.e_rawstrtabsz = sz; + + sz = LIBELF_ADJUST_AR_SIZE(sz); + s += sz; + } + + /* + * If the string table hasn't been seen yet, look for + * it in the next member. + */ + if (scanahead) { + READ_AR_HEADER(s, arh, sz, end); + + /* "// " => string table for long file names. */ + if (arh.ar_name[0] == '/' && arh.ar_name[1] == '/' && + arh.ar_name[2] == ' ') { + + s += sizeof(arh); + + e->e_u.e_ar.e_rawstrtab = s; + e->e_u.e_ar.e_rawstrtabsz = sz; + + sz = LIBELF_ADJUST_AR_SIZE(sz); + s += sz; + } + } + } else if (strncmp(arh.ar_name, LIBELF_AR_BSD_SYMTAB_NAME, + sizeof(LIBELF_AR_BSD_SYMTAB_NAME) - 1) == 0) { + /* + * BSD style archive symbol table. + */ + s += sizeof(arh); + e->e_u.e_ar.e_rawsymtab = s; + e->e_u.e_ar.e_rawsymtabsz = sz; + + sz = LIBELF_ADJUST_AR_SIZE(sz); + s += sz; + } + + /* + * Update the 'next' offset, so that a subsequent elf_begin() + * works as expected. + */ + e->e_u.e_ar.e_next = (off_t) (s - e->e_rawfile); + + return (e); + +error: + LIBELF_SET_ERROR(ARCHIVE, 0); + return (NULL); + +} diff --git a/rocclr/compiler/lib/loaders/elf/utils/libelf/libelf_checksum.c b/rocclr/compiler/lib/loaders/elf/utils/libelf/libelf_checksum.c new file mode 100644 index 0000000000..0bece9a7e3 --- /dev/null +++ b/rocclr/compiler/lib/loaders/elf/utils/libelf/libelf_checksum.c @@ -0,0 +1,100 @@ +/*- + * Copyright (c) 2006,2008 Joseph Koshy + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include + +#include + +#include "_libelf.h" + +LIBELF_VCSID("$Id: libelf_checksum.c 189 2008-07-20 10:38:08Z jkoshy $"); + +static unsigned long +_libelf_sum(unsigned long c, const unsigned char *s, size_t size) +{ + if (s == NULL || size == 0) + return (c); + + while (size--) + c += *s++; + + return (c); +} + +unsigned long +_libelf_checksum(Elf *e, int elfclass) +{ + size_t shn; + Elf_Scn *scn; + Elf_Data *d; + unsigned long checksum; + GElf_Ehdr eh; + GElf_Shdr shdr; + + if (e == NULL) { + LIBELF_SET_ERROR(ARGUMENT, 0); + return (0L); + } + + if (e->e_class != elfclass) { + LIBELF_SET_ERROR(CLASS, 0); + return (0L); + } + + if (gelf_getehdr(e, &eh) == NULL) + return (0); + + /* + * Iterate over all sections in the ELF file, computing the + * checksum along the way. + * + * The first section is always SHN_UNDEF and can be skipped. + * Non-allocatable sections are skipped, as are sections that + * could be affected by utilities such as strip(1). + */ + + checksum = 0; + for (shn = 1; shn < e->e_u.e_elf.e_nscn; shn++) { + if ((scn = elf_getscn(e, shn)) == NULL) + return (0); + if (gelf_getshdr(scn, &shdr) == NULL) + return (0); + if ((shdr.sh_flags & SHF_ALLOC) == 0 || + shdr.sh_type == SHT_DYNAMIC || + shdr.sh_type == SHT_DYNSYM) + continue; + + d = NULL; + while ((d = elf_rawdata(scn, d)) != NULL) + checksum = _libelf_sum(checksum, + (unsigned char *) d->d_buf, d->d_size); + } + + /* + * Return a 16-bit checksum compatible with Solaris. + */ + return (((checksum >> 16) & 0xFFFFUL) + (checksum & 0xFFFFUL)); +} diff --git a/rocclr/compiler/lib/loaders/elf/utils/libelf/libelf_convert.c b/rocclr/compiler/lib/loaders/elf/utils/libelf/libelf_convert.c new file mode 100644 index 0000000000..cea35bf965 --- /dev/null +++ b/rocclr/compiler/lib/loaders/elf/utils/libelf/libelf_convert.c @@ -0,0 +1,3112 @@ +/*- + * Copyright (c) 2006-2008 Joseph Koshy + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS `AS IS' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include + +#include +#include +#include + +#include "_libelf.h" + +LIBELF_VCSID("$Id: libelf_convert.m4 1048 2010-07-22 02:32:57Z jkoshy $"); + +/* WARNING: GENERATED FROM libelf_convert.m4. */ + +/* + * Macros to swap various integral quantities. + */ + +#define SWAP_HALF(X) do { \ + uint16_t _x = (uint16_t) (X); \ + uint16_t _t = _x & 0xFF; \ + _t <<= 8; _x >>= 8; _t |= _x & 0xFF; \ + (X) = _t; \ + } while (0) +#define SWAP_WORD(X) do { \ + uint32_t _x = (uint32_t) (X); \ + uint32_t _t = _x & 0xFF; \ + _t <<= 8; _x >>= 8; _t |= _x & 0xFF; \ + _t <<= 8; _x >>= 8; _t |= _x & 0xFF; \ + _t <<= 8; _x >>= 8; _t |= _x & 0xFF; \ + (X) = _t; \ + } while (0) +#define SWAP_ADDR32(X) SWAP_WORD(X) +#define SWAP_OFF32(X) SWAP_WORD(X) +#define SWAP_SWORD(X) SWAP_WORD(X) +#define SWAP_WORD64(X) do { \ + uint64_t _x = (uint64_t) (X); \ + uint64_t _t = _x & 0xFF; \ + _t <<= 8; _x >>= 8; _t |= _x & 0xFF; \ + _t <<= 8; _x >>= 8; _t |= _x & 0xFF; \ + _t <<= 8; _x >>= 8; _t |= _x & 0xFF; \ + _t <<= 8; _x >>= 8; _t |= _x & 0xFF; \ + _t <<= 8; _x >>= 8; _t |= _x & 0xFF; \ + _t <<= 8; _x >>= 8; _t |= _x & 0xFF; \ + _t <<= 8; _x >>= 8; _t |= _x & 0xFF; \ + (X) = _t; \ + } while (0) +#define SWAP_ADDR64(X) SWAP_WORD64(X) +#define SWAP_LWORD(X) SWAP_WORD64(X) +#define SWAP_OFF64(X) SWAP_WORD64(X) +#define SWAP_SXWORD(X) SWAP_WORD64(X) +#define SWAP_XWORD(X) SWAP_WORD64(X) + +/* + * Write out various integral values. The destination pointer could + * be unaligned. Values are written out in native byte order. The + * destination pointer is incremented after the write. + */ +#define WRITE_BYTE(P,X) do { \ + char *const _p = (char *) (P); \ + _p[0] = (char) (X); \ + (P) = _p + 1; \ + } while (0) +#define WRITE_HALF(P,X) do { \ + uint16_t _t = (X); \ + char *const _p = (char *) (P); \ + const char *const _q = (char *) &_t; \ + _p[0] = _q[0]; \ + _p[1] = _q[1]; \ + (P) = _p + 2; \ + } while (0) +#define WRITE_WORD(P,X) do { \ + uint32_t _t = (X); \ + char *const _p = (char *) (P); \ + const char *const _q = (char *) &_t; \ + _p[0] = _q[0]; \ + _p[1] = _q[1]; \ + _p[2] = _q[2]; \ + _p[3] = _q[3]; \ + (P) = _p + 4; \ + } while (0) +#define WRITE_ADDR32(P,X) WRITE_WORD(P,X) +#define WRITE_OFF32(P,X) WRITE_WORD(P,X) +#define WRITE_SWORD(P,X) WRITE_WORD(P,X) +#define WRITE_WORD64(P,X) do { \ + uint64_t _t = (X); \ + char *const _p = (char *) (P); \ + const char *const _q = (char *) &_t; \ + _p[0] = _q[0]; \ + _p[1] = _q[1]; \ + _p[2] = _q[2]; \ + _p[3] = _q[3]; \ + _p[4] = _q[4]; \ + _p[5] = _q[5]; \ + _p[6] = _q[6]; \ + _p[7] = _q[7]; \ + (P) = _p + 8; \ + } while (0) +#define WRITE_ADDR64(P,X) WRITE_WORD64(P,X) +#define WRITE_LWORD(P,X) WRITE_WORD64(P,X) +#define WRITE_OFF64(P,X) WRITE_WORD64(P,X) +#define WRITE_SXWORD(P,X) WRITE_WORD64(P,X) +#define WRITE_XWORD(P,X) WRITE_WORD64(P,X) +#define WRITE_IDENT(P,X) do { \ + (void) memcpy((P), (X), sizeof((X))); \ + (P) = (P) + EI_NIDENT; \ + } while (0) + +/* + * Read in various integral values. The source pointer could be + * unaligned. Values are read in native byte order. The source + * pointer is incremented appropriately. + */ + +#define READ_BYTE(P,X) do { \ + const char *const _p = \ + (const char *) (P); \ + (X) = _p[0]; \ + (P) = (P) + 1; \ + } while (0) +#define READ_HALF(P,X) do { \ + uint16_t _t; \ + char *const _q = (char *) &_t; \ + const char *const _p = \ + (const char *) (P); \ + _q[0] = _p[0]; \ + _q[1] = _p[1]; \ + (P) = (P) + 2; \ + (X) = _t; \ + } while (0) +#define READ_WORD(P,X) do { \ + uint32_t _t; \ + char *const _q = (char *) &_t; \ + const char *const _p = \ + (const char *) (P); \ + _q[0] = _p[0]; \ + _q[1] = _p[1]; \ + _q[2] = _p[2]; \ + _q[3] = _p[3]; \ + (P) = (P) + 4; \ + (X) = _t; \ + } while (0) +#define READ_ADDR32(P,X) READ_WORD(P,X) +#define READ_OFF32(P,X) READ_WORD(P,X) +#define READ_SWORD(P,X) READ_WORD(P,X) +#define READ_WORD64(P,X) do { \ + uint64_t _t; \ + char *const _q = (char *) &_t; \ + const char *const _p = \ + (const char *) (P); \ + _q[0] = _p[0]; \ + _q[1] = _p[1]; \ + _q[2] = _p[2]; \ + _q[3] = _p[3]; \ + _q[4] = _p[4]; \ + _q[5] = _p[5]; \ + _q[6] = _p[6]; \ + _q[7] = _p[7]; \ + (P) = (P) + 8; \ + (X) = _t; \ + } while (0) +#define READ_ADDR64(P,X) READ_WORD64(P,X) +#define READ_LWORD(P,X) READ_WORD64(P,X) +#define READ_OFF64(P,X) READ_WORD64(P,X) +#define READ_SXWORD(P,X) READ_WORD64(P,X) +#define READ_XWORD(P,X) READ_WORD64(P,X) +#define READ_IDENT(P,X) do { \ + (void) memcpy((X), (P), sizeof((X))); \ + (P) = (P) + EI_NIDENT; \ + } while (0) + +#define ROUNDUP2(V,N) (V) = ((((V) + (N) - 1)) & ~((N) - 1)) + + + +/* + * Sections of type ELF_T_BYTE are never byteswapped, consequently a + * simple memcpy suffices for both directions of conversion. + */ + +static int +libelf_cvt_BYTE_tox(char *dst, size_t dsz, char *src, size_t count, + int byteswap) +{ + (void) byteswap; + if (dsz < count) + return (0); + if (dst != src) + (void) memcpy(dst, src, count); + return (1); +} + +#if LIBELF_CONFIG_ADDR + +static int +libelf_cvt_ADDR32_tof(char *dst, size_t dsz, char *src, size_t count, + int byteswap) +{ + Elf32_Addr t, *s = (Elf32_Addr *) (uintptr_t) src; + size_t c; + + (void) dsz; + + if (!byteswap) { + (void) memcpy(dst, src, count * sizeof(*s)); + return (1); + } + + for (c = 0; c < count; c++) { + t = *s++; + SWAP_ADDR32(t); + WRITE_ADDR32(dst,t); + } + + return (1); +} + +static int +libelf_cvt_ADDR32_tom(char *dst, size_t dsz, char *src, size_t count, + int byteswap) +{ + Elf32_Addr t, *d = (Elf32_Addr *) (uintptr_t) dst; + size_t c; + + if (dsz < count * sizeof(Elf32_Addr)) + return (0); + + if (!byteswap) { + (void) memcpy(dst, src, count * sizeof(*d)); + return (1); + } + + for (c = 0; c < count; c++) { + READ_ADDR32(src,t); + SWAP_ADDR32(t); + *d++ = t; + } + + return (1); +} + +static int +libelf_cvt_ADDR64_tof(char *dst, size_t dsz, char *src, size_t count, + int byteswap) +{ + Elf64_Addr t, *s = (Elf64_Addr *) (uintptr_t) src; + size_t c; + + (void) dsz; + + if (!byteswap) { + (void) memcpy(dst, src, count * sizeof(*s)); + return (1); + } + + for (c = 0; c < count; c++) { + t = *s++; + SWAP_ADDR64(t); + WRITE_ADDR64(dst,t); + } + + return (1); +} + +static int +libelf_cvt_ADDR64_tom(char *dst, size_t dsz, char *src, size_t count, + int byteswap) +{ + Elf64_Addr t, *d = (Elf64_Addr *) (uintptr_t) dst; + size_t c; + + if (dsz < count * sizeof(Elf64_Addr)) + return (0); + + if (!byteswap) { + (void) memcpy(dst, src, count * sizeof(*d)); + return (1); + } + + for (c = 0; c < count; c++) { + READ_ADDR64(src,t); + SWAP_ADDR64(t); + *d++ = t; + } + + return (1); +} + +#endif /* LIBELF_CONFIG_ADDR */ +#if LIBELF_CONFIG_BYTE + +#endif /* LIBELF_CONFIG_BYTE */ +#if LIBELF_CONFIG_CAP + +static int +libelf_cvt32_CAP_tof(char *dst, size_t dsz, char *src, size_t count, + int byteswap) +{ + Elf32_Cap t, *s; + size_t c; + + (void) dsz; + + s = (Elf32_Cap *) (uintptr_t) src; + for (c = 0; c < count; c++) { + t = *s++; + if (byteswap) { + /* Swap an Elf32_Cap */ + SWAP_WORD(t.c_tag); + SWAP_WORD(t.c_un.c_val); + /**/ + } + /* Write an Elf32_Cap */ + WRITE_WORD(dst,t.c_tag); + WRITE_WORD(dst,t.c_un.c_val); + /**/ + } + + return (1); +} + +static int +libelf_cvt64_CAP_tof(char *dst, size_t dsz, char *src, size_t count, + int byteswap) +{ + Elf64_Cap t, *s; + size_t c; + + (void) dsz; + + s = (Elf64_Cap *) (uintptr_t) src; + for (c = 0; c < count; c++) { + t = *s++; + if (byteswap) { + /* Swap an Elf64_Cap */ + SWAP_XWORD(t.c_tag); + SWAP_XWORD(t.c_un.c_val); + /**/ + } + /* Write an Elf64_Cap */ + WRITE_XWORD(dst,t.c_tag); + WRITE_XWORD(dst,t.c_un.c_val); + /**/ + } + + return (1); +} + +static int +libelf_cvt32_CAP_tom(char *dst, size_t dsz, char *src, size_t count, + int byteswap) +{ + Elf32_Cap t, *d; + char *s,*s0; + size_t fsz; + + fsz = elf32_fsize(ELF_T_CAP, (size_t) 1, EV_CURRENT); + d = ((Elf32_Cap *) (uintptr_t) dst) + (count - 1); + s0 = (char *) src + (count - 1) * fsz; + + if (dsz < count * sizeof(Elf32_Cap)) + return (0); + + while (count--) { + s = s0; + /* Read an Elf32_Cap */ + READ_WORD(s,t.c_tag); + READ_WORD(s,t.c_un.c_val); + /**/ + if (byteswap) { + /* Swap an Elf32_Cap */ + SWAP_WORD(t.c_tag); + SWAP_WORD(t.c_un.c_val); + /**/ + } + *d-- = t; s0 -= fsz; + } + + return (1); +} + +static int +libelf_cvt64_CAP_tom(char *dst, size_t dsz, char *src, size_t count, + int byteswap) +{ + Elf64_Cap t, *d; + char *s,*s0; + size_t fsz; + + fsz = elf64_fsize(ELF_T_CAP, (size_t) 1, EV_CURRENT); + d = ((Elf64_Cap *) (uintptr_t) dst) + (count - 1); + s0 = (char *) src + (count - 1) * fsz; + + if (dsz < count * sizeof(Elf64_Cap)) + return (0); + + while (count--) { + s = s0; + /* Read an Elf64_Cap */ + READ_XWORD(s,t.c_tag); + READ_XWORD(s,t.c_un.c_val); + /**/ + if (byteswap) { + /* Swap an Elf64_Cap */ + SWAP_XWORD(t.c_tag); + SWAP_XWORD(t.c_un.c_val); + /**/ + } + *d-- = t; s0 -= fsz; + } + + return (1); +} + +#endif /* LIBELF_CONFIG_CAP */ +#if LIBELF_CONFIG_DYN + +static int +libelf_cvt32_DYN_tof(char *dst, size_t dsz, char *src, size_t count, + int byteswap) +{ + Elf32_Dyn t, *s; + size_t c; + + (void) dsz; + + s = (Elf32_Dyn *) (uintptr_t) src; + for (c = 0; c < count; c++) { + t = *s++; + if (byteswap) { + /* Swap an Elf32_Dyn */ + SWAP_SWORD(t.d_tag); + SWAP_WORD(t.d_un.d_ptr); + /**/ + } + /* Write an Elf32_Dyn */ + WRITE_SWORD(dst,t.d_tag); + WRITE_WORD(dst,t.d_un.d_ptr); + /**/ + } + + return (1); +} + +static int +libelf_cvt64_DYN_tof(char *dst, size_t dsz, char *src, size_t count, + int byteswap) +{ + Elf64_Dyn t, *s; + size_t c; + + (void) dsz; + + s = (Elf64_Dyn *) (uintptr_t) src; + for (c = 0; c < count; c++) { + t = *s++; + if (byteswap) { + /* Swap an Elf64_Dyn */ + SWAP_SXWORD(t.d_tag); + SWAP_XWORD(t.d_un.d_ptr); + /**/ + } + /* Write an Elf64_Dyn */ + WRITE_SXWORD(dst,t.d_tag); + WRITE_XWORD(dst,t.d_un.d_ptr); + /**/ + } + + return (1); +} + +static int +libelf_cvt32_DYN_tom(char *dst, size_t dsz, char *src, size_t count, + int byteswap) +{ + Elf32_Dyn t, *d; + char *s,*s0; + size_t fsz; + + fsz = elf32_fsize(ELF_T_DYN, (size_t) 1, EV_CURRENT); + d = ((Elf32_Dyn *) (uintptr_t) dst) + (count - 1); + s0 = (char *) src + (count - 1) * fsz; + + if (dsz < count * sizeof(Elf32_Dyn)) + return (0); + + while (count--) { + s = s0; + /* Read an Elf32_Dyn */ + READ_SWORD(s,t.d_tag); + READ_WORD(s,t.d_un.d_ptr); + /**/ + if (byteswap) { + /* Swap an Elf32_Dyn */ + SWAP_SWORD(t.d_tag); + SWAP_WORD(t.d_un.d_ptr); + /**/ + } + *d-- = t; s0 -= fsz; + } + + return (1); +} + +static int +libelf_cvt64_DYN_tom(char *dst, size_t dsz, char *src, size_t count, + int byteswap) +{ + Elf64_Dyn t, *d; + char *s,*s0; + size_t fsz; + + fsz = elf64_fsize(ELF_T_DYN, (size_t) 1, EV_CURRENT); + d = ((Elf64_Dyn *) (uintptr_t) dst) + (count - 1); + s0 = (char *) src + (count - 1) * fsz; + + if (dsz < count * sizeof(Elf64_Dyn)) + return (0); + + while (count--) { + s = s0; + /* Read an Elf64_Dyn */ + READ_SXWORD(s,t.d_tag); + READ_XWORD(s,t.d_un.d_ptr); + /**/ + if (byteswap) { + /* Swap an Elf64_Dyn */ + SWAP_SXWORD(t.d_tag); + SWAP_XWORD(t.d_un.d_ptr); + /**/ + } + *d-- = t; s0 -= fsz; + } + + return (1); +} + +#endif /* LIBELF_CONFIG_DYN */ +#if LIBELF_CONFIG_EHDR + +static int +libelf_cvt32_EHDR_tof(char *dst, size_t dsz, char *src, size_t count, + int byteswap) +{ + Elf32_Ehdr t, *s; + size_t c; + + (void) dsz; + + s = (Elf32_Ehdr *) (uintptr_t) src; + for (c = 0; c < count; c++) { + t = *s++; + if (byteswap) { + /* Swap an Elf32_Ehdr */ + SWAP_HALF(t.e_type); + SWAP_HALF(t.e_machine); + SWAP_WORD(t.e_version); + SWAP_ADDR32(t.e_entry); + SWAP_OFF32(t.e_phoff); + SWAP_OFF32(t.e_shoff); + SWAP_WORD(t.e_flags); + SWAP_HALF(t.e_ehsize); + SWAP_HALF(t.e_phentsize); + SWAP_HALF(t.e_phnum); + SWAP_HALF(t.e_shentsize); + SWAP_HALF(t.e_shnum); + SWAP_HALF(t.e_shstrndx); + /**/ + } + /* Write an Elf32_Ehdr */ + WRITE_IDENT(dst,t.e_ident); + WRITE_HALF(dst,t.e_type); + WRITE_HALF(dst,t.e_machine); + WRITE_WORD(dst,t.e_version); + WRITE_ADDR32(dst,t.e_entry); + WRITE_OFF32(dst,t.e_phoff); + WRITE_OFF32(dst,t.e_shoff); + WRITE_WORD(dst,t.e_flags); + WRITE_HALF(dst,t.e_ehsize); + WRITE_HALF(dst,t.e_phentsize); + WRITE_HALF(dst,t.e_phnum); + WRITE_HALF(dst,t.e_shentsize); + WRITE_HALF(dst,t.e_shnum); + WRITE_HALF(dst,t.e_shstrndx); + /**/ + } + + return (1); +} + +static int +libelf_cvt64_EHDR_tof(char *dst, size_t dsz, char *src, size_t count, + int byteswap) +{ + Elf64_Ehdr t, *s; + size_t c; + + (void) dsz; + + s = (Elf64_Ehdr *) (uintptr_t) src; + for (c = 0; c < count; c++) { + t = *s++; + if (byteswap) { + /* Swap an Elf64_Ehdr */ + SWAP_HALF(t.e_type); + SWAP_HALF(t.e_machine); + SWAP_WORD(t.e_version); + SWAP_ADDR64(t.e_entry); + SWAP_OFF64(t.e_phoff); + SWAP_OFF64(t.e_shoff); + SWAP_WORD(t.e_flags); + SWAP_HALF(t.e_ehsize); + SWAP_HALF(t.e_phentsize); + SWAP_HALF(t.e_phnum); + SWAP_HALF(t.e_shentsize); + SWAP_HALF(t.e_shnum); + SWAP_HALF(t.e_shstrndx); + /**/ + } + /* Write an Elf64_Ehdr */ + WRITE_IDENT(dst,t.e_ident); + WRITE_HALF(dst,t.e_type); + WRITE_HALF(dst,t.e_machine); + WRITE_WORD(dst,t.e_version); + WRITE_ADDR64(dst,t.e_entry); + WRITE_OFF64(dst,t.e_phoff); + WRITE_OFF64(dst,t.e_shoff); + WRITE_WORD(dst,t.e_flags); + WRITE_HALF(dst,t.e_ehsize); + WRITE_HALF(dst,t.e_phentsize); + WRITE_HALF(dst,t.e_phnum); + WRITE_HALF(dst,t.e_shentsize); + WRITE_HALF(dst,t.e_shnum); + WRITE_HALF(dst,t.e_shstrndx); + /**/ + } + + return (1); +} + +static int +libelf_cvt32_EHDR_tom(char *dst, size_t dsz, char *src, size_t count, + int byteswap) +{ + Elf32_Ehdr t, *d; + char *s,*s0; + size_t fsz; + + fsz = elf32_fsize(ELF_T_EHDR, (size_t) 1, EV_CURRENT); + d = ((Elf32_Ehdr *) (uintptr_t) dst) + (count - 1); + s0 = (char *) src + (count - 1) * fsz; + + if (dsz < count * sizeof(Elf32_Ehdr)) + return (0); + + while (count--) { + s = s0; + /* Read an Elf32_Ehdr */ + READ_IDENT(s,t.e_ident); + READ_HALF(s,t.e_type); + READ_HALF(s,t.e_machine); + READ_WORD(s,t.e_version); + READ_ADDR32(s,t.e_entry); + READ_OFF32(s,t.e_phoff); + READ_OFF32(s,t.e_shoff); + READ_WORD(s,t.e_flags); + READ_HALF(s,t.e_ehsize); + READ_HALF(s,t.e_phentsize); + READ_HALF(s,t.e_phnum); + READ_HALF(s,t.e_shentsize); + READ_HALF(s,t.e_shnum); + READ_HALF(s,t.e_shstrndx); + /**/ + if (byteswap) { + /* Swap an Elf32_Ehdr */ + SWAP_HALF(t.e_type); + SWAP_HALF(t.e_machine); + SWAP_WORD(t.e_version); + SWAP_ADDR32(t.e_entry); + SWAP_OFF32(t.e_phoff); + SWAP_OFF32(t.e_shoff); + SWAP_WORD(t.e_flags); + SWAP_HALF(t.e_ehsize); + SWAP_HALF(t.e_phentsize); + SWAP_HALF(t.e_phnum); + SWAP_HALF(t.e_shentsize); + SWAP_HALF(t.e_shnum); + SWAP_HALF(t.e_shstrndx); + /**/ + } + *d-- = t; s0 -= fsz; + } + + return (1); +} + +static int +libelf_cvt64_EHDR_tom(char *dst, size_t dsz, char *src, size_t count, + int byteswap) +{ + Elf64_Ehdr t, *d; + char *s,*s0; + size_t fsz; + + fsz = elf64_fsize(ELF_T_EHDR, (size_t) 1, EV_CURRENT); + d = ((Elf64_Ehdr *) (uintptr_t) dst) + (count - 1); + s0 = (char *) src + (count - 1) * fsz; + + if (dsz < count * sizeof(Elf64_Ehdr)) + return (0); + + while (count--) { + s = s0; + /* Read an Elf64_Ehdr */ + READ_IDENT(s,t.e_ident); + READ_HALF(s,t.e_type); + READ_HALF(s,t.e_machine); + READ_WORD(s,t.e_version); + READ_ADDR64(s,t.e_entry); + READ_OFF64(s,t.e_phoff); + READ_OFF64(s,t.e_shoff); + READ_WORD(s,t.e_flags); + READ_HALF(s,t.e_ehsize); + READ_HALF(s,t.e_phentsize); + READ_HALF(s,t.e_phnum); + READ_HALF(s,t.e_shentsize); + READ_HALF(s,t.e_shnum); + READ_HALF(s,t.e_shstrndx); + /**/ + if (byteswap) { + /* Swap an Elf64_Ehdr */ + SWAP_HALF(t.e_type); + SWAP_HALF(t.e_machine); + SWAP_WORD(t.e_version); + SWAP_ADDR64(t.e_entry); + SWAP_OFF64(t.e_phoff); + SWAP_OFF64(t.e_shoff); + SWAP_WORD(t.e_flags); + SWAP_HALF(t.e_ehsize); + SWAP_HALF(t.e_phentsize); + SWAP_HALF(t.e_phnum); + SWAP_HALF(t.e_shentsize); + SWAP_HALF(t.e_shnum); + SWAP_HALF(t.e_shstrndx); + /**/ + } + *d-- = t; s0 -= fsz; + } + + return (1); +} + +#endif /* LIBELF_CONFIG_EHDR */ +#if LIBELF_CONFIG_GNUHASH + +#endif /* LIBELF_CONFIG_GNUHASH */ +#if LIBELF_CONFIG_HALF + +static int +libelf_cvt_HALF_tof(char *dst, size_t dsz, char *src, size_t count, + int byteswap) +{ + Elf64_Half t, *s = (Elf64_Half *) (uintptr_t) src; + size_t c; + + (void) dsz; + + if (!byteswap) { + (void) memcpy(dst, src, count * sizeof(*s)); + return (1); + } + + for (c = 0; c < count; c++) { + t = *s++; + SWAP_HALF(t); + WRITE_HALF(dst,t); + } + + return (1); +} + + +static int +libelf_cvt_HALF_tom(char *dst, size_t dsz, char *src, size_t count, + int byteswap) +{ + Elf64_Half t, *d = (Elf64_Half *) (uintptr_t) dst; + size_t c; + + if (dsz < count * sizeof(Elf64_Half)) + return (0); + + if (!byteswap) { + (void) memcpy(dst, src, count * sizeof(*d)); + return (1); + } + + for (c = 0; c < count; c++) { + READ_HALF(src,t); + SWAP_HALF(t); + *d++ = t; + } + + return (1); +} + +#endif /* LIBELF_CONFIG_HALF */ +#if LIBELF_CONFIG_LWORD + +static int +libelf_cvt_LWORD_tof(char *dst, size_t dsz, char *src, size_t count, + int byteswap) +{ + Elf64_Lword t, *s = (Elf64_Lword *) (uintptr_t) src; + size_t c; + + (void) dsz; + + if (!byteswap) { + (void) memcpy(dst, src, count * sizeof(*s)); + return (1); + } + + for (c = 0; c < count; c++) { + t = *s++; + SWAP_LWORD(t); + WRITE_LWORD(dst,t); + } + + return (1); +} + + +static int +libelf_cvt_LWORD_tom(char *dst, size_t dsz, char *src, size_t count, + int byteswap) +{ + Elf64_Lword t, *d = (Elf64_Lword *) (uintptr_t) dst; + size_t c; + + if (dsz < count * sizeof(Elf64_Lword)) + return (0); + + if (!byteswap) { + (void) memcpy(dst, src, count * sizeof(*d)); + return (1); + } + + for (c = 0; c < count; c++) { + READ_LWORD(src,t); + SWAP_LWORD(t); + *d++ = t; + } + + return (1); +} + +#endif /* LIBELF_CONFIG_LWORD */ +#if LIBELF_CONFIG_MOVE + +static int +libelf_cvt32_MOVE_tof(char *dst, size_t dsz, char *src, size_t count, + int byteswap) +{ + Elf32_Move t, *s; + size_t c; + + (void) dsz; + + s = (Elf32_Move *) (uintptr_t) src; + for (c = 0; c < count; c++) { + t = *s++; + if (byteswap) { + /* Swap an Elf32_Move */ + SWAP_LWORD(t.m_value); + SWAP_WORD(t.m_info); + SWAP_WORD(t.m_poffset); + SWAP_HALF(t.m_repeat); + SWAP_HALF(t.m_stride); + /**/ + } + /* Write an Elf32_Move */ + WRITE_LWORD(dst,t.m_value); + WRITE_WORD(dst,t.m_info); + WRITE_WORD(dst,t.m_poffset); + WRITE_HALF(dst,t.m_repeat); + WRITE_HALF(dst,t.m_stride); + /**/ + } + + return (1); +} + +static int +libelf_cvt64_MOVE_tof(char *dst, size_t dsz, char *src, size_t count, + int byteswap) +{ + Elf64_Move t, *s; + size_t c; + + (void) dsz; + + s = (Elf64_Move *) (uintptr_t) src; + for (c = 0; c < count; c++) { + t = *s++; + if (byteswap) { + /* Swap an Elf64_Move */ + SWAP_LWORD(t.m_value); + SWAP_XWORD(t.m_info); + SWAP_XWORD(t.m_poffset); + SWAP_HALF(t.m_repeat); + SWAP_HALF(t.m_stride); + /**/ + } + /* Write an Elf64_Move */ + WRITE_LWORD(dst,t.m_value); + WRITE_XWORD(dst,t.m_info); + WRITE_XWORD(dst,t.m_poffset); + WRITE_HALF(dst,t.m_repeat); + WRITE_HALF(dst,t.m_stride); + /**/ + } + + return (1); +} + +static int +libelf_cvt32_MOVE_tom(char *dst, size_t dsz, char *src, size_t count, + int byteswap) +{ + Elf32_Move t, *d; + char *s,*s0; + size_t fsz; + + fsz = elf32_fsize(ELF_T_MOVE, (size_t) 1, EV_CURRENT); + d = ((Elf32_Move *) (uintptr_t) dst) + (count - 1); + s0 = (char *) src + (count - 1) * fsz; + + if (dsz < count * sizeof(Elf32_Move)) + return (0); + + while (count--) { + s = s0; + /* Read an Elf32_Move */ + READ_LWORD(s,t.m_value); + READ_WORD(s,t.m_info); + READ_WORD(s,t.m_poffset); + READ_HALF(s,t.m_repeat); + READ_HALF(s,t.m_stride); + /**/ + if (byteswap) { + /* Swap an Elf32_Move */ + SWAP_LWORD(t.m_value); + SWAP_WORD(t.m_info); + SWAP_WORD(t.m_poffset); + SWAP_HALF(t.m_repeat); + SWAP_HALF(t.m_stride); + /**/ + } + *d-- = t; s0 -= fsz; + } + + return (1); +} + +static int +libelf_cvt64_MOVE_tom(char *dst, size_t dsz, char *src, size_t count, + int byteswap) +{ + Elf64_Move t, *d; + char *s,*s0; + size_t fsz; + + fsz = elf64_fsize(ELF_T_MOVE, (size_t) 1, EV_CURRENT); + d = ((Elf64_Move *) (uintptr_t) dst) + (count - 1); + s0 = (char *) src + (count - 1) * fsz; + + if (dsz < count * sizeof(Elf64_Move)) + return (0); + + while (count--) { + s = s0; + /* Read an Elf64_Move */ + READ_LWORD(s,t.m_value); + READ_XWORD(s,t.m_info); + READ_XWORD(s,t.m_poffset); + READ_HALF(s,t.m_repeat); + READ_HALF(s,t.m_stride); + /**/ + if (byteswap) { + /* Swap an Elf64_Move */ + SWAP_LWORD(t.m_value); + SWAP_XWORD(t.m_info); + SWAP_XWORD(t.m_poffset); + SWAP_HALF(t.m_repeat); + SWAP_HALF(t.m_stride); + /**/ + } + *d-- = t; s0 -= fsz; + } + + return (1); +} + +#endif /* LIBELF_CONFIG_MOVE */ +#if LIBELF_CONFIG_MOVEP + +#endif /* LIBELF_CONFIG_MOVEP */ +#if LIBELF_CONFIG_NOTE + +#endif /* LIBELF_CONFIG_NOTE */ +#if LIBELF_CONFIG_OFF + +static int +libelf_cvt_OFF32_tof(char *dst, size_t dsz, char *src, size_t count, + int byteswap) +{ + Elf32_Off t, *s = (Elf32_Off *) (uintptr_t) src; + size_t c; + + (void) dsz; + + if (!byteswap) { + (void) memcpy(dst, src, count * sizeof(*s)); + return (1); + } + + for (c = 0; c < count; c++) { + t = *s++; + SWAP_OFF32(t); + WRITE_OFF32(dst,t); + } + + return (1); +} + +static int +libelf_cvt_OFF32_tom(char *dst, size_t dsz, char *src, size_t count, + int byteswap) +{ + Elf32_Off t, *d = (Elf32_Off *) (uintptr_t) dst; + size_t c; + + if (dsz < count * sizeof(Elf32_Off)) + return (0); + + if (!byteswap) { + (void) memcpy(dst, src, count * sizeof(*d)); + return (1); + } + + for (c = 0; c < count; c++) { + READ_OFF32(src,t); + SWAP_OFF32(t); + *d++ = t; + } + + return (1); +} + +static int +libelf_cvt_OFF64_tof(char *dst, size_t dsz, char *src, size_t count, + int byteswap) +{ + Elf64_Off t, *s = (Elf64_Off *) (uintptr_t) src; + size_t c; + + (void) dsz; + + if (!byteswap) { + (void) memcpy(dst, src, count * sizeof(*s)); + return (1); + } + + for (c = 0; c < count; c++) { + t = *s++; + SWAP_OFF64(t); + WRITE_OFF64(dst,t); + } + + return (1); +} + +static int +libelf_cvt_OFF64_tom(char *dst, size_t dsz, char *src, size_t count, + int byteswap) +{ + Elf64_Off t, *d = (Elf64_Off *) (uintptr_t) dst; + size_t c; + + if (dsz < count * sizeof(Elf64_Off)) + return (0); + + if (!byteswap) { + (void) memcpy(dst, src, count * sizeof(*d)); + return (1); + } + + for (c = 0; c < count; c++) { + READ_OFF64(src,t); + SWAP_OFF64(t); + *d++ = t; + } + + return (1); +} + +#endif /* LIBELF_CONFIG_OFF */ +#if LIBELF_CONFIG_PHDR + +static int +libelf_cvt32_PHDR_tof(char *dst, size_t dsz, char *src, size_t count, + int byteswap) +{ + Elf32_Phdr t, *s; + size_t c; + + (void) dsz; + + s = (Elf32_Phdr *) (uintptr_t) src; + for (c = 0; c < count; c++) { + t = *s++; + if (byteswap) { + /* Swap an Elf32_Phdr */ + SWAP_WORD(t.p_type); + SWAP_OFF32(t.p_offset); + SWAP_ADDR32(t.p_vaddr); + SWAP_ADDR32(t.p_paddr); + SWAP_WORD(t.p_filesz); + SWAP_WORD(t.p_memsz); + SWAP_WORD(t.p_flags); + SWAP_WORD(t.p_align); + /**/ + } + /* Write an Elf32_Phdr */ + WRITE_WORD(dst,t.p_type); + WRITE_OFF32(dst,t.p_offset); + WRITE_ADDR32(dst,t.p_vaddr); + WRITE_ADDR32(dst,t.p_paddr); + WRITE_WORD(dst,t.p_filesz); + WRITE_WORD(dst,t.p_memsz); + WRITE_WORD(dst,t.p_flags); + WRITE_WORD(dst,t.p_align); + /**/ + } + + return (1); +} + +static int +libelf_cvt64_PHDR_tof(char *dst, size_t dsz, char *src, size_t count, + int byteswap) +{ + Elf64_Phdr t, *s; + size_t c; + + (void) dsz; + + s = (Elf64_Phdr *) (uintptr_t) src; + for (c = 0; c < count; c++) { + t = *s++; + if (byteswap) { + /* Swap an Elf64_Phdr */ + SWAP_WORD(t.p_type); + SWAP_WORD(t.p_flags); + SWAP_OFF64(t.p_offset); + SWAP_ADDR64(t.p_vaddr); + SWAP_ADDR64(t.p_paddr); + SWAP_XWORD(t.p_filesz); + SWAP_XWORD(t.p_memsz); + SWAP_XWORD(t.p_align); + /**/ + } + /* Write an Elf64_Phdr */ + WRITE_WORD(dst,t.p_type); + WRITE_WORD(dst,t.p_flags); + WRITE_OFF64(dst,t.p_offset); + WRITE_ADDR64(dst,t.p_vaddr); + WRITE_ADDR64(dst,t.p_paddr); + WRITE_XWORD(dst,t.p_filesz); + WRITE_XWORD(dst,t.p_memsz); + WRITE_XWORD(dst,t.p_align); + /**/ + } + + return (1); +} + +static int +libelf_cvt32_PHDR_tom(char *dst, size_t dsz, char *src, size_t count, + int byteswap) +{ + Elf32_Phdr t, *d; + char *s,*s0; + size_t fsz; + + fsz = elf32_fsize(ELF_T_PHDR, (size_t) 1, EV_CURRENT); + d = ((Elf32_Phdr *) (uintptr_t) dst) + (count - 1); + s0 = (char *) src + (count - 1) * fsz; + + if (dsz < count * sizeof(Elf32_Phdr)) + return (0); + + while (count--) { + s = s0; + /* Read an Elf32_Phdr */ + READ_WORD(s,t.p_type); + READ_OFF32(s,t.p_offset); + READ_ADDR32(s,t.p_vaddr); + READ_ADDR32(s,t.p_paddr); + READ_WORD(s,t.p_filesz); + READ_WORD(s,t.p_memsz); + READ_WORD(s,t.p_flags); + READ_WORD(s,t.p_align); + /**/ + if (byteswap) { + /* Swap an Elf32_Phdr */ + SWAP_WORD(t.p_type); + SWAP_OFF32(t.p_offset); + SWAP_ADDR32(t.p_vaddr); + SWAP_ADDR32(t.p_paddr); + SWAP_WORD(t.p_filesz); + SWAP_WORD(t.p_memsz); + SWAP_WORD(t.p_flags); + SWAP_WORD(t.p_align); + /**/ + } + *d-- = t; s0 -= fsz; + } + + return (1); +} + +static int +libelf_cvt64_PHDR_tom(char *dst, size_t dsz, char *src, size_t count, + int byteswap) +{ + Elf64_Phdr t, *d; + char *s,*s0; + size_t fsz; + + fsz = elf64_fsize(ELF_T_PHDR, (size_t) 1, EV_CURRENT); + d = ((Elf64_Phdr *) (uintptr_t) dst) + (count - 1); + s0 = (char *) src + (count - 1) * fsz; + + if (dsz < count * sizeof(Elf64_Phdr)) + return (0); + + while (count--) { + s = s0; + /* Read an Elf64_Phdr */ + READ_WORD(s,t.p_type); + READ_WORD(s,t.p_flags); + READ_OFF64(s,t.p_offset); + READ_ADDR64(s,t.p_vaddr); + READ_ADDR64(s,t.p_paddr); + READ_XWORD(s,t.p_filesz); + READ_XWORD(s,t.p_memsz); + READ_XWORD(s,t.p_align); + /**/ + if (byteswap) { + /* Swap an Elf64_Phdr */ + SWAP_WORD(t.p_type); + SWAP_WORD(t.p_flags); + SWAP_OFF64(t.p_offset); + SWAP_ADDR64(t.p_vaddr); + SWAP_ADDR64(t.p_paddr); + SWAP_XWORD(t.p_filesz); + SWAP_XWORD(t.p_memsz); + SWAP_XWORD(t.p_align); + /**/ + } + *d-- = t; s0 -= fsz; + } + + return (1); +} + +#endif /* LIBELF_CONFIG_PHDR */ +#if LIBELF_CONFIG_REL + +static int +libelf_cvt32_REL_tof(char *dst, size_t dsz, char *src, size_t count, + int byteswap) +{ + Elf32_Rel t, *s; + size_t c; + + (void) dsz; + + s = (Elf32_Rel *) (uintptr_t) src; + for (c = 0; c < count; c++) { + t = *s++; + if (byteswap) { + /* Swap an Elf32_Rel */ + SWAP_ADDR32(t.r_offset); + SWAP_WORD(t.r_info); + /**/ + } + /* Write an Elf32_Rel */ + WRITE_ADDR32(dst,t.r_offset); + WRITE_WORD(dst,t.r_info); + /**/ + } + + return (1); +} + +static int +libelf_cvt64_REL_tof(char *dst, size_t dsz, char *src, size_t count, + int byteswap) +{ + Elf64_Rel t, *s; + size_t c; + + (void) dsz; + + s = (Elf64_Rel *) (uintptr_t) src; + for (c = 0; c < count; c++) { + t = *s++; + if (byteswap) { + /* Swap an Elf64_Rel */ + SWAP_ADDR64(t.r_offset); + SWAP_XWORD(t.r_info); + /**/ + } + /* Write an Elf64_Rel */ + WRITE_ADDR64(dst,t.r_offset); + WRITE_XWORD(dst,t.r_info); + /**/ + } + + return (1); +} + +static int +libelf_cvt32_REL_tom(char *dst, size_t dsz, char *src, size_t count, + int byteswap) +{ + Elf32_Rel t, *d; + char *s,*s0; + size_t fsz; + + fsz = elf32_fsize(ELF_T_REL, (size_t) 1, EV_CURRENT); + d = ((Elf32_Rel *) (uintptr_t) dst) + (count - 1); + s0 = (char *) src + (count - 1) * fsz; + + if (dsz < count * sizeof(Elf32_Rel)) + return (0); + + while (count--) { + s = s0; + /* Read an Elf32_Rel */ + READ_ADDR32(s,t.r_offset); + READ_WORD(s,t.r_info); + /**/ + if (byteswap) { + /* Swap an Elf32_Rel */ + SWAP_ADDR32(t.r_offset); + SWAP_WORD(t.r_info); + /**/ + } + *d-- = t; s0 -= fsz; + } + + return (1); +} + +static int +libelf_cvt64_REL_tom(char *dst, size_t dsz, char *src, size_t count, + int byteswap) +{ + Elf64_Rel t, *d; + char *s,*s0; + size_t fsz; + + fsz = elf64_fsize(ELF_T_REL, (size_t) 1, EV_CURRENT); + d = ((Elf64_Rel *) (uintptr_t) dst) + (count - 1); + s0 = (char *) src + (count - 1) * fsz; + + if (dsz < count * sizeof(Elf64_Rel)) + return (0); + + while (count--) { + s = s0; + /* Read an Elf64_Rel */ + READ_ADDR64(s,t.r_offset); + READ_XWORD(s,t.r_info); + /**/ + if (byteswap) { + /* Swap an Elf64_Rel */ + SWAP_ADDR64(t.r_offset); + SWAP_XWORD(t.r_info); + /**/ + } + *d-- = t; s0 -= fsz; + } + + return (1); +} + +#endif /* LIBELF_CONFIG_REL */ +#if LIBELF_CONFIG_RELA + +static int +libelf_cvt32_RELA_tof(char *dst, size_t dsz, char *src, size_t count, + int byteswap) +{ + Elf32_Rela t, *s; + size_t c; + + (void) dsz; + + s = (Elf32_Rela *) (uintptr_t) src; + for (c = 0; c < count; c++) { + t = *s++; + if (byteswap) { + /* Swap an Elf32_Rela */ + SWAP_ADDR32(t.r_offset); + SWAP_WORD(t.r_info); + SWAP_SWORD(t.r_addend); + /**/ + } + /* Write an Elf32_Rela */ + WRITE_ADDR32(dst,t.r_offset); + WRITE_WORD(dst,t.r_info); + WRITE_SWORD(dst,t.r_addend); + /**/ + } + + return (1); +} + +static int +libelf_cvt64_RELA_tof(char *dst, size_t dsz, char *src, size_t count, + int byteswap) +{ + Elf64_Rela t, *s; + size_t c; + + (void) dsz; + + s = (Elf64_Rela *) (uintptr_t) src; + for (c = 0; c < count; c++) { + t = *s++; + if (byteswap) { + /* Swap an Elf64_Rela */ + SWAP_ADDR64(t.r_offset); + SWAP_XWORD(t.r_info); + SWAP_SXWORD(t.r_addend); + /**/ + } + /* Write an Elf64_Rela */ + WRITE_ADDR64(dst,t.r_offset); + WRITE_XWORD(dst,t.r_info); + WRITE_SXWORD(dst,t.r_addend); + /**/ + } + + return (1); +} + +static int +libelf_cvt32_RELA_tom(char *dst, size_t dsz, char *src, size_t count, + int byteswap) +{ + Elf32_Rela t, *d; + char *s,*s0; + size_t fsz; + + fsz = elf32_fsize(ELF_T_RELA, (size_t) 1, EV_CURRENT); + d = ((Elf32_Rela *) (uintptr_t) dst) + (count - 1); + s0 = (char *) src + (count - 1) * fsz; + + if (dsz < count * sizeof(Elf32_Rela)) + return (0); + + while (count--) { + s = s0; + /* Read an Elf32_Rela */ + READ_ADDR32(s,t.r_offset); + READ_WORD(s,t.r_info); + READ_SWORD(s,t.r_addend); + /**/ + if (byteswap) { + /* Swap an Elf32_Rela */ + SWAP_ADDR32(t.r_offset); + SWAP_WORD(t.r_info); + SWAP_SWORD(t.r_addend); + /**/ + } + *d-- = t; s0 -= fsz; + } + + return (1); +} + +static int +libelf_cvt64_RELA_tom(char *dst, size_t dsz, char *src, size_t count, + int byteswap) +{ + Elf64_Rela t, *d; + char *s,*s0; + size_t fsz; + + fsz = elf64_fsize(ELF_T_RELA, (size_t) 1, EV_CURRENT); + d = ((Elf64_Rela *) (uintptr_t) dst) + (count - 1); + s0 = (char *) src + (count - 1) * fsz; + + if (dsz < count * sizeof(Elf64_Rela)) + return (0); + + while (count--) { + s = s0; + /* Read an Elf64_Rela */ + READ_ADDR64(s,t.r_offset); + READ_XWORD(s,t.r_info); + READ_SXWORD(s,t.r_addend); + /**/ + if (byteswap) { + /* Swap an Elf64_Rela */ + SWAP_ADDR64(t.r_offset); + SWAP_XWORD(t.r_info); + SWAP_SXWORD(t.r_addend); + /**/ + } + *d-- = t; s0 -= fsz; + } + + return (1); +} + +#endif /* LIBELF_CONFIG_RELA */ +#if LIBELF_CONFIG_SHDR + +static int +libelf_cvt32_SHDR_tof(char *dst, size_t dsz, char *src, size_t count, + int byteswap) +{ + Elf32_Shdr t, *s; + size_t c; + + (void) dsz; + + s = (Elf32_Shdr *) (uintptr_t) src; + for (c = 0; c < count; c++) { + t = *s++; + if (byteswap) { + /* Swap an Elf32_Shdr */ + SWAP_WORD(t.sh_name); + SWAP_WORD(t.sh_type); + SWAP_WORD(t.sh_flags); + SWAP_ADDR32(t.sh_addr); + SWAP_OFF32(t.sh_offset); + SWAP_WORD(t.sh_size); + SWAP_WORD(t.sh_link); + SWAP_WORD(t.sh_info); + SWAP_WORD(t.sh_addralign); + SWAP_WORD(t.sh_entsize); + /**/ + } + /* Write an Elf32_Shdr */ + WRITE_WORD(dst,t.sh_name); + WRITE_WORD(dst,t.sh_type); + WRITE_WORD(dst,t.sh_flags); + WRITE_ADDR32(dst,t.sh_addr); + WRITE_OFF32(dst,t.sh_offset); + WRITE_WORD(dst,t.sh_size); + WRITE_WORD(dst,t.sh_link); + WRITE_WORD(dst,t.sh_info); + WRITE_WORD(dst,t.sh_addralign); + WRITE_WORD(dst,t.sh_entsize); + /**/ + } + + return (1); +} + +static int +libelf_cvt64_SHDR_tof(char *dst, size_t dsz, char *src, size_t count, + int byteswap) +{ + Elf64_Shdr t, *s; + size_t c; + + (void) dsz; + + s = (Elf64_Shdr *) (uintptr_t) src; + for (c = 0; c < count; c++) { + t = *s++; + if (byteswap) { + /* Swap an Elf64_Shdr */ + SWAP_WORD(t.sh_name); + SWAP_WORD(t.sh_type); + SWAP_XWORD(t.sh_flags); + SWAP_ADDR64(t.sh_addr); + SWAP_OFF64(t.sh_offset); + SWAP_XWORD(t.sh_size); + SWAP_WORD(t.sh_link); + SWAP_WORD(t.sh_info); + SWAP_XWORD(t.sh_addralign); + SWAP_XWORD(t.sh_entsize); + /**/ + } + /* Write an Elf64_Shdr */ + WRITE_WORD(dst,t.sh_name); + WRITE_WORD(dst,t.sh_type); + WRITE_XWORD(dst,t.sh_flags); + WRITE_ADDR64(dst,t.sh_addr); + WRITE_OFF64(dst,t.sh_offset); + WRITE_XWORD(dst,t.sh_size); + WRITE_WORD(dst,t.sh_link); + WRITE_WORD(dst,t.sh_info); + WRITE_XWORD(dst,t.sh_addralign); + WRITE_XWORD(dst,t.sh_entsize); + /**/ + } + + return (1); +} + +static int +libelf_cvt32_SHDR_tom(char *dst, size_t dsz, char *src, size_t count, + int byteswap) +{ + Elf32_Shdr t, *d; + char *s,*s0; + size_t fsz; + + fsz = elf32_fsize(ELF_T_SHDR, (size_t) 1, EV_CURRENT); + d = ((Elf32_Shdr *) (uintptr_t) dst) + (count - 1); + s0 = (char *) src + (count - 1) * fsz; + + if (dsz < count * sizeof(Elf32_Shdr)) + return (0); + + while (count--) { + s = s0; + /* Read an Elf32_Shdr */ + READ_WORD(s,t.sh_name); + READ_WORD(s,t.sh_type); + READ_WORD(s,t.sh_flags); + READ_ADDR32(s,t.sh_addr); + READ_OFF32(s,t.sh_offset); + READ_WORD(s,t.sh_size); + READ_WORD(s,t.sh_link); + READ_WORD(s,t.sh_info); + READ_WORD(s,t.sh_addralign); + READ_WORD(s,t.sh_entsize); + /**/ + if (byteswap) { + /* Swap an Elf32_Shdr */ + SWAP_WORD(t.sh_name); + SWAP_WORD(t.sh_type); + SWAP_WORD(t.sh_flags); + SWAP_ADDR32(t.sh_addr); + SWAP_OFF32(t.sh_offset); + SWAP_WORD(t.sh_size); + SWAP_WORD(t.sh_link); + SWAP_WORD(t.sh_info); + SWAP_WORD(t.sh_addralign); + SWAP_WORD(t.sh_entsize); + /**/ + } + *d-- = t; s0 -= fsz; + } + + return (1); +} + +static int +libelf_cvt64_SHDR_tom(char *dst, size_t dsz, char *src, size_t count, + int byteswap) +{ + Elf64_Shdr t, *d; + char *s,*s0; + size_t fsz; + + fsz = elf64_fsize(ELF_T_SHDR, (size_t) 1, EV_CURRENT); + d = ((Elf64_Shdr *) (uintptr_t) dst) + (count - 1); + s0 = (char *) src + (count - 1) * fsz; + + if (dsz < count * sizeof(Elf64_Shdr)) + return (0); + + while (count--) { + s = s0; + /* Read an Elf64_Shdr */ + READ_WORD(s,t.sh_name); + READ_WORD(s,t.sh_type); + READ_XWORD(s,t.sh_flags); + READ_ADDR64(s,t.sh_addr); + READ_OFF64(s,t.sh_offset); + READ_XWORD(s,t.sh_size); + READ_WORD(s,t.sh_link); + READ_WORD(s,t.sh_info); + READ_XWORD(s,t.sh_addralign); + READ_XWORD(s,t.sh_entsize); + /**/ + if (byteswap) { + /* Swap an Elf64_Shdr */ + SWAP_WORD(t.sh_name); + SWAP_WORD(t.sh_type); + SWAP_XWORD(t.sh_flags); + SWAP_ADDR64(t.sh_addr); + SWAP_OFF64(t.sh_offset); + SWAP_XWORD(t.sh_size); + SWAP_WORD(t.sh_link); + SWAP_WORD(t.sh_info); + SWAP_XWORD(t.sh_addralign); + SWAP_XWORD(t.sh_entsize); + /**/ + } + *d-- = t; s0 -= fsz; + } + + return (1); +} + +#endif /* LIBELF_CONFIG_SHDR */ +#if LIBELF_CONFIG_SWORD + +static int +libelf_cvt_SWORD_tof(char *dst, size_t dsz, char *src, size_t count, + int byteswap) +{ + Elf64_Sword t, *s = (Elf64_Sword *) (uintptr_t) src; + size_t c; + + (void) dsz; + + if (!byteswap) { + (void) memcpy(dst, src, count * sizeof(*s)); + return (1); + } + + for (c = 0; c < count; c++) { + t = *s++; + SWAP_SWORD(t); + WRITE_SWORD(dst,t); + } + + return (1); +} + + +static int +libelf_cvt_SWORD_tom(char *dst, size_t dsz, char *src, size_t count, + int byteswap) +{ + Elf64_Sword t, *d = (Elf64_Sword *) (uintptr_t) dst; + size_t c; + + if (dsz < count * sizeof(Elf64_Sword)) + return (0); + + if (!byteswap) { + (void) memcpy(dst, src, count * sizeof(*d)); + return (1); + } + + for (c = 0; c < count; c++) { + READ_SWORD(src,t); + SWAP_SWORD(t); + *d++ = t; + } + + return (1); +} + +#endif /* LIBELF_CONFIG_SWORD */ +#if LIBELF_CONFIG_SXWORD + +static int +libelf_cvt_SXWORD_tof(char *dst, size_t dsz, char *src, size_t count, + int byteswap) +{ + Elf64_Sxword t, *s = (Elf64_Sxword *) (uintptr_t) src; + size_t c; + + (void) dsz; + + if (!byteswap) { + (void) memcpy(dst, src, count * sizeof(*s)); + return (1); + } + + for (c = 0; c < count; c++) { + t = *s++; + SWAP_SXWORD(t); + WRITE_SXWORD(dst,t); + } + + return (1); +} + + +static int +libelf_cvt_SXWORD_tom(char *dst, size_t dsz, char *src, size_t count, + int byteswap) +{ + Elf64_Sxword t, *d = (Elf64_Sxword *) (uintptr_t) dst; + size_t c; + + if (dsz < count * sizeof(Elf64_Sxword)) + return (0); + + if (!byteswap) { + (void) memcpy(dst, src, count * sizeof(*d)); + return (1); + } + + for (c = 0; c < count; c++) { + READ_SXWORD(src,t); + SWAP_SXWORD(t); + *d++ = t; + } + + return (1); +} + +#endif /* LIBELF_CONFIG_SXWORD */ +#if LIBELF_CONFIG_SYMINFO + +static int +libelf_cvt32_SYMINFO_tof(char *dst, size_t dsz, char *src, size_t count, + int byteswap) +{ + Elf32_Syminfo t, *s; + size_t c; + + (void) dsz; + + s = (Elf32_Syminfo *) (uintptr_t) src; + for (c = 0; c < count; c++) { + t = *s++; + if (byteswap) { + /* Swap an Elf32_Syminfo */ + SWAP_HALF(t.si_boundto); + SWAP_HALF(t.si_flags); + /**/ + } + /* Write an Elf32_Syminfo */ + WRITE_HALF(dst,t.si_boundto); + WRITE_HALF(dst,t.si_flags); + /**/ + } + + return (1); +} + +static int +libelf_cvt64_SYMINFO_tof(char *dst, size_t dsz, char *src, size_t count, + int byteswap) +{ + Elf64_Syminfo t, *s; + size_t c; + + (void) dsz; + + s = (Elf64_Syminfo *) (uintptr_t) src; + for (c = 0; c < count; c++) { + t = *s++; + if (byteswap) { + /* Swap an Elf64_Syminfo */ + SWAP_HALF(t.si_boundto); + SWAP_HALF(t.si_flags); + /**/ + } + /* Write an Elf64_Syminfo */ + WRITE_HALF(dst,t.si_boundto); + WRITE_HALF(dst,t.si_flags); + /**/ + } + + return (1); +} + +static int +libelf_cvt32_SYMINFO_tom(char *dst, size_t dsz, char *src, size_t count, + int byteswap) +{ + Elf32_Syminfo t, *d; + char *s,*s0; + size_t fsz; + + fsz = elf32_fsize(ELF_T_SYMINFO, (size_t) 1, EV_CURRENT); + d = ((Elf32_Syminfo *) (uintptr_t) dst) + (count - 1); + s0 = (char *) src + (count - 1) * fsz; + + if (dsz < count * sizeof(Elf32_Syminfo)) + return (0); + + while (count--) { + s = s0; + /* Read an Elf32_Syminfo */ + READ_HALF(s,t.si_boundto); + READ_HALF(s,t.si_flags); + /**/ + if (byteswap) { + /* Swap an Elf32_Syminfo */ + SWAP_HALF(t.si_boundto); + SWAP_HALF(t.si_flags); + /**/ + } + *d-- = t; s0 -= fsz; + } + + return (1); +} + +static int +libelf_cvt64_SYMINFO_tom(char *dst, size_t dsz, char *src, size_t count, + int byteswap) +{ + Elf64_Syminfo t, *d; + char *s,*s0; + size_t fsz; + + fsz = elf64_fsize(ELF_T_SYMINFO, (size_t) 1, EV_CURRENT); + d = ((Elf64_Syminfo *) (uintptr_t) dst) + (count - 1); + s0 = (char *) src + (count - 1) * fsz; + + if (dsz < count * sizeof(Elf64_Syminfo)) + return (0); + + while (count--) { + s = s0; + /* Read an Elf64_Syminfo */ + READ_HALF(s,t.si_boundto); + READ_HALF(s,t.si_flags); + /**/ + if (byteswap) { + /* Swap an Elf64_Syminfo */ + SWAP_HALF(t.si_boundto); + SWAP_HALF(t.si_flags); + /**/ + } + *d-- = t; s0 -= fsz; + } + + return (1); +} + +#endif /* LIBELF_CONFIG_SYMINFO */ +#if LIBELF_CONFIG_SYM + +static int +libelf_cvt32_SYM_tof(char *dst, size_t dsz, char *src, size_t count, + int byteswap) +{ + Elf32_Sym t, *s; + size_t c; + + (void) dsz; + + s = (Elf32_Sym *) (uintptr_t) src; + for (c = 0; c < count; c++) { + t = *s++; + if (byteswap) { + /* Swap an Elf32_Sym */ + SWAP_WORD(t.st_name); + SWAP_ADDR32(t.st_value); + SWAP_WORD(t.st_size); + SWAP_HALF(t.st_shndx); + /**/ + } + /* Write an Elf32_Sym */ + WRITE_WORD(dst,t.st_name); + WRITE_ADDR32(dst,t.st_value); + WRITE_WORD(dst,t.st_size); + WRITE_BYTE(dst,t.st_info); + WRITE_BYTE(dst,t.st_other); + WRITE_HALF(dst,t.st_shndx); + /**/ + } + + return (1); +} + +static int +libelf_cvt64_SYM_tof(char *dst, size_t dsz, char *src, size_t count, + int byteswap) +{ + Elf64_Sym t, *s; + size_t c; + + (void) dsz; + + s = (Elf64_Sym *) (uintptr_t) src; + for (c = 0; c < count; c++) { + t = *s++; + if (byteswap) { + /* Swap an Elf64_Sym */ + SWAP_WORD(t.st_name); + SWAP_HALF(t.st_shndx); + SWAP_ADDR64(t.st_value); + SWAP_XWORD(t.st_size); + /**/ + } + /* Write an Elf64_Sym */ + WRITE_WORD(dst,t.st_name); + WRITE_BYTE(dst,t.st_info); + WRITE_BYTE(dst,t.st_other); + WRITE_HALF(dst,t.st_shndx); + WRITE_ADDR64(dst,t.st_value); + WRITE_XWORD(dst,t.st_size); + /**/ + } + + return (1); +} + +static int +libelf_cvt32_SYM_tom(char *dst, size_t dsz, char *src, size_t count, + int byteswap) +{ + Elf32_Sym t, *d; + char *s,*s0; + size_t fsz; + + fsz = elf32_fsize(ELF_T_SYM, (size_t) 1, EV_CURRENT); + d = ((Elf32_Sym *) (uintptr_t) dst) + (count - 1); + s0 = (char *) src + (count - 1) * fsz; + + if (dsz < count * sizeof(Elf32_Sym)) + return (0); + + while (count--) { + s = s0; + /* Read an Elf32_Sym */ + READ_WORD(s,t.st_name); + READ_ADDR32(s,t.st_value); + READ_WORD(s,t.st_size); + READ_BYTE(s,t.st_info); + READ_BYTE(s,t.st_other); + READ_HALF(s,t.st_shndx); + /**/ + if (byteswap) { + /* Swap an Elf32_Sym */ + SWAP_WORD(t.st_name); + SWAP_ADDR32(t.st_value); + SWAP_WORD(t.st_size); + SWAP_HALF(t.st_shndx); + /**/ + } + *d-- = t; s0 -= fsz; + } + + return (1); +} + +static int +libelf_cvt64_SYM_tom(char *dst, size_t dsz, char *src, size_t count, + int byteswap) +{ + Elf64_Sym t, *d; + char *s,*s0; + size_t fsz; + + fsz = elf64_fsize(ELF_T_SYM, (size_t) 1, EV_CURRENT); + d = ((Elf64_Sym *) (uintptr_t) dst) + (count - 1); + s0 = (char *) src + (count - 1) * fsz; + + if (dsz < count * sizeof(Elf64_Sym)) + return (0); + + while (count--) { + s = s0; + /* Read an Elf64_Sym */ + READ_WORD(s,t.st_name); + READ_BYTE(s,t.st_info); + READ_BYTE(s,t.st_other); + READ_HALF(s,t.st_shndx); + READ_ADDR64(s,t.st_value); + READ_XWORD(s,t.st_size); + /**/ + if (byteswap) { + /* Swap an Elf64_Sym */ + SWAP_WORD(t.st_name); + SWAP_HALF(t.st_shndx); + SWAP_ADDR64(t.st_value); + SWAP_XWORD(t.st_size); + /**/ + } + *d-- = t; s0 -= fsz; + } + + return (1); +} + +#endif /* LIBELF_CONFIG_SYM */ +#if LIBELF_CONFIG_VDEF + +static int +libelf_cvt32_VDEF_tof(char *dst, size_t dsz, char *src, size_t count, + int byteswap) +{ + Elf32_Verdef t, *s; + size_t c; + + (void) dsz; + + s = (Elf32_Verdef *) (uintptr_t) src; + for (c = 0; c < count; c++) { + t = *s++; + if (byteswap) { + /* Swap an Elf32_Verdef */ + SWAP_HALF(t.vd_version); + SWAP_HALF(t.vd_flags); + SWAP_HALF(t.vd_ndx); + SWAP_HALF(t.vd_cnt); + SWAP_WORD(t.vd_hash); + SWAP_WORD(t.vd_aux); + SWAP_WORD(t.vd_next); + /**/ + } + /* Write an Elf32_Verdef */ + WRITE_HALF(dst,t.vd_version); + WRITE_HALF(dst,t.vd_flags); + WRITE_HALF(dst,t.vd_ndx); + WRITE_HALF(dst,t.vd_cnt); + WRITE_WORD(dst,t.vd_hash); + WRITE_WORD(dst,t.vd_aux); + WRITE_WORD(dst,t.vd_next); + /**/ + } + + return (1); +} + +static int +libelf_cvt64_VDEF_tof(char *dst, size_t dsz, char *src, size_t count, + int byteswap) +{ + Elf64_Verdef t, *s; + size_t c; + + (void) dsz; + + s = (Elf64_Verdef *) (uintptr_t) src; + for (c = 0; c < count; c++) { + t = *s++; + if (byteswap) { + /* Swap an Elf64_Verdef */ + SWAP_HALF(t.vd_version); + SWAP_HALF(t.vd_flags); + SWAP_HALF(t.vd_ndx); + SWAP_HALF(t.vd_cnt); + SWAP_WORD(t.vd_hash); + SWAP_WORD(t.vd_aux); + SWAP_WORD(t.vd_next); + /**/ + } + /* Write an Elf64_Verdef */ + WRITE_HALF(dst,t.vd_version); + WRITE_HALF(dst,t.vd_flags); + WRITE_HALF(dst,t.vd_ndx); + WRITE_HALF(dst,t.vd_cnt); + WRITE_WORD(dst,t.vd_hash); + WRITE_WORD(dst,t.vd_aux); + WRITE_WORD(dst,t.vd_next); + /**/ + } + + return (1); +} + +static int +libelf_cvt32_VDEF_tom(char *dst, size_t dsz, char *src, size_t count, + int byteswap) +{ + Elf32_Verdef t, *d; + char *s,*s0; + size_t fsz; + + fsz = elf32_fsize(ELF_T_VDEF, (size_t) 1, EV_CURRENT); + d = ((Elf32_Verdef *) (uintptr_t) dst) + (count - 1); + s0 = (char *) src + (count - 1) * fsz; + + if (dsz < count * sizeof(Elf32_Verdef)) + return (0); + + while (count--) { + s = s0; + /* Read an Elf32_Verdef */ + READ_HALF(s,t.vd_version); + READ_HALF(s,t.vd_flags); + READ_HALF(s,t.vd_ndx); + READ_HALF(s,t.vd_cnt); + READ_WORD(s,t.vd_hash); + READ_WORD(s,t.vd_aux); + READ_WORD(s,t.vd_next); + /**/ + if (byteswap) { + /* Swap an Elf32_Verdef */ + SWAP_HALF(t.vd_version); + SWAP_HALF(t.vd_flags); + SWAP_HALF(t.vd_ndx); + SWAP_HALF(t.vd_cnt); + SWAP_WORD(t.vd_hash); + SWAP_WORD(t.vd_aux); + SWAP_WORD(t.vd_next); + /**/ + } + *d-- = t; s0 -= fsz; + } + + return (1); +} + +static int +libelf_cvt64_VDEF_tom(char *dst, size_t dsz, char *src, size_t count, + int byteswap) +{ + Elf64_Verdef t, *d; + char *s,*s0; + size_t fsz; + + fsz = elf64_fsize(ELF_T_VDEF, (size_t) 1, EV_CURRENT); + d = ((Elf64_Verdef *) (uintptr_t) dst) + (count - 1); + s0 = (char *) src + (count - 1) * fsz; + + if (dsz < count * sizeof(Elf64_Verdef)) + return (0); + + while (count--) { + s = s0; + /* Read an Elf64_Verdef */ + READ_HALF(s,t.vd_version); + READ_HALF(s,t.vd_flags); + READ_HALF(s,t.vd_ndx); + READ_HALF(s,t.vd_cnt); + READ_WORD(s,t.vd_hash); + READ_WORD(s,t.vd_aux); + READ_WORD(s,t.vd_next); + /**/ + if (byteswap) { + /* Swap an Elf64_Verdef */ + SWAP_HALF(t.vd_version); + SWAP_HALF(t.vd_flags); + SWAP_HALF(t.vd_ndx); + SWAP_HALF(t.vd_cnt); + SWAP_WORD(t.vd_hash); + SWAP_WORD(t.vd_aux); + SWAP_WORD(t.vd_next); + /**/ + } + *d-- = t; s0 -= fsz; + } + + return (1); +} + +#endif /* LIBELF_CONFIG_VDEF */ +#if LIBELF_CONFIG_VNEED + +static int +libelf_cvt32_VNEED_tof(char *dst, size_t dsz, char *src, size_t count, + int byteswap) +{ + Elf32_Verneed t, *s; + size_t c; + + (void) dsz; + + s = (Elf32_Verneed *) (uintptr_t) src; + for (c = 0; c < count; c++) { + t = *s++; + if (byteswap) { + /* Swap an Elf32_Verneed */ + SWAP_HALF(t.vn_version); + SWAP_HALF(t.vn_cnt); + SWAP_WORD(t.vn_file); + SWAP_WORD(t.vn_aux); + SWAP_WORD(t.vn_next); + /**/ + } + /* Write an Elf32_Verneed */ + WRITE_HALF(dst,t.vn_version); + WRITE_HALF(dst,t.vn_cnt); + WRITE_WORD(dst,t.vn_file); + WRITE_WORD(dst,t.vn_aux); + WRITE_WORD(dst,t.vn_next); + /**/ + } + + return (1); +} + +static int +libelf_cvt64_VNEED_tof(char *dst, size_t dsz, char *src, size_t count, + int byteswap) +{ + Elf64_Verneed t, *s; + size_t c; + + (void) dsz; + + s = (Elf64_Verneed *) (uintptr_t) src; + for (c = 0; c < count; c++) { + t = *s++; + if (byteswap) { + /* Swap an Elf64_Verneed */ + SWAP_HALF(t.vn_version); + SWAP_HALF(t.vn_cnt); + SWAP_WORD(t.vn_file); + SWAP_WORD(t.vn_aux); + SWAP_WORD(t.vn_next); + /**/ + } + /* Write an Elf64_Verneed */ + WRITE_HALF(dst,t.vn_version); + WRITE_HALF(dst,t.vn_cnt); + WRITE_WORD(dst,t.vn_file); + WRITE_WORD(dst,t.vn_aux); + WRITE_WORD(dst,t.vn_next); + /**/ + } + + return (1); +} + +static int +libelf_cvt32_VNEED_tom(char *dst, size_t dsz, char *src, size_t count, + int byteswap) +{ + Elf32_Verneed t, *d; + char *s,*s0; + size_t fsz; + + fsz = elf32_fsize(ELF_T_VNEED, (size_t) 1, EV_CURRENT); + d = ((Elf32_Verneed *) (uintptr_t) dst) + (count - 1); + s0 = (char *) src + (count - 1) * fsz; + + if (dsz < count * sizeof(Elf32_Verneed)) + return (0); + + while (count--) { + s = s0; + /* Read an Elf32_Verneed */ + READ_HALF(s,t.vn_version); + READ_HALF(s,t.vn_cnt); + READ_WORD(s,t.vn_file); + READ_WORD(s,t.vn_aux); + READ_WORD(s,t.vn_next); + /**/ + if (byteswap) { + /* Swap an Elf32_Verneed */ + SWAP_HALF(t.vn_version); + SWAP_HALF(t.vn_cnt); + SWAP_WORD(t.vn_file); + SWAP_WORD(t.vn_aux); + SWAP_WORD(t.vn_next); + /**/ + } + *d-- = t; s0 -= fsz; + } + + return (1); +} + +static int +libelf_cvt64_VNEED_tom(char *dst, size_t dsz, char *src, size_t count, + int byteswap) +{ + Elf64_Verneed t, *d; + char *s,*s0; + size_t fsz; + + fsz = elf64_fsize(ELF_T_VNEED, (size_t) 1, EV_CURRENT); + d = ((Elf64_Verneed *) (uintptr_t) dst) + (count - 1); + s0 = (char *) src + (count - 1) * fsz; + + if (dsz < count * sizeof(Elf64_Verneed)) + return (0); + + while (count--) { + s = s0; + /* Read an Elf64_Verneed */ + READ_HALF(s,t.vn_version); + READ_HALF(s,t.vn_cnt); + READ_WORD(s,t.vn_file); + READ_WORD(s,t.vn_aux); + READ_WORD(s,t.vn_next); + /**/ + if (byteswap) { + /* Swap an Elf64_Verneed */ + SWAP_HALF(t.vn_version); + SWAP_HALF(t.vn_cnt); + SWAP_WORD(t.vn_file); + SWAP_WORD(t.vn_aux); + SWAP_WORD(t.vn_next); + /**/ + } + *d-- = t; s0 -= fsz; + } + + return (1); +} + +#endif /* LIBELF_CONFIG_VNEED */ +#if LIBELF_CONFIG_WORD + +static int +libelf_cvt_WORD_tof(char *dst, size_t dsz, char *src, size_t count, + int byteswap) +{ + Elf64_Word t, *s = (Elf64_Word *) (uintptr_t) src; + size_t c; + + (void) dsz; + + if (!byteswap) { + (void) memcpy(dst, src, count * sizeof(*s)); + return (1); + } + + for (c = 0; c < count; c++) { + t = *s++; + SWAP_WORD(t); + WRITE_WORD(dst,t); + } + + return (1); +} + + +static int +libelf_cvt_WORD_tom(char *dst, size_t dsz, char *src, size_t count, + int byteswap) +{ + Elf64_Word t, *d = (Elf64_Word *) (uintptr_t) dst; + size_t c; + + if (dsz < count * sizeof(Elf64_Word)) + return (0); + + if (!byteswap) { + (void) memcpy(dst, src, count * sizeof(*d)); + return (1); + } + + for (c = 0; c < count; c++) { + READ_WORD(src,t); + SWAP_WORD(t); + *d++ = t; + } + + return (1); +} + +#endif /* LIBELF_CONFIG_WORD */ +#if LIBELF_CONFIG_XWORD + +static int +libelf_cvt_XWORD_tof(char *dst, size_t dsz, char *src, size_t count, + int byteswap) +{ + Elf64_Xword t, *s = (Elf64_Xword *) (uintptr_t) src; + size_t c; + + (void) dsz; + + if (!byteswap) { + (void) memcpy(dst, src, count * sizeof(*s)); + return (1); + } + + for (c = 0; c < count; c++) { + t = *s++; + SWAP_XWORD(t); + WRITE_XWORD(dst,t); + } + + return (1); +} + + +static int +libelf_cvt_XWORD_tom(char *dst, size_t dsz, char *src, size_t count, + int byteswap) +{ + Elf64_Xword t, *d = (Elf64_Xword *) (uintptr_t) dst; + size_t c; + + if (dsz < count * sizeof(Elf64_Xword)) + return (0); + + if (!byteswap) { + (void) memcpy(dst, src, count * sizeof(*d)); + return (1); + } + + for (c = 0; c < count; c++) { + READ_XWORD(src,t); + SWAP_XWORD(t); + *d++ = t; + } + + return (1); +} + +#endif /* LIBELF_CONFIG_XWORD */ + + +#if LIBELF_CONFIG_GNUHASH +/* + * Sections of type ELF_T_GNUHASH start with a header containing 4 32-bit + * words. Bloom filter data comes next, followed by hash buckets and the + * hash chain. + * + * Bloom filter words are 64 bit wide on ELFCLASS64 objects and are 32 bit + * wide on ELFCLASS32 objects. The other objects in this section are 32 + * bits wide. + * + * Argument srcsz denotes the number of bytes to be converted. In the + * 32-bit case we need to translate srcsz to a count of 32-bit words. + */ + +static int +libelf_cvt32_GNUHASH_tom(char *dst, size_t dsz, char *src, size_t srcsz, + int byteswap) +{ + return (libelf_cvt_WORD_tom(dst, dsz, src, srcsz / sizeof(uint32_t), + byteswap)); +} + +static int +libelf_cvt32_GNUHASH_tof(char *dst, size_t dsz, char *src, size_t srcsz, + int byteswap) +{ + return (libelf_cvt_WORD_tof(dst, dsz, src, srcsz / sizeof(uint32_t), + byteswap)); +} + +static int +libelf_cvt64_GNUHASH_tom(char *dst, size_t dsz, char *src, size_t srcsz, + int byteswap) +{ + size_t sz; + uint64_t t64, *bloom64; + Elf_GNU_Hash_Header *gh; + uint32_t n, nbuckets, nchains, maskwords, shift2, symndx, t32; + uint32_t *buckets, *chains; + + sz = 4 * sizeof(uint32_t); /* File header is 4 words long. */ + if (dsz < sizeof(Elf_GNU_Hash_Header) || srcsz < sz) + return (0); + + /* Read in the section header and byteswap if needed. */ + READ_WORD(src, nbuckets); + READ_WORD(src, symndx); + READ_WORD(src, maskwords); + READ_WORD(src, shift2); + + srcsz -= sz; + + if (byteswap) { + SWAP_WORD(nbuckets); + SWAP_WORD(symndx); + SWAP_WORD(maskwords); + SWAP_WORD(shift2); + } + + /* Check source buffer and destination buffer sizes. */ + sz = nbuckets * sizeof(uint32_t) + maskwords * sizeof(uint64_t); + if (srcsz < sz || dsz < sz + sizeof(Elf_GNU_Hash_Header)) + return (0); + + gh = (Elf_GNU_Hash_Header *) (uintptr_t) dst; + gh->gh_nbuckets = nbuckets; + gh->gh_symndx = symndx; + gh->gh_maskwords = maskwords; + gh->gh_shift2 = shift2; + + dsz -= sizeof(Elf_GNU_Hash_Header); + dst += sizeof(Elf_GNU_Hash_Header); + + bloom64 = (uint64_t *) (uintptr_t) dst; + + /* Copy bloom filter data. */ + for (n = 0; n < maskwords; n++) { + READ_XWORD(src, t64); + if (byteswap) + SWAP_XWORD(t64); + bloom64[n] = t64; + } + + /* The hash buckets follows the bloom filter. */ + dst += maskwords * sizeof(uint64_t); + buckets = (uint32_t *) (uintptr_t) dst; + + for (n = 0; n < nbuckets; n++) { + READ_WORD(src, t32); + if (byteswap) + SWAP_WORD(t32); + buckets[n] = t32; + } + + dst += nbuckets * sizeof(uint32_t); + + /* The hash chain follows the hash buckets. */ + dsz -= sz; + srcsz -= sz; + + if (dsz < srcsz) /* Destination lacks space. */ + return (0); + + nchains = srcsz / sizeof(uint32_t); + chains = (uint32_t *) (uintptr_t) dst; + + for (n = 0; n < nchains; n++) { + READ_WORD(src, t32); + if (byteswap) + SWAP_WORD(t32); + *chains++ = t32; + } + + return (1); +} + +static int +libelf_cvt64_GNUHASH_tof(char *dst, size_t dsz, char *src, size_t srcsz, + int byteswap) +{ + uint32_t *s32; + size_t sz, hdrsz; + uint64_t *s64, t64; + Elf_GNU_Hash_Header *gh; + uint32_t maskwords, n, nbuckets, nchains, t0, t1, t2, t3, t32; + + hdrsz = 4 * sizeof(uint32_t); /* Header is 4x32 bits. */ + if (dsz < hdrsz || srcsz < sizeof(Elf_GNU_Hash_Header)) + return (0); + + gh = (Elf_GNU_Hash_Header *) (uintptr_t) src; + + t0 = nbuckets = gh->gh_nbuckets; + t1 = gh->gh_symndx; + t2 = maskwords = gh->gh_maskwords; + t3 = gh->gh_shift2; + + src += sizeof(Elf_GNU_Hash_Header); + srcsz -= sizeof(Elf_GNU_Hash_Header); + dsz -= hdrsz; + + sz = gh->gh_nbuckets * sizeof(uint32_t) + gh->gh_maskwords * + sizeof(uint64_t); + + if (srcsz < sz || dsz < sz) + return (0); + + /* Write out the header. */ + if (byteswap) { + SWAP_WORD(t0); + SWAP_WORD(t1); + SWAP_WORD(t2); + SWAP_WORD(t3); + } + + WRITE_WORD(dst, t0); + WRITE_WORD(dst, t1); + WRITE_WORD(dst, t2); + WRITE_WORD(dst, t3); + + /* Copy the bloom filter and the hash table. */ + s64 = (uint64_t *) (uintptr_t) src; + for (n = 0; n < maskwords; n++) { + t64 = *s64++; + if (byteswap) + SWAP_XWORD(t64); + WRITE_WORD64(dst, t64); + } + + s32 = (uint32_t *) s64; + for (n = 0; n < nbuckets; n++) { + t32 = *s32++; + if (byteswap) + SWAP_WORD(t32); + WRITE_WORD(dst, t32); + } + + srcsz -= sz; + dsz -= sz; + + /* Copy out the hash chains. */ + if (dsz < srcsz) + return (0); + + nchains = srcsz / sizeof(uint32_t); + for (n = 0; n < nchains; n++) { + t32 = *s32++; + if (byteswap) + SWAP_WORD(t32); + WRITE_WORD(dst, t32); + } + + return (1); +} +#endif /* LIBELF_CONFIG_GNUHASH */ + +#if LIBELF_CONFIG_NOTE +/* + * Elf_Note structures comprise a fixed size header followed by variable + * length strings. The fixed size header needs to be byte swapped, but + * not the strings. + * + * Argument count denotes the total number of bytes to be converted. + * The destination buffer needs to be at least count bytes in size. + */ +static int +libelf_cvt_NOTE_tom(char *dst, size_t dsz, char *src, size_t count, + int byteswap) +{ + uint32_t namesz, descsz, type; + Elf_Note *en; + size_t sz, hdrsz; + + if (dsz < count) /* Destination buffer is too small. */ + return (0); + + hdrsz = 3 * sizeof(uint32_t); + if (count < hdrsz) /* Source too small. */ + return (0); + + if (!byteswap) { + (void) memcpy(dst, src, count); + return (1); + } + + /* Process all notes in the section. */ + while (count > hdrsz) { + /* Read the note header. */ + READ_WORD(src, namesz); + READ_WORD(src, descsz); + READ_WORD(src, type); + + /* Translate. */ + SWAP_WORD(namesz); + SWAP_WORD(descsz); + SWAP_WORD(type); + + /* Copy out the translated note header. */ + en = (Elf_Note *) (uintptr_t) dst; + en->n_namesz = namesz; + en->n_descsz = descsz; + en->n_type = type; + + dsz -= sizeof(Elf_Note); + dst += sizeof(Elf_Note); + count -= hdrsz; + + ROUNDUP2(namesz, 4); + ROUNDUP2(descsz, 4); + + sz = namesz + descsz; + + if (count < sz || dsz < sz) /* Buffers are too small. */ + return (0); + + (void) memcpy(dst, src, sz); + + src += sz; + dst += sz; + + count -= sz; + dsz -= sz; + } + + return (1); +} + +static int +libelf_cvt_NOTE_tof(char *dst, size_t dsz, char *src, size_t count, + int byteswap) +{ + uint32_t namesz, descsz, type; + Elf_Note *en; + size_t sz; + + if (dsz < count) + return (0); + + if (!byteswap) { + (void) memcpy(dst, src, count); + return (1); + } + + while (count > sizeof(Elf_Note)) { + + en = (Elf_Note *) (uintptr_t) src; + namesz = en->n_namesz; + descsz = en->n_descsz; + type = en->n_type; + + SWAP_WORD(namesz); + SWAP_WORD(descsz); + SWAP_WORD(type); + + WRITE_WORD(dst, namesz); + WRITE_WORD(dst, descsz); + WRITE_WORD(dst, type); + + src += sizeof(Elf_Note); + + ROUNDUP2(namesz, 4); + ROUNDUP2(descsz, 4); + + sz = namesz + descsz; + + if (count < sz) + sz = count; + + (void) memcpy(dst, src, sz); + + src += sz; + dst += sz; + count -= sz; + } + + return (1); +} +#endif /* LIBELF_CONFIG_NOTE */ + +struct converters { + int (*tof32)(char *dst, size_t dsz, char *src, size_t cnt, + int byteswap); + int (*tom32)(char *dst, size_t dsz, char *src, size_t cnt, + int byteswap); + int (*tof64)(char *dst, size_t dsz, char *src, size_t cnt, + int byteswap); + int (*tom64)(char *dst, size_t dsz, char *src, size_t cnt, + int byteswap); +}; + + + +static struct converters cvt[ELF_T_NUM] = { +#if defined(__GNUC__) +#if LIBELF_CONFIG_ADDR + [ELF_T_ADDR] = { + .tof32 = libelf_cvt_ADDR32_tof, .tom32 = libelf_cvt_ADDR32_tom, + .tof64 = libelf_cvt_ADDR64_tof, .tom64 = libelf_cvt_ADDR64_tom }, +#endif +#if LIBELF_CONFIG_CAP + [ELF_T_CAP] = { + .tof32 = libelf_cvt32_CAP_tof, .tom32 = libelf_cvt32_CAP_tom, + .tof64 = libelf_cvt64_CAP_tof, .tom64 = libelf_cvt64_CAP_tom }, +#endif +#if LIBELF_CONFIG_DYN + [ELF_T_DYN] = { + .tof32 = libelf_cvt32_DYN_tof, .tom32 = libelf_cvt32_DYN_tom, + .tof64 = libelf_cvt64_DYN_tof, .tom64 = libelf_cvt64_DYN_tom }, +#endif +#if LIBELF_CONFIG_EHDR + [ELF_T_EHDR] = { + .tof32 = libelf_cvt32_EHDR_tof, .tom32 = libelf_cvt32_EHDR_tom, + .tof64 = libelf_cvt64_EHDR_tof, .tom64 = libelf_cvt64_EHDR_tom }, +#endif +#if LIBELF_CONFIG_HALF + [ELF_T_HALF] = { + .tof32 = libelf_cvt_HALF_tof, .tom32 = libelf_cvt_HALF_tom, + .tof64 = libelf_cvt_HALF_tof, .tom64 = libelf_cvt_HALF_tom }, +#endif +#if LIBELF_CONFIG_LWORD + [ELF_T_LWORD] = { + .tof32 = libelf_cvt_LWORD_tof, .tom32 = libelf_cvt_LWORD_tom, + .tof64 = libelf_cvt_LWORD_tof, .tom64 = libelf_cvt_LWORD_tom }, +#endif +#if LIBELF_CONFIG_MOVE + [ELF_T_MOVE] = { + .tof32 = libelf_cvt32_MOVE_tof, .tom32 = libelf_cvt32_MOVE_tom, + .tof64 = libelf_cvt64_MOVE_tof, .tom64 = libelf_cvt64_MOVE_tom }, +#endif +#if LIBELF_CONFIG_MOVEP + [ELF_T_MOVEP] = { + .tof32 = NULL, .tom32 = NULL, + .tof64 = NULL, .tom64 = NULL }, +#endif +#if LIBELF_CONFIG_OFF + [ELF_T_OFF] = { + .tof32 = libelf_cvt_OFF32_tof, .tom32 = libelf_cvt_OFF32_tom, + .tof64 = libelf_cvt_OFF64_tof, .tom64 = libelf_cvt_OFF64_tom }, +#endif +#if LIBELF_CONFIG_PHDR + [ELF_T_PHDR] = { + .tof32 = libelf_cvt32_PHDR_tof, .tom32 = libelf_cvt32_PHDR_tom, + .tof64 = libelf_cvt64_PHDR_tof, .tom64 = libelf_cvt64_PHDR_tom }, +#endif +#if LIBELF_CONFIG_REL + [ELF_T_REL] = { + .tof32 = libelf_cvt32_REL_tof, .tom32 = libelf_cvt32_REL_tom, + .tof64 = libelf_cvt64_REL_tof, .tom64 = libelf_cvt64_REL_tom }, +#endif +#if LIBELF_CONFIG_RELA + [ELF_T_RELA] = { + .tof32 = libelf_cvt32_RELA_tof, .tom32 = libelf_cvt32_RELA_tom, + .tof64 = libelf_cvt64_RELA_tof, .tom64 = libelf_cvt64_RELA_tom }, +#endif +#if LIBELF_CONFIG_SHDR + [ELF_T_SHDR] = { + .tof32 = libelf_cvt32_SHDR_tof, .tom32 = libelf_cvt32_SHDR_tom, + .tof64 = libelf_cvt64_SHDR_tof, .tom64 = libelf_cvt64_SHDR_tom }, +#endif +#if LIBELF_CONFIG_SWORD + [ELF_T_SWORD] = { + .tof32 = libelf_cvt_SWORD_tof, .tom32 = libelf_cvt_SWORD_tom, + .tof64 = libelf_cvt_SWORD_tof, .tom64 = libelf_cvt_SWORD_tom }, +#endif +#if LIBELF_CONFIG_SXWORD + [ELF_T_SXWORD] = { + .tof32 = NULL, .tom32 = NULL, + .tof64 = libelf_cvt_SXWORD_tof, .tom64 = libelf_cvt_SXWORD_tom }, +#endif +#if LIBELF_CONFIG_SYMINFO + [ELF_T_SYMINFO] = { + .tof32 = libelf_cvt32_SYMINFO_tof, .tom32 = libelf_cvt32_SYMINFO_tom, + .tof64 = libelf_cvt64_SYMINFO_tof, .tom64 = libelf_cvt64_SYMINFO_tom }, +#endif +#if LIBELF_CONFIG_SYM + [ELF_T_SYM] = { + .tof32 = libelf_cvt32_SYM_tof, .tom32 = libelf_cvt32_SYM_tom, + .tof64 = libelf_cvt64_SYM_tof, .tom64 = libelf_cvt64_SYM_tom }, +#endif +#if LIBELF_CONFIG_VDEF + [ELF_T_VDEF] = { + .tof32 = libelf_cvt32_VDEF_tof, .tom32 = libelf_cvt32_VDEF_tom, + .tof64 = libelf_cvt64_VDEF_tof, .tom64 = libelf_cvt64_VDEF_tom }, +#endif +#if LIBELF_CONFIG_VNEED + [ELF_T_VNEED] = { + .tof32 = libelf_cvt32_VNEED_tof, .tom32 = libelf_cvt32_VNEED_tom, + .tof64 = libelf_cvt64_VNEED_tof, .tom64 = libelf_cvt64_VNEED_tom }, +#endif +#if LIBELF_CONFIG_WORD + [ELF_T_WORD] = { + .tof32 = libelf_cvt_WORD_tof, .tom32 = libelf_cvt_WORD_tom, + .tof64 = libelf_cvt_WORD_tof, .tom64 = libelf_cvt_WORD_tom }, +#endif +#if LIBELF_CONFIG_XWORD + [ELF_T_XWORD] = { + .tof32 = NULL, .tom32 = NULL, + .tof64 = libelf_cvt_XWORD_tof, .tom64 = libelf_cvt_XWORD_tom }, +#endif + + + /* + * Types that needs hand-coded converters follow. + */ + + [ELF_T_BYTE] = { + .tof32 = libelf_cvt_BYTE_tox, + .tom32 = libelf_cvt_BYTE_tox, + .tof64 = libelf_cvt_BYTE_tox, + .tom64 = libelf_cvt_BYTE_tox + }, + +#if LIBELF_CONFIG_GNUHASH + [ELF_T_GNUHASH] = { + .tof32 = libelf_cvt32_GNUHASH_tof, + .tom32 = libelf_cvt32_GNUHASH_tom, + .tof64 = libelf_cvt64_GNUHASH_tof, + .tom64 = libelf_cvt64_GNUHASH_tom + }, +#endif /* LIBELF_CONFIG_GNUHASH */ + +#if LIBELF_CONFIG_NOTE + [ELF_T_NOTE] = { + .tof32 = libelf_cvt_NOTE_tof, + .tom32 = libelf_cvt_NOTE_tom, + .tof64 = libelf_cvt_NOTE_tof, + .tom64 = libelf_cvt_NOTE_tom + } +#endif /* LIBELF_CONFIG_NOTE */ +#else + { libelf_cvt_ADDR32_tof, + libelf_cvt_ADDR32_tom, + libelf_cvt_ADDR64_tof, + libelf_cvt_ADDR64_tom + }, + { libelf_cvt_BYTE_tox, + libelf_cvt_BYTE_tox, + libelf_cvt_BYTE_tox, + libelf_cvt_BYTE_tox + }, + { 0, 0, 0, 0} , + { libelf_cvt32_DYN_tof, + libelf_cvt32_DYN_tom, + libelf_cvt64_DYN_tof, + libelf_cvt64_DYN_tom + }, + { libelf_cvt32_EHDR_tof, + libelf_cvt32_EHDR_tom, + libelf_cvt64_EHDR_tof, + libelf_cvt64_EHDR_tom + }, + { + libelf_cvt_HALF_tof, + libelf_cvt_HALF_tom, + libelf_cvt_HALF_tof, + libelf_cvt_HALF_tom + }, + {0, 0, 0, 0}, + {0, 0, 0, 0}, + {0, 0, 0, 0}, +#if LIBELF_CONFIG_NOTE + { libelf_cvt_NOTE_tof, + libelf_cvt_NOTE_tom, + libelf_cvt_NOTE_tof, + libelf_cvt_NOTE_tom + }, +#else + {0, 0, 0, 0}, +#endif /* LIBELF_CONFIG_NOTE */ + { libelf_cvt_OFF32_tof, + libelf_cvt_OFF32_tom, + libelf_cvt_OFF64_tof, + libelf_cvt_OFF64_tom + }, + { libelf_cvt32_PHDR_tof, + libelf_cvt32_PHDR_tom, + libelf_cvt64_PHDR_tof, + libelf_cvt64_PHDR_tom + }, + { libelf_cvt32_REL_tof, + libelf_cvt32_REL_tom, + libelf_cvt64_REL_tof, + libelf_cvt64_REL_tom + }, + { libelf_cvt32_RELA_tof, + libelf_cvt32_RELA_tom, + libelf_cvt64_RELA_tof, + libelf_cvt64_RELA_tom + }, + { libelf_cvt32_SHDR_tof, + libelf_cvt32_SHDR_tom, + libelf_cvt64_SHDR_tof, + libelf_cvt64_SHDR_tom + }, + { libelf_cvt_SWORD_tof, + libelf_cvt_SWORD_tom, + libelf_cvt_SWORD_tof, + libelf_cvt_SWORD_tom + }, + { 0, 0, + libelf_cvt_SXWORD_tof, + libelf_cvt_SXWORD_tom + }, + {0, 0, 0, 0}, + { libelf_cvt32_SYM_tof, + libelf_cvt32_SYM_tom, + libelf_cvt64_SYM_tof, + libelf_cvt64_SYM_tom + }, + {0, 0, 0, 0}, {0, 0, 0, 0}, + { libelf_cvt_WORD_tof, + libelf_cvt_WORD_tom, + libelf_cvt_WORD_tof, + libelf_cvt_WORD_tom + }, + { 0, 0, + libelf_cvt_XWORD_tof, + libelf_cvt_XWORD_tom + }, + { libelf_cvt32_GNUHASH_tof, + libelf_cvt32_GNUHASH_tom, + libelf_cvt64_GNUHASH_tof, + libelf_cvt64_GNUHASH_tom + } +#endif +}; + +int (*_libelf_get_translator(Elf_Type t, int direction, int elfclass)) + (char *_dst, size_t dsz, char *_src, size_t _cnt, int _byteswap) +{ + assert(elfclass == ELFCLASS32 || elfclass == ELFCLASS64); + assert(direction == ELF_TOFILE || direction == ELF_TOMEMORY); + + if (t >= ELF_T_NUM || + (elfclass != ELFCLASS32 && elfclass != ELFCLASS64) || + (direction != ELF_TOFILE && direction != ELF_TOMEMORY)) + return (NULL); + + return ((elfclass == ELFCLASS32) ? + (direction == ELF_TOFILE ? cvt[t].tof32 : cvt[t].tom32) : + (direction == ELF_TOFILE ? cvt[t].tof64 : cvt[t].tom64)); +} diff --git a/rocclr/compiler/lib/loaders/elf/utils/libelf/libelf_data.c b/rocclr/compiler/lib/loaders/elf/utils/libelf/libelf_data.c new file mode 100644 index 0000000000..85039f0dba --- /dev/null +++ b/rocclr/compiler/lib/loaders/elf/utils/libelf/libelf_data.c @@ -0,0 +1,114 @@ +/*- + * Copyright (c) 2006,2008 Joseph Koshy + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include + +#include + +#include "_libelf.h" + +LIBELF_VCSID("$Id: libelf_data.c 1264 2010-11-12 14:53:23Z jkoshy $"); + +int +_libelf_xlate_shtype(uint32_t sht) +{ + switch (sht) { + case SHT_DYNAMIC: + return (ELF_T_DYN); + case SHT_DYNSYM: + return (ELF_T_SYM); +#if defined(SHT_FINI_ARRAY) + case SHT_FINI_ARRAY: + return (ELF_T_ADDR); +#endif +#if defined(SHT_GNU_HASH) + case SHT_GNU_HASH: + return (ELF_T_GNUHASH); +#endif +#if defined(SHT_GNU_LIBLIST) + case SHT_GNU_LIBLIST: + return (ELF_T_WORD); +#endif +#if defined(SHT_GROUP) + case SHT_GROUP: + return (ELF_T_WORD); +#endif + case SHT_HASH: + return (ELF_T_WORD); +#if defined(SHT_INIT_ARRAY) + case SHT_INIT_ARRAY: + return (ELF_T_ADDR); +#endif + case SHT_NOBITS: + return (ELF_T_BYTE); + case SHT_NOTE: + return (ELF_T_NOTE); +#if defined(SHT_PREINIT_ARRAY) + case SHT_PREINIT_ARRAY: + return (ELF_T_ADDR); +#endif + case SHT_PROGBITS: + return (ELF_T_BYTE); + case SHT_REL: + return (ELF_T_REL); + case SHT_RELA: + return (ELF_T_RELA); + case SHT_STRTAB: + return (ELF_T_BYTE); + case SHT_SYMTAB: + return (ELF_T_SYM); +#if defined(SHT_SYMTAB_SHNDX) + case SHT_SYMTAB_SHNDX: + return (ELF_T_WORD); +#endif +#if defined(SHT_SUNW_dof) + case SHT_SUNW_dof: + return (ELF_T_BYTE); +#endif +#if defined(SHT_SUNW_move) + case SHT_SUNW_move: + return (ELF_T_MOVE); +#endif +#if defined(SHT_SUNW_syminfo) + case SHT_SUNW_syminfo: + return (ELF_T_SYMINFO); +#endif +#if defined(SHT_SUNW_verdef) + case SHT_SUNW_verdef: /* == SHT_GNU_verdef */ + return (ELF_T_VDEF); +#endif +#if defined(SHT_SUNW_verneed) + case SHT_SUNW_verneed: /* == SHT_GNU_verneed */ + return (ELF_T_VNEED); +#endif +#if defined(SHT_SUNW_versym) + case SHT_SUNW_versym: /* == SHT_GNU_versym */ + return (ELF_T_HALF); +#endif + default: + return (-1); + } +} diff --git a/rocclr/compiler/lib/loaders/elf/utils/libelf/libelf_ehdr.c b/rocclr/compiler/lib/loaders/elf/utils/libelf/libelf_ehdr.c new file mode 100644 index 0000000000..966c6cdb1a --- /dev/null +++ b/rocclr/compiler/lib/loaders/elf/utils/libelf/libelf_ehdr.c @@ -0,0 +1,211 @@ +/*- + * Copyright (c) 2006,2008 Joseph Koshy + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include + +#include +#include +#include +#include +#include + +#include "_libelf.h" + +LIBELF_VCSID("$Id: libelf_ehdr.c 1677 2011-07-28 04:35:53Z jkoshy $"); + +/* + * Retrieve counts for sections, phdrs and the section string table index + * from section header #0 of the ELF object. + */ +static int +_libelf_load_extended(Elf *e, int ec, uint64_t shoff, uint16_t phnum, + uint16_t strndx) +{ + Elf_Scn *scn; + size_t fsz; + int (*xlator)(char *_d, size_t _dsz, char *_s, size_t _c, int _swap); + uint32_t shtype; + + assert(STAILQ_EMPTY(&e->e_u.e_elf.e_scn)); + + fsz = _libelf_fsize(ELF_T_SHDR, ec, e->e_version, 1); + assert(fsz > 0); + + if (e->e_rawsize < shoff + fsz) { /* raw file too small */ + LIBELF_SET_ERROR(HEADER, 0); + return (0); + } + + if ((scn = _libelf_allocate_scn(e, (size_t) 0)) == NULL) + return (0); + + xlator = _libelf_get_translator(ELF_T_SHDR, ELF_TOMEMORY, ec); + (*xlator)((char *) &scn->s_shdr, sizeof(scn->s_shdr), + e->e_rawfile + shoff, (size_t) 1, + e->e_byteorder != LIBELF_PRIVATE(byteorder)); + +#define GET_SHDR_MEMBER(M) ((ec == ELFCLASS32) ? scn->s_shdr.s_shdr32.M : \ + scn->s_shdr.s_shdr64.M) + + if ((shtype = GET_SHDR_MEMBER(sh_type)) != SHT_NULL) { + LIBELF_SET_ERROR(SECTION, 0); + return (0); + } + + e->e_u.e_elf.e_nscn = GET_SHDR_MEMBER(sh_size); + e->e_u.e_elf.e_nphdr = (phnum != PN_XNUM) ? phnum : + GET_SHDR_MEMBER(sh_info); + e->e_u.e_elf.e_strndx = (strndx != SHN_XINDEX) ? strndx : + GET_SHDR_MEMBER(sh_link); +#undef GET_SHDR_MEMBER + + return (1); +} + +#define EHDR_INIT(E,SZ) do { \ + Elf##SZ##_Ehdr *eh = (E); \ + eh->e_ident[EI_MAG0] = ELFMAG0; \ + eh->e_ident[EI_MAG1] = ELFMAG1; \ + eh->e_ident[EI_MAG2] = ELFMAG2; \ + eh->e_ident[EI_MAG3] = ELFMAG3; \ + eh->e_ident[EI_CLASS] = ELFCLASS##SZ; \ + eh->e_ident[EI_DATA] = ELFDATANONE; \ + eh->e_ident[EI_VERSION] = LIBELF_PRIVATE(version); \ + eh->e_machine = EM_NONE; \ + eh->e_type = ELF_K_NONE; \ + eh->e_version = LIBELF_PRIVATE(version); \ + } while (0) + +void * +_libelf_ehdr(Elf *e, int ec, int allocate) +{ + void *ehdr; + size_t fsz, msz; + uint16_t phnum, shnum, strndx; + uint64_t shoff; + int (*xlator)(char *_d, size_t _dsz, char *_s, size_t _c, int _swap); + + assert(ec == ELFCLASS32 || ec == ELFCLASS64); + + if (e == NULL || e->e_kind != ELF_K_ELF) { + LIBELF_SET_ERROR(ARGUMENT, 0); + return (NULL); + } + + if (e->e_class != ELFCLASSNONE && e->e_class != ec) { + LIBELF_SET_ERROR(CLASS, 0); + return (NULL); + } + + if (e->e_version != EV_CURRENT) { + LIBELF_SET_ERROR(VERSION, 0); + return (NULL); + } + + if (e->e_class == ELFCLASSNONE) + e->e_class = ec; + + if (ec == ELFCLASS32) + ehdr = (void *) e->e_u.e_elf.e_ehdr.e_ehdr32; + else + ehdr = (void *) e->e_u.e_elf.e_ehdr.e_ehdr64; + + if (ehdr != NULL) /* already have a translated ehdr */ + return (ehdr); + + fsz = _libelf_fsize(ELF_T_EHDR, ec, e->e_version, (size_t) 1); + assert(fsz > 0); + + // If we have a file that is attached to a read/write elf + // or the elf is a read only elf and the size is smaller than the + // file, then error. + if ((e->e_cmd == ELF_C_READ || (e->e_cmd == ELF_C_RDWR && e->e_rawfile)) && e->e_rawsize < fsz) { + LIBELF_SET_ERROR(HEADER, 0); + return (NULL); + } + + msz = _libelf_msize(ELF_T_EHDR, ec, EV_CURRENT); + + assert(msz > 0); + + if ((ehdr = e->e_mem.alloc(msz)) == NULL) { + LIBELF_SET_ERROR(RESOURCE, 0); + return (NULL); + } + memset(ehdr, 0, msz); + + if (ec == ELFCLASS32) { + e->e_u.e_elf.e_ehdr.e_ehdr32 = ehdr; + EHDR_INIT(ehdr,32); + } else { + e->e_u.e_elf.e_ehdr.e_ehdr64 = ehdr; + EHDR_INIT(ehdr,64); + } + + if (allocate) + e->e_flags |= ELF_F_DIRTY; + + // If there is no raw file, return as we are done and don't need to + // process the file on the disk. + if (!e->e_rawfile) + return (ehdr); + + xlator = _libelf_get_translator(ELF_T_EHDR, ELF_TOMEMORY, ec); + (*xlator)(ehdr, msz, e->e_rawfile, (size_t) 1, + e->e_byteorder != LIBELF_PRIVATE(byteorder)); + + /* + * If extended numbering is being used, read the correct + * number of sections and program header entries. + */ + if (ec == ELFCLASS32) { + phnum = ((Elf32_Ehdr *) ehdr)->e_phnum; + shnum = ((Elf32_Ehdr *) ehdr)->e_shnum; + shoff = ((Elf32_Ehdr *) ehdr)->e_shoff; + strndx = ((Elf32_Ehdr *) ehdr)->e_shstrndx; + } else { + phnum = ((Elf64_Ehdr *) ehdr)->e_phnum; + shnum = ((Elf64_Ehdr *) ehdr)->e_shnum; + shoff = ((Elf64_Ehdr *) ehdr)->e_shoff; + strndx = ((Elf64_Ehdr *) ehdr)->e_shstrndx; + } + + if (shnum >= SHN_LORESERVE || + (shoff == 0LL && (shnum != 0 || phnum == PN_XNUM || + strndx == SHN_XINDEX))) { + LIBELF_SET_ERROR(HEADER, 0); + return (NULL); + } + + if (shnum != 0 || shoff == 0LL) { /* not using extended numbering */ + e->e_u.e_elf.e_nphdr = phnum; + e->e_u.e_elf.e_nscn = shnum; + e->e_u.e_elf.e_strndx = strndx; + } else if (_libelf_load_extended(e, ec, shoff, phnum, strndx) == 0) + return (NULL); + + return (ehdr); +} diff --git a/rocclr/compiler/lib/loaders/elf/utils/libelf/libelf_extended.c b/rocclr/compiler/lib/loaders/elf/utils/libelf/libelf_extended.c new file mode 100644 index 0000000000..10590bb845 --- /dev/null +++ b/rocclr/compiler/lib/loaders/elf/utils/libelf/libelf_extended.c @@ -0,0 +1,136 @@ +/*- + * Copyright (c) 2006,2008 Joseph Koshy + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include + +#include +#include + +#include "_libelf.h" + +LIBELF_VCSID("$Id: libelf_extended.c 1360 2011-01-08 08:27:41Z jkoshy $"); + +/* + * Retrieve section #0, allocating a new section if needed. + */ +static Elf_Scn * +_libelf_getscn0(Elf *e) +{ + Elf_Scn *s; + + if ((s = STAILQ_FIRST(&e->e_u.e_elf.e_scn)) != NULL) + return (s); + + return (_libelf_allocate_scn(e, (size_t) SHN_UNDEF)); +} + +int +_libelf_setshnum(Elf *e, void *eh, int ec, size_t shnum) +{ + Elf_Scn *scn; + + if (shnum >= SHN_LORESERVE) { + if ((scn = _libelf_getscn0(e)) == NULL) + return (0); + + assert(scn->s_ndx == SHN_UNDEF); + + if (ec == ELFCLASS32) + scn->s_shdr.s_shdr32.sh_size = shnum; + else + scn->s_shdr.s_shdr64.sh_size = shnum; + + (void) elf_flagshdr(scn, ELF_C_SET, ELF_F_DIRTY); + + shnum = 0; + } + + if (ec == ELFCLASS32) + ((Elf32_Ehdr *) eh)->e_shnum = shnum; + else + ((Elf64_Ehdr *) eh)->e_shnum = shnum; + + + return (1); +} + +int +_libelf_setshstrndx(Elf *e, void *eh, int ec, size_t shstrndx) +{ + Elf_Scn *scn; + + if (shstrndx >= SHN_LORESERVE) { + if ((scn = _libelf_getscn0(e)) == NULL) + return (0); + + assert(scn->s_ndx == SHN_UNDEF); + + if (ec == ELFCLASS32) + scn->s_shdr.s_shdr32.sh_link = shstrndx; + else + scn->s_shdr.s_shdr64.sh_link = shstrndx; + + (void) elf_flagshdr(scn, ELF_C_SET, ELF_F_DIRTY); + + shstrndx = SHN_XINDEX; + } + + if (ec == ELFCLASS32) + ((Elf32_Ehdr *) eh)->e_shstrndx = shstrndx; + else + ((Elf64_Ehdr *) eh)->e_shstrndx = shstrndx; + + return (1); +} + +int +_libelf_setphnum(Elf *e, void *eh, int ec, size_t phnum) +{ + Elf_Scn *scn; + + if (phnum >= PN_XNUM) { + if ((scn = _libelf_getscn0(e)) == NULL) + return (0); + + assert(scn->s_ndx == SHN_UNDEF); + + if (ec == ELFCLASS32) + scn->s_shdr.s_shdr32.sh_info = phnum; + else + scn->s_shdr.s_shdr64.sh_info = phnum; + + (void) elf_flagshdr(scn, ELF_C_SET, ELF_F_DIRTY); + + phnum = PN_XNUM; + } + + if (ec == ELFCLASS32) + ((Elf32_Ehdr *) eh)->e_phnum = phnum; + else + ((Elf64_Ehdr *) eh)->e_phnum = phnum; + + return (1); +} diff --git a/rocclr/compiler/lib/loaders/elf/utils/libelf/libelf_fsize.c b/rocclr/compiler/lib/loaders/elf/utils/libelf/libelf_fsize.c new file mode 100644 index 0000000000..9c6006080a --- /dev/null +++ b/rocclr/compiler/lib/loaders/elf/utils/libelf/libelf_fsize.c @@ -0,0 +1,149 @@ +/*- + * Copyright (c) 2006,2008 Joseph Koshy + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS `AS IS' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include + +#include "_libelf.h" + +LIBELF_VCSID("$Id: libelf_fsize.m4 320 2009-03-07 16:37:53Z jkoshy $"); + +/* WARNING: GENERATED FROM libelf_fsize.m4. */ + +/* + * Create an array of file sizes from the elf_type definitions + */ + + + +struct fsize { + size_t fsz32; + size_t fsz64; +}; + +static struct fsize fsize[ELF_T_NUM] = { +#if defined(__GNUC__) +#if LIBELF_CONFIG_ADDR + [ELF_T_ADDR] = { .fsz32 = sizeof(Elf32_Addr), .fsz64 = sizeof(Elf64_Addr) }, +#endif +#if LIBELF_CONFIG_BYTE + [ELF_T_BYTE] = { .fsz32 = 1, .fsz64 = 1 }, +#endif +#if LIBELF_CONFIG_CAP + [ELF_T_CAP] = { .fsz32 = sizeof(Elf32_Word)+sizeof(Elf32_Word)+0, .fsz64 = sizeof(Elf64_Xword)+sizeof(Elf64_Xword)+0 }, +#endif +#if LIBELF_CONFIG_DYN + [ELF_T_DYN] = { .fsz32 = sizeof(Elf32_Sword)+sizeof(Elf32_Word)+0, .fsz64 = sizeof(Elf64_Sxword)+sizeof(Elf64_Xword)+0 }, +#endif +#if LIBELF_CONFIG_EHDR + [ELF_T_EHDR] = { .fsz32 = EI_NIDENT+sizeof(Elf32_Half)+sizeof(Elf32_Half)+sizeof(Elf32_Word)+sizeof(Elf32_Addr)+sizeof(Elf32_Off)+sizeof(Elf32_Off)+sizeof(Elf32_Word)+sizeof(Elf32_Half)+sizeof(Elf32_Half)+sizeof(Elf32_Half)+sizeof(Elf32_Half)+sizeof(Elf32_Half)+sizeof(Elf32_Half)+0, .fsz64 = EI_NIDENT+sizeof(Elf64_Half)+sizeof(Elf64_Half)+sizeof(Elf64_Word)+sizeof(Elf64_Addr)+sizeof(Elf64_Off)+sizeof(Elf64_Off)+sizeof(Elf64_Word)+sizeof(Elf64_Half)+sizeof(Elf64_Half)+sizeof(Elf64_Half)+sizeof(Elf64_Half)+sizeof(Elf64_Half)+sizeof(Elf64_Half)+0 }, +#endif +#if LIBELF_CONFIG_GNUHASH + [ELF_T_GNUHASH] = { .fsz32 = 1, .fsz64 = 1 }, +#endif +#if LIBELF_CONFIG_HALF + [ELF_T_HALF] = { .fsz32 = sizeof(Elf32_Half), .fsz64 = sizeof(Elf64_Half) }, +#endif +#if LIBELF_CONFIG_LWORD + [ELF_T_LWORD] = { .fsz32 = sizeof(Elf32_Lword), .fsz64 = sizeof(Elf64_Lword) }, +#endif +#if LIBELF_CONFIG_MOVE + [ELF_T_MOVE] = { .fsz32 = sizeof(Elf32_Lword)+sizeof(Elf32_Word)+sizeof(Elf32_Word)+sizeof(Elf32_Half)+sizeof(Elf32_Half)+0, .fsz64 = sizeof(Elf64_Lword)+sizeof(Elf64_Xword)+sizeof(Elf64_Xword)+sizeof(Elf64_Half)+sizeof(Elf64_Half)+0 }, +#endif +#if LIBELF_CONFIG_MOVEP + [ELF_T_MOVEP] = { .fsz32 = 0, .fsz64 = 0 }, +#endif +#if LIBELF_CONFIG_NOTE + [ELF_T_NOTE] = { .fsz32 = 1, .fsz64 = 1 }, +#endif +#if LIBELF_CONFIG_OFF + [ELF_T_OFF] = { .fsz32 = sizeof(Elf32_Off), .fsz64 = sizeof(Elf64_Off) }, +#endif +#if LIBELF_CONFIG_PHDR + [ELF_T_PHDR] = { .fsz32 = sizeof(Elf32_Word)+sizeof(Elf32_Off)+sizeof(Elf32_Addr)+sizeof(Elf32_Addr)+sizeof(Elf32_Word)+sizeof(Elf32_Word)+sizeof(Elf32_Word)+sizeof(Elf32_Word)+0, .fsz64 = sizeof(Elf64_Word)+sizeof(Elf64_Word)+sizeof(Elf64_Off)+sizeof(Elf64_Addr)+sizeof(Elf64_Addr)+sizeof(Elf64_Xword)+sizeof(Elf64_Xword)+sizeof(Elf64_Xword)+0 }, +#endif +#if LIBELF_CONFIG_REL + [ELF_T_REL] = { .fsz32 = sizeof(Elf32_Addr)+sizeof(Elf32_Word)+0, .fsz64 = sizeof(Elf64_Addr)+sizeof(Elf64_Xword)+0 }, +#endif +#if LIBELF_CONFIG_RELA + [ELF_T_RELA] = { .fsz32 = sizeof(Elf32_Addr)+sizeof(Elf32_Word)+sizeof(Elf32_Sword)+0, .fsz64 = sizeof(Elf64_Addr)+sizeof(Elf64_Xword)+sizeof(Elf64_Sxword)+0 }, +#endif +#if LIBELF_CONFIG_SHDR + [ELF_T_SHDR] = { .fsz32 = sizeof(Elf32_Word)+sizeof(Elf32_Word)+sizeof(Elf32_Word)+sizeof(Elf32_Addr)+sizeof(Elf32_Off)+sizeof(Elf32_Word)+sizeof(Elf32_Word)+sizeof(Elf32_Word)+sizeof(Elf32_Word)+sizeof(Elf32_Word)+0, .fsz64 = sizeof(Elf64_Word)+sizeof(Elf64_Word)+sizeof(Elf64_Xword)+sizeof(Elf64_Addr)+sizeof(Elf64_Off)+sizeof(Elf64_Xword)+sizeof(Elf64_Word)+sizeof(Elf64_Word)+sizeof(Elf64_Xword)+sizeof(Elf64_Xword)+0 }, +#endif +#if LIBELF_CONFIG_SWORD + [ELF_T_SWORD] = { .fsz32 = sizeof(Elf32_Sword), .fsz64 = sizeof(Elf64_Sword) }, +#endif +#if LIBELF_CONFIG_SXWORD + [ELF_T_SXWORD] = { .fsz32 = 0, .fsz64 = sizeof(Elf64_Sxword) }, +#endif +#if LIBELF_CONFIG_SYMINFO + [ELF_T_SYMINFO] = { .fsz32 = sizeof(Elf32_Half)+sizeof(Elf32_Half)+0, .fsz64 = sizeof(Elf64_Half)+sizeof(Elf64_Half)+0 }, +#endif +#if LIBELF_CONFIG_SYM + [ELF_T_SYM] = { .fsz32 = sizeof(Elf32_Word)+sizeof(Elf32_Addr)+sizeof(Elf32_Word)+1+1+sizeof(Elf32_Half)+0, .fsz64 = sizeof(Elf64_Word)+1+1+sizeof(Elf64_Half)+sizeof(Elf64_Addr)+sizeof(Elf64_Xword)+0 }, +#endif +#if LIBELF_CONFIG_VDEF + [ELF_T_VDEF] = { .fsz32 = sizeof(Elf32_Half)+sizeof(Elf32_Half)+sizeof(Elf32_Half)+sizeof(Elf32_Half)+sizeof(Elf32_Word)+sizeof(Elf32_Word)+sizeof(Elf32_Word)+0, .fsz64 = sizeof(Elf64_Half)+sizeof(Elf64_Half)+sizeof(Elf64_Half)+sizeof(Elf64_Half)+sizeof(Elf64_Word)+sizeof(Elf64_Word)+sizeof(Elf64_Word)+0 }, +#endif +#if LIBELF_CONFIG_VNEED + [ELF_T_VNEED] = { .fsz32 = sizeof(Elf32_Half)+sizeof(Elf32_Half)+sizeof(Elf32_Word)+sizeof(Elf32_Word)+sizeof(Elf32_Word)+0, .fsz64 = sizeof(Elf64_Half)+sizeof(Elf64_Half)+sizeof(Elf64_Word)+sizeof(Elf64_Word)+sizeof(Elf64_Word)+0 }, +#endif +#if LIBELF_CONFIG_WORD + [ELF_T_WORD] = { .fsz32 = sizeof(Elf32_Word), .fsz64 = sizeof(Elf64_Word) }, +#endif +#if LIBELF_CONFIG_XWORD + [ELF_T_XWORD] = { .fsz32 = 0, .fsz64 = sizeof(Elf64_Xword) }, +#endif +#elif defined(_MSC_VER) + {4, 8}, {1, 1}, {0, 0}, {8, 16}, {52, 64}, + {2, 2}, {0, 0}, {0, 0}, {0, 0}, {1, 1}, + {4, 8}, {32, 56}, {8, 16}, {12, 24}, {40, 64}, + {4, 4}, {0, 8}, {0, 0}, {16, 24}, {20, 20}, + {16, 16}, {4, 4}, {0, 8}, {1, 1} +#else +#error +#endif +}; + +size_t +_libelf_fsize(Elf_Type t, int ec, unsigned int v, size_t c) +{ + size_t sz; + + sz = 0; + if (v != EV_CURRENT) + LIBELF_SET_ERROR(VERSION, 0); + else if ((int) t < ELF_T_FIRST || t > ELF_T_LAST) + LIBELF_SET_ERROR(ARGUMENT, 0); + else { + sz = ec == ELFCLASS64 ? fsize[t].fsz64 : fsize[t].fsz32; + if (sz == 0) + LIBELF_SET_ERROR(UNIMPL, 0); + } + + return (sz*c); +} + diff --git a/rocclr/compiler/lib/loaders/elf/utils/libelf/libelf_msize.c b/rocclr/compiler/lib/loaders/elf/utils/libelf/libelf_msize.c new file mode 100644 index 0000000000..5c56d57efe --- /dev/null +++ b/rocclr/compiler/lib/loaders/elf/utils/libelf/libelf_msize.c @@ -0,0 +1,147 @@ +/*- + * Copyright (c) 2006,2008 Joseph Koshy + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS `AS IS' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include + +#include +#include +#include + +#include "_libelf.h" + +LIBELF_VCSID("$Id: libelf_msize.m4 311 2009-02-26 16:46:31Z jkoshy $"); + +/* WARNING: GENERATED FROM libelf_msize.m4. */ + +struct msize { + size_t msz32; + size_t msz64; +}; + + + +static struct msize msize[ELF_T_NUM] = { +#if defined(__GNUC__) +#if LIBELF_CONFIG_ADDR + [ELF_T_ADDR] = { .msz32 = sizeof(Elf32_Addr), .msz64 = sizeof(Elf64_Addr) }, +#endif +#if LIBELF_CONFIG_BYTE + [ELF_T_BYTE] = { .msz32 = 1, .msz64 = 1 }, +#endif +#if LIBELF_CONFIG_CAP + [ELF_T_CAP] = { .msz32 = sizeof(Elf32_Cap), .msz64 = sizeof(Elf64_Cap) }, +#endif +#if LIBELF_CONFIG_DYN + [ELF_T_DYN] = { .msz32 = sizeof(Elf32_Dyn), .msz64 = sizeof(Elf64_Dyn) }, +#endif +#if LIBELF_CONFIG_EHDR + [ELF_T_EHDR] = { .msz32 = sizeof(Elf32_Ehdr), .msz64 = sizeof(Elf64_Ehdr) }, +#endif +#if LIBELF_CONFIG_GNUHASH + [ELF_T_GNUHASH] = { .msz32 = 1, .msz64 = 1 }, +#endif +#if LIBELF_CONFIG_HALF + [ELF_T_HALF] = { .msz32 = sizeof(Elf32_Half), .msz64 = sizeof(Elf64_Half) }, +#endif +#if LIBELF_CONFIG_LWORD + [ELF_T_LWORD] = { .msz32 = sizeof(Elf32_Lword), .msz64 = sizeof(Elf64_Lword) }, +#endif +#if LIBELF_CONFIG_MOVE + [ELF_T_MOVE] = { .msz32 = sizeof(Elf32_Move), .msz64 = sizeof(Elf64_Move) }, +#endif +#if LIBELF_CONFIG_MOVEP + [ELF_T_MOVEP] = { .msz32 = 0, .msz64 = 0 }, +#endif +#if LIBELF_CONFIG_NOTE + [ELF_T_NOTE] = { .msz32 = 1, .msz64 = 1 }, +#endif +#if LIBELF_CONFIG_OFF + [ELF_T_OFF] = { .msz32 = sizeof(Elf32_Off), .msz64 = sizeof(Elf64_Off) }, +#endif +#if LIBELF_CONFIG_PHDR + [ELF_T_PHDR] = { .msz32 = sizeof(Elf32_Phdr), .msz64 = sizeof(Elf64_Phdr) }, +#endif +#if LIBELF_CONFIG_REL + [ELF_T_REL] = { .msz32 = sizeof(Elf32_Rel), .msz64 = sizeof(Elf64_Rel) }, +#endif +#if LIBELF_CONFIG_RELA + [ELF_T_RELA] = { .msz32 = sizeof(Elf32_Rela), .msz64 = sizeof(Elf64_Rela) }, +#endif +#if LIBELF_CONFIG_SHDR + [ELF_T_SHDR] = { .msz32 = sizeof(Elf32_Shdr), .msz64 = sizeof(Elf64_Shdr) }, +#endif +#if LIBELF_CONFIG_SWORD + [ELF_T_SWORD] = { .msz32 = sizeof(Elf32_Sword), .msz64 = sizeof(Elf64_Sword) }, +#endif +#if LIBELF_CONFIG_SXWORD + [ELF_T_SXWORD] = { .msz32 = 0, .msz64 = sizeof(Elf64_Sxword) }, +#endif +#if LIBELF_CONFIG_SYMINFO + [ELF_T_SYMINFO] = { .msz32 = sizeof(Elf32_Syminfo), .msz64 = sizeof(Elf64_Syminfo) }, +#endif +#if LIBELF_CONFIG_SYM + [ELF_T_SYM] = { .msz32 = sizeof(Elf32_Sym), .msz64 = sizeof(Elf64_Sym) }, +#endif +#if LIBELF_CONFIG_VDEF + [ELF_T_VDEF] = { .msz32 = sizeof(Elf32_Verdef), .msz64 = sizeof(Elf64_Verdef) }, +#endif +#if LIBELF_CONFIG_VNEED + [ELF_T_VNEED] = { .msz32 = sizeof(Elf32_Verneed), .msz64 = sizeof(Elf64_Verneed) }, +#endif +#if LIBELF_CONFIG_WORD + [ELF_T_WORD] = { .msz32 = sizeof(Elf32_Word), .msz64 = sizeof(Elf64_Word) }, +#endif +#if LIBELF_CONFIG_XWORD + [ELF_T_XWORD] = { .msz32 = 0, .msz64 = sizeof(Elf64_Xword) }, +#endif +#elif defined(_MSC_VER) + {4, 8}, {1, 1}, {0, 0}, {8, 16}, {52, 64}, + {2, 2}, {0, 0}, {0, 0}, {0, 0}, {1, 1}, + {4, 8}, {32, 56}, {8, 16}, {12, 24}, {40, 64}, + {4, 4}, {0, 8}, {0, 0}, {16, 24}, {20, 20}, + {16, 16}, {4, 4}, {0, 8}, {1, 1} +#else +#error +#endif +}; + +size_t +_libelf_msize(Elf_Type t, int elfclass, unsigned int version) +{ + size_t sz; + + assert(elfclass == ELFCLASS32 || elfclass == ELFCLASS64); + assert((signed) t >= ELF_T_FIRST && t <= ELF_T_LAST); + + if (version != EV_CURRENT) { + LIBELF_SET_ERROR(VERSION, 0); + return (0); + } + + sz = (elfclass == ELFCLASS32) ? msize[t].msz32 : msize[t].msz64; + + return (sz); +} diff --git a/rocclr/compiler/lib/loaders/elf/utils/libelf/libelf_phdr.c b/rocclr/compiler/lib/loaders/elf/utils/libelf/libelf_phdr.c new file mode 100644 index 0000000000..a3111dc074 --- /dev/null +++ b/rocclr/compiler/lib/loaders/elf/utils/libelf/libelf_phdr.c @@ -0,0 +1,161 @@ +/*- + * Copyright (c) 2006,2008 Joseph Koshy + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include + +#include +#include +#include +#include +#include + +#include "_libelf.h" + +LIBELF_VCSID("$Id: libelf_phdr.c 1677 2011-07-28 04:35:53Z jkoshy $"); + +void * +_libelf_getphdr(Elf *e, int ec) +{ + size_t phnum, phentsize; + size_t fsz, msz; + uint64_t phoff; + Elf32_Ehdr *eh32; + Elf64_Ehdr *eh64; + void *ehdr, *phdr; + int (*xlator)(char *_d, size_t _dsz, char *_s, size_t _c, int _swap); + + assert(ec == ELFCLASS32 || ec == ELFCLASS64); + + if (e == NULL) { + LIBELF_SET_ERROR(ARGUMENT, 0); + return (NULL); + } + + if ((phdr = (ec == ELFCLASS32 ? + (void *) e->e_u.e_elf.e_phdr.e_phdr32 : + (void *) e->e_u.e_elf.e_phdr.e_phdr64)) != NULL) + return (phdr); + + /* + * Check the PHDR related fields in the EHDR for sanity. + */ + + if ((ehdr = _libelf_ehdr(e, ec, 0)) == NULL) + return (NULL); + + phnum = e->e_u.e_elf.e_nphdr; + + if (ec == ELFCLASS32) { + eh32 = (Elf32_Ehdr *) ehdr; + phentsize = eh32->e_phentsize; + phoff = (uint64_t) eh32->e_phoff; + } else { + eh64 = (Elf64_Ehdr *) ehdr; + phentsize = eh64->e_phentsize; + phoff = (uint64_t) eh64->e_phoff; + } + + fsz = gelf_fsize(e, ELF_T_PHDR, phnum, e->e_version); + + assert(fsz > 0); + + if ((uint64_t) e->e_rawsize < (phoff + fsz)) { + LIBELF_SET_ERROR(HEADER, 0); + return (NULL); + } + + msz = _libelf_msize(ELF_T_PHDR, ec, EV_CURRENT); + + assert(msz > 0); + + if ((phdr = e->e_mem.alloc(phnum * msz)) == NULL) { + LIBELF_SET_ERROR(RESOURCE, 0); + return (NULL); + } + memset(phdr, 0, msz); + + if (ec == ELFCLASS32) + e->e_u.e_elf.e_phdr.e_phdr32 = phdr; + else + e->e_u.e_elf.e_phdr.e_phdr64 = phdr; + + + xlator = _libelf_get_translator(ELF_T_PHDR, ELF_TOMEMORY, ec); + (*xlator)(phdr, phnum * msz, e->e_rawfile + phoff, phnum, + e->e_byteorder != LIBELF_PRIVATE(byteorder)); + + return (phdr); +} + +void * +_libelf_newphdr(Elf *e, int ec, size_t count) +{ + void *ehdr, *newphdr, *oldphdr; + size_t msz; + + if (e == NULL) { + LIBELF_SET_ERROR(ARGUMENT, 0); + return (NULL); + } + + if ((ehdr = _libelf_ehdr(e, ec, 0)) == NULL) { + LIBELF_SET_ERROR(SEQUENCE, 0); + return (NULL); + } + + assert(e->e_class == ec); + assert(ec == ELFCLASS32 || ec == ELFCLASS64); + assert(e->e_version == EV_CURRENT); + + msz = _libelf_msize(ELF_T_PHDR, ec, e->e_version); + + assert(msz > 0); + + newphdr = NULL; + if (count > 0 && (newphdr = e->e_mem.alloc(count * msz)) == NULL) { + LIBELF_SET_ERROR(RESOURCE, 0); + return (NULL); + } + if (count > 0) { + memset(newphdr, 0, count * msz); + } + + if (ec == ELFCLASS32) { + if ((oldphdr = (void *) e->e_u.e_elf.e_phdr.e_phdr32) != NULL) + e->e_mem.dealloc(oldphdr); + e->e_u.e_elf.e_phdr.e_phdr32 = (Elf32_Phdr *) newphdr; + } else { + if ((oldphdr = (void *) e->e_u.e_elf.e_phdr.e_phdr64) != NULL) + e->e_mem.dealloc(oldphdr); + e->e_u.e_elf.e_phdr.e_phdr64 = (Elf64_Phdr *) newphdr; + } + + e->e_u.e_elf.e_nphdr = count; + + elf_flagphdr(e, ELF_C_SET, ELF_F_DIRTY); + + return (newphdr); +} diff --git a/rocclr/compiler/lib/loaders/elf/utils/libelf/libelf_shdr.c b/rocclr/compiler/lib/loaders/elf/utils/libelf/libelf_shdr.c new file mode 100644 index 0000000000..a696cefc76 --- /dev/null +++ b/rocclr/compiler/lib/loaders/elf/utils/libelf/libelf_shdr.c @@ -0,0 +1,56 @@ +/*- + * Copyright (c) 2006,2008 Joseph Koshy + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include + +#include +#include + +#include "_libelf.h" + +LIBELF_VCSID("$Id: libelf_shdr.c 189 2008-07-20 10:38:08Z jkoshy $"); + +void * +_libelf_getshdr(Elf_Scn *s, int ec) +{ + Elf *e; + + if (s == NULL || (e = s->s_elf) == NULL || + e->e_kind != ELF_K_ELF) { + LIBELF_SET_ERROR(ARGUMENT, 0); + return (NULL); + } + + if (ec == ELFCLASSNONE) + ec = e->e_class; + + if (ec != e->e_class) { + LIBELF_SET_ERROR(CLASS, 0); + return (NULL); + } + + return ((void *) &s->s_shdr); +} diff --git a/rocclr/compiler/lib/loaders/elf/utils/libelf/libelf_xlate.c b/rocclr/compiler/lib/loaders/elf/utils/libelf/libelf_xlate.c new file mode 100644 index 0000000000..ace4e09363 --- /dev/null +++ b/rocclr/compiler/lib/loaders/elf/utils/libelf/libelf_xlate.c @@ -0,0 +1,150 @@ +/*- + * Copyright (c) 2006,2008 Joseph Koshy + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include + +#include +#include + +#include "_libelf.h" + +LIBELF_VCSID("$Id: libelf_xlate.c 316 2009-02-28 16:08:44Z jkoshy $"); + +/* + * Translate to/from the file representation of ELF objects. + * + * Translation could potentially involve the following + * transformations: + * + * - an endianness conversion, + * - a change of layout, as the file representation of ELF objects + * can differ from their in-memory representation. + * - a change in representation due to a layout version change. + */ + +Elf_Data * +_libelf_xlate(Elf_Data *dst, const Elf_Data *src, unsigned int encoding, + int elfclass, int direction) +{ + int byteswap; + size_t cnt, dsz, fsz, msz; + uintptr_t sb, se, db, de; + + if (encoding == ELFDATANONE) + encoding = LIBELF_PRIVATE(byteorder); + + if ((encoding != ELFDATA2LSB && encoding != ELFDATA2MSB) || + dst == NULL || src == NULL || dst == src) { + LIBELF_SET_ERROR(ARGUMENT, 0); + return (NULL); + } + + assert(elfclass == ELFCLASS32 || elfclass == ELFCLASS64); + assert(direction == ELF_TOFILE || direction == ELF_TOMEMORY); + + if (dst->d_version != src->d_version) { + LIBELF_SET_ERROR(UNIMPL, 0); + return (NULL); + } + + if (src->d_buf == NULL || dst->d_buf == NULL) { + LIBELF_SET_ERROR(DATA, 0); + return (NULL); + } + + if ((int) src->d_type < 0 || src->d_type >= ELF_T_NUM) { + LIBELF_SET_ERROR(DATA, 0); + return (NULL); + } + + if ((fsz = (elfclass == ELFCLASS32 ? elf32_fsize : elf64_fsize) + (src->d_type, (size_t) 1, src->d_version)) == 0) + return (NULL); + + msz = _libelf_msize(src->d_type, elfclass, src->d_version); + + assert(msz > 0); + + if (src->d_size % (direction == ELF_TOMEMORY ? fsz : msz)) { + LIBELF_SET_ERROR(DATA, 0); + return (NULL); + } + + /* + * Determine the number of objects that need to be converted, and + * the space required for the converted objects in the destination + * buffer. + */ + if (direction == ELF_TOMEMORY) { + cnt = src->d_size / fsz; + dsz = cnt * msz; + } else { + cnt = src->d_size / msz; + dsz = cnt * fsz; + } + + if (dst->d_size < dsz) { + LIBELF_SET_ERROR(DATA, 0); + return (NULL); + } + + sb = (uintptr_t) src->d_buf; + se = sb + src->d_size; + db = (uintptr_t) dst->d_buf; + de = db + dst->d_size; + + /* + * Check for overlapping buffers. Note that db == sb is + * allowed. + */ + if (db != sb && de > sb && se > db) { + LIBELF_SET_ERROR(DATA, 0); + return (NULL); + } + + if ((direction == ELF_TOMEMORY ? db : sb) % + _libelf_malign(src->d_type, elfclass)) { + LIBELF_SET_ERROR(DATA, 0); + return (NULL); + } + + dst->d_type = src->d_type; + dst->d_size = dsz; + + byteswap = encoding != LIBELF_PRIVATE(byteorder); + + if (src->d_size == 0 || + (db == sb && !byteswap && fsz == msz)) + return (dst); /* nothing more to do */ + + if (!(_libelf_get_translator(src->d_type, direction, elfclass)) + (dst->d_buf, dsz, src->d_buf, cnt, byteswap)) { + LIBELF_SET_ERROR(DATA, 0); + return (NULL); + } + + return (dst); +} diff --git a/rocclr/compiler/lib/loaders/elf/utils/libelf/roundup.h b/rocclr/compiler/lib/loaders/elf/utils/libelf/roundup.h new file mode 100644 index 0000000000..8fce8222f1 --- /dev/null +++ b/rocclr/compiler/lib/loaders/elf/utils/libelf/roundup.h @@ -0,0 +1,8 @@ +#ifdef __GNUC__ +# define roundup(x, y) (__builtin_constant_p (y) && powerof2 (y) \ + ? (((x) + (y) - 1) & ~((y) - 1)) \ + : ((((x) + ((y) - 1)) / (y)) * (y))) +#else +# define roundup(x, y) ((((x) + ((y) - 1)) / (y)) * (y)) +#endif + diff --git a/rocclr/compiler/lib/promotions/oclutils/os/alloc.cpp b/rocclr/compiler/lib/promotions/oclutils/os/alloc.cpp new file mode 100644 index 0000000000..8751e80635 --- /dev/null +++ b/rocclr/compiler/lib/promotions/oclutils/os/alloc.cpp @@ -0,0 +1,80 @@ +// +// Copyright (c) 2008 Advanced Micro Devices, Inc. All rights reserved. +// + +#include "os/alloc.hpp" +#include "os/os.hpp" +#include "utils/util.hpp" + +#include + +namespace amd { + +void* +AlignedMemory::allocate(size_t size, size_t alignment) +{ + return Os::alignedMalloc(size, alignment); +} + +void* +GuardedMemory::allocate(size_t size, size_t alignment, size_t guardSize) +{ + size_t sizeToAllocate = guardSize + alignment; + sizeToAllocate += size + guardSize + Os::pageSize(); + address userHostMemGuarded = Os::reserveMemory(sizeToAllocate, Os::MEM_PROT_RW); + address userHostMem = userHostMemGuarded + sizeToAllocate; + userHostMem = amd::alignDown(userHostMem - guardSize, Os::pageSize()); + + // Protect the guard pages after the end of the users's buffer. + if (!Os::protectMemory(userHostMem, guardSize, Os::MEM_PROT_NONE)) { + fatal("Protect memory (up) failed"); + } + + userHostMem = userHostMem - size; + userHostMem = amd::alignDown(userHostMem, alignment); + // Write the actual size allocated including all the guard pages, + // alignment, page file size... as well as the size of guarded byte + // count before the beginning of the user's buffer. + size_t* temp = reinterpret_cast(userHostMem); + *--temp = sizeToAllocate; + *--temp = userHostMem - userHostMemGuarded; + + // Protect the guard pages before the beginning of the user's buffer. + if (!Os::protectMemory(userHostMemGuarded, guardSize, Os::MEM_PROT_NONE)) { + fatal("Protect memory (down) failed"); + } + + return userHostMem; +} + +void +AlignedMemory::deallocate(void* ptr) +{ + Os::alignedFree(ptr); +} + +void +GuardedMemory::deallocate(void* ptr) +{ + size_t* userHostMem = static_cast(ptr); + + size_t size = *--userHostMem; + size_t offset = *--userHostMem; + + Os::releaseMemory(static_cast
(ptr) - offset, size); +} + +void* +HeapObject::operator new(size_t size) +{ + return malloc(size); +} + +void +HeapObject::operator delete(void* obj) +{ + free(obj); +} + + +} // namespace amd diff --git a/rocclr/compiler/lib/promotions/oclutils/os/alloc.hpp b/rocclr/compiler/lib/promotions/oclutils/os/alloc.hpp new file mode 100644 index 0000000000..c243d2b0d0 --- /dev/null +++ b/rocclr/compiler/lib/promotions/oclutils/os/alloc.hpp @@ -0,0 +1,30 @@ +// +// Copyright (c) 2008 Advanced Micro Devices, Inc. All rights reserved. +// + +#ifndef ALLOC_HPP_ +#define ALLOC_HPP_ + +#include "top.hpp" + +namespace amd { + +class AlignedMemory : public AllStatic +{ +public: + static void* allocate(size_t size, size_t alignment); + + static void deallocate(void* ptr); +}; + +class GuardedMemory : public AllStatic +{ +public: + static void* allocate(size_t size, size_t alignment, size_t guardSize); + + static void deallocate(void* ptr); +}; + +} // namespace amd + +#endif /*ALLOC_HPP_*/ diff --git a/rocclr/compiler/lib/promotions/oclutils/os/os.cpp b/rocclr/compiler/lib/promotions/oclutils/os/os.cpp new file mode 100644 index 0000000000..0f01f31902 --- /dev/null +++ b/rocclr/compiler/lib/promotions/oclutils/os/os.cpp @@ -0,0 +1,145 @@ +// +// Copyright (c) 2008 Advanced Micro Devices, Inc. All rights reserved. +// + +#include "os/os.hpp" +#include "thread/thread.hpp" + +#include +#include + +#if defined(_WIN32) || defined(__CYGWIN__) +# include +#else // !_WIN32 +# include +# include +#endif // !_WIN32 + +#if defined(ATI_ARCH_X86) +#include // for _mm_pause +#endif // ATI_ARCH_X86 + +namespace amd { + +void* +Os::loadLibrary(const char* libraryname) +{ + void* handle = Os::loadLibrary_(libraryname); + if (handle != NULL) { + return handle; + } + + // Try with the system library prefix and extension instead. + std::string str = libraryname; + + size_t namestart = str.rfind(fileSeparator()); + namestart = (namestart != std::string::npos) ? namestart + 1 : 0; + + const char* prefix = Os::libraryPrefix(); + if (prefix != NULL + && str.compare(namestart, strlen(prefix), prefix) == 0) { + // It is alread present, not need to prepend it. + prefix = NULL; + } + size_t dot = str.rfind('.'); + if (dot != std::string::npos) { + // check that the dot was on the filename not a dir name. + if (namestart < dot) { + // strip the previous extension. + str.resize(dot); + } + } + if (prefix != NULL && prefix[0] != '\0') { + str.insert(namestart, prefix); + } + str.append(Os::libraryExtension()); + + handle = Os::loadLibrary_(str.c_str()); + if (handle != NULL || str.find(fileSeparator()) != std::string::npos) { + return handle; + } + + // Try to find the lib in the current directory. + return Os::loadLibrary((std::string(".") + fileSeparator() + + std::string(libraryname)).c_str()); +} + +size_t Os::pageSize_ = 0; + +int Os::processorCount_ = 0; + +void +Os::spinPause() +{ +#if defined(ATI_ARCH_X86) + _mm_pause(); +#elif defined(__ARM_ARCH_7A__) + __asm__ __volatile__("yield"); +#endif +} + +void +Os::sleep(long n) +{ + // FIXME_lmoriche: Should be nano-seconds not seconds. +#ifdef _WIN32 + ::Sleep(n); +#else // !_WIN32 + time_t seconds = (time_t) n / 1000; + long nanoseconds = ((long) n - seconds * 1000) * 1000000; + timespec ts = { seconds, nanoseconds }; + ::nanosleep(&ts, NULL); +#endif // !_WIN32 +} + +void +Os::touchStackPages(address bottom, address top) +{ + top = alignDown(top, pageSize_) - pageSize_; + while (top >= bottom) { + *top = 0; + top -= pageSize_; + } +} + +bool +Os::skipIDIV(address& pc) +{ + address insn = pc; + if (insn[0] == 0x66) { // LCP prefix + insn += 1; + } + if ((insn[0] & 0xf0) == 0x40) { // REX prefix + insn += 1; + } + if (insn[0] == 0xf6 || insn[0] == 0xf7) { // IDIV + // This is a DivisionError: skip the insn and resume execution + char mod = insn[1] >> 6; + char rm = insn[1] & 0x7; + insn += 2; // skip opcode and mod/rm + + if (rm == 0x4 && mod != 0x3) { + insn += 1; // sib follows mod/rm + } + + if ((mod == 0x0 && rm == 0x5) || mod == 0x2) { + insn += 4; // disp32 + } + else if (mod == 0x1) { + insn += 1; // disp8 + } + pc = insn; + return true; + } + return false; +} + +void +Os::setThreadAffinity(const void* handle, unsigned int cpu) +{ + ThreadAffinityMask mask; + mask.set(cpu); + setThreadAffinity(handle, mask); +} + +} // namespace amd diff --git a/rocclr/compiler/lib/promotions/oclutils/os/os.hpp b/rocclr/compiler/lib/promotions/oclutils/os/os.hpp new file mode 100644 index 0000000000..249838420a --- /dev/null +++ b/rocclr/compiler/lib/promotions/oclutils/os/os.hpp @@ -0,0 +1,506 @@ +// +// Copyright (c) 2008 Advanced Micro Devices, Inc. All rights reserved. +// + +#ifndef OS_HPP_ +#define OS_HPP_ + +#include "top.hpp" +#include "utils/util.hpp" + +#include +#include + +#if defined(__linux__) +# include +#endif + +#ifdef _WIN32 +# include // For KAFFINITY +#endif // _WIN32 + +// Smallest supported VM page size. +#define MIN_PAGE_SHIFT 12 +#define MIN_PAGE_SIZE (1UL << MIN_PAGE_SHIFT) + +namespace amd { + +/*! \addtogroup Os Operating System Abstraction + * + * \copydoc amd::Os + * + * @{ + */ + +class Thread; // For Os::createOsThread() + +class Os : AllStatic +{ +public: + enum MemProt + { + MEM_PROT_NONE = 0, + MEM_PROT_READ, + MEM_PROT_RW, + MEM_PROT_RWX + }; + + class ThreadAffinityMask + { + friend class Os; + private: +#if defined(__linux__) + cpu_set_t mask_; +#else // _WIN32 + #if !defined(_WIN32) + typedef uint KAFFINITY; + #endif + KAFFINITY mask_[512 / sizeof(KAFFINITY)]; +#endif + + public: + ThreadAffinityMask() { init(); } + + inline void init(); + inline void set(uint cpu); + inline void clear(uint cpu); + inline bool isSet(uint cpu) const; + inline bool isEmpty() const; + inline uint countSet() const; + + inline uint getFirstSet() const; + inline uint getNextSet(uint cpu) const; + +#if defined(__linux__) + inline void set(const cpu_set_t& mask); + inline void clear(const cpu_set_t& mask); + inline void adjust(cpu_set_t& mask) const; + inline cpu_set_t& getNative() { return mask_; } +#else + inline void set(size_t group, KAFFINITY affinity); + inline void adjust(size_t group, KAFFINITY& affinity) const; +#endif + }; + +private: + static size_t pageSize_; //!< The default os page size. + static int processorCount_; //!< The number of active processors. + +private: + //! Load the shared library named by \a filename + static void* loadLibrary_(const char* filename); + +public: + //! Initialize the Os package. + static bool init(); + //! Tear down the Os package. + static void tearDown(); + + // Topology helper routines: + // + + //! Return the number of active processors in the system. + inline static int processorCount(); + +#if defined(ATI_ARCH_X86) + //! Query the processor information about supported features and CPU type. + static void cpuid(int regs[4], int info); + //! Get value of extended control register + static uint64_t xgetbv(uint32_t which); +#endif // ATI_ARCH_X86 + + // Stack helper routines: + // + + //! Return the current stack base and size information. + static void currentStackInfo(address* base, size_t *size); + + //! Return the value of the current stack pointer. + static NOT_WIN64(inline) address currentStackPtr(); + //! Set the value of the current stack pointer. + static WIN64_ONLY(inline) void WINDOWS_ONLY(__stdcall/*callee cleanup*/) + setCurrentStackPtr(address sp); + //! Touches all stack pages between [bottom,top[ + static void touchStackPages(address bottom, address top); + + // Thread routines: + // + + //! Create a native thread and link it to the given OsThread. + static const void* createOsThread(Thread* osThread); + //! Set the thread's affinity to the given cpu ordinal. + static void setThreadAffinity(const void* handle, unsigned int cpu); + //! Set the thread's affinity to the given cpu mask. + static void setThreadAffinity(const void* handle, const ThreadAffinityMask& mask); + //! Set the currently running thread's name. + static void setCurrentThreadName(const char* name); + + //! Sleep for n milli-seconds. + static void sleep(long n); + //! Yield to threads of the same or lower priority + static void yield(); + //! Execute a pause instruction (for spin loops). + static void spinPause(); + + // Memory routines: + // + + //! Return the default os page size. + inline static size_t pageSize(); + //! Return the amount of host total physical memory in bytes. + static uint64_t hostTotalPhysicalMemory(); + + //! Reserve a chunk of memory (priv | anon | map on demand). + static address reserveMemory(size_t size, MemProt prot = MEM_PROT_NONE); + //! Set the page protections for the given memory region. + static bool protectMemory(void* addr, size_t size, MemProt prot); + //! Release a chunk of memory allocated with reserveMemory. + static bool releaseMemory(void* addr, size_t size); + //! Allocate an aligned chunk of memory. + static void* alignedMalloc(size_t size, size_t alignment); + //! Deallocate an aligned chunk of memory. + static void alignedFree(void* mem); + //! Platform-specific optimized memcpy() + static void* fastMemcpy(void *dest, const void *src, size_t n); + + // File/Path helper routines: + // + + //! Return the shared library extension string. + static const char* libraryExtension(); + //! Return the shared library prefix string. + static const char* libraryPrefix(); + //! Return the object extension string. + static const char* objectExtension(); + //! Return the file separator char. + static char fileSeparator(); + //! Return the path separator char. + static char pathSeparator(); + //! Return whether the path exists + static bool pathExists(const std::string& path); + //! Create the path if it does not exist + static bool createPath(const std::string& path); + //! Remove the path if it is empty + static bool removePath(const std::string& path); + //! Printf re-implementation (due to MS CRT problem) + static int printf(const char*fmt,...); + /*! \brief Invokes the command processor for the command execution + * + * \result Returns the operation result + */ + static int systemCall( + const std::string& command); //!< command for execution + + /*! \brief Retrieves a string containing the value + * of the environment variable + * + * \result Returns the environment variable value + */ + static std::string getEnvironment( + const std::string& name); //!< the environment variable's name + + /*! \brief Retrieves the path of the directory designated for temporary + * files + * + * \result Returns the temporary path + */ + static std::string getTempPath(); + + /*! \brief Creates a name for a temporary file + * + * \result Returns the name of temporary file + */ + static std::string getTempFileName(); + + //! Deletes file + static int unlink(const std::string& path); + + // Library routines: + // + typedef bool (*SymbolCallback)(std::string, const void*, void*); + + //! Load the shared library named by \a filename + static void* loadLibrary(const char* filename); + //! Unload the shared library. + static void unloadLibrary(void* handle); + //! Return the address of the function identified by \a name. + static void* getSymbol(void* handle, const char* name); + //! Get all the __kernel functions in the given shared library. + static bool iterateSymbols(void* handle, SymbolCallback func, void* data); + + // Time routines: + // + + //! Return the current system time counter in nanoseconds. + static uint64_t timeNanos(); + //! Return the system timer's resolution in nanoseconds. + static uint64_t timerResolutionNanos(); + //! Return the timeNanos starting point offset to Epoch. + static uint64_t offsetToEpochNanos(); + + // X86 Instructions helpers: + // + + //! Skip an IDIV (F6/F7) instruction and return a pointer to the next insn. + static bool skipIDIV(address& insn); + + // return gloabal memory size to be assigned to device info + static size_t getPhysicalMemSize(); +}; + +/*@}*/ + +inline size_t +Os::pageSize() +{ + assert(pageSize_ != 0 && "runtime is not initialized"); + return pageSize_; +} + +inline int +Os::processorCount() +{ + return processorCount_; +} + +#if defined(_WIN64) + +extern "C" void _Os_setCurrentStackPtr(address sp); + +ALWAYSINLINE void +Os::setCurrentStackPtr(address sp) +{ + _Os_setCurrentStackPtr(sp); +} + +#else // !_WIN64 + +ALWAYSINLINE address +Os::currentStackPtr() +{ + intptr_t value; + +#if defined(__GNUC__) + __asm__ __volatile__ ( +# if defined(ATI_ARCH_X86) + LP64_SWITCH("movl %%esp", "movq %%rsp") ",%0" : "=r"(value) +# elif defined(ATI_ARCH_ARM) + "mov %0,sp" : "=r"(value) +# endif + ); +#else // !__GNUC__ + __asm mov value, esp; +#endif // !__GNUC__ + + return (address)value; +} + +#endif // !_WIN64 + + +#if defined(__linux__) + +inline void +Os::ThreadAffinityMask::init() +{ + CPU_ZERO(&mask_); +} + +inline void +Os::ThreadAffinityMask::set(uint cpu) +{ + CPU_SET(cpu, &mask_); +} + +inline void +Os::ThreadAffinityMask::clear(uint cpu) +{ + CPU_CLR(cpu, &mask_); +} + +inline bool +Os::ThreadAffinityMask::isSet(uint cpu) const +{ + return CPU_ISSET(cpu, &mask_); +} + +inline bool +Os::ThreadAffinityMask::isEmpty() const +{ + const uint32_t* bits = (const uint32_t*)mask_.__bits; + for (uint i = 0; i < sizeof(mask_.__bits) / sizeof(uint32_t); ++i) { + if (bits[i] != 0) { + return false; + } + } + return true; +} + +inline void +Os::ThreadAffinityMask::set(const cpu_set_t& mask) +{ + mask_ = mask; +} + +inline void +Os::ThreadAffinityMask::clear(const cpu_set_t& mask) +{ + const uint32_t* bitsClear = (const uint32_t*)mask.__bits; + uint32_t* bits = (uint32_t*)mask_.__bits; + for (uint i = 0; i < sizeof(mask_.__bits) / sizeof(uint32_t); ++i) { + bits[i] &= ~bitsClear[i]; + } +} + +inline void +Os::ThreadAffinityMask::adjust(cpu_set_t& mask) const +{ + uint32_t* bitsOut = (uint32_t*)mask.__bits; + const uint32_t* bits = (const uint32_t*)mask_.__bits; + for (uint i = 0; i < sizeof(mask_.__bits) / sizeof(uint32_t); ++i) { + bitsOut[i] &= bits[i]; + } +} + +inline uint +Os::ThreadAffinityMask::countSet() const +{ + uint count = 0; + const uint32_t* bits = (const uint32_t*)mask_.__bits; + for (uint i = 0; i < sizeof(mask_.__bits) / sizeof(uint32_t); ++i) { + count += countBitsSet(bits[i]); + } + return count; +} + +inline uint +Os::ThreadAffinityMask::getFirstSet() const +{ + const uint32_t* bits = (const uint32_t*)mask_.__bits; + for (uint i = 0; i < sizeof(mask_.__bits) / sizeof(uint32_t); ++i) { + if (bits[i] != 0) { + return leastBitSet(bits[i]) + (i * (8*sizeof(uint32_t))); + } + } + return (uint)-1; +} + +inline uint +Os::ThreadAffinityMask::getNextSet(uint cpu) const +{ + const uint32_t* bits = (const uint32_t*)mask_.__bits; + ++cpu; + uint j = cpu % (8*sizeof(uint32_t)); + for (uint i = cpu / (8*sizeof(uint32_t)); + i < sizeof(mask_.__bits) / sizeof(uint32_t); ++i) { + if (bits[i] != 0) { + for (; j < (8*sizeof(uint32_t)); ++j) { + if (0 != (bits[i] & ((uint32_t)1 << j))) { + return i * (8*sizeof(uint32_t)) + j; + } + } + } + j = 0; + } + return (uint)-1; +} + +#else + +inline void +Os::ThreadAffinityMask::init() +{ + for (uint i = 0; i < sizeof(mask_) / sizeof(KAFFINITY); ++i) { + mask_[i] = (KAFFINITY)0; + } +} + +inline void +Os::ThreadAffinityMask::set(uint cpu) +{ + mask_[cpu / (8*sizeof(KAFFINITY))] |= + (KAFFINITY)1 << (cpu % (8*sizeof(KAFFINITY))); +} + +inline void +Os::ThreadAffinityMask::clear(uint cpu) +{ + mask_[cpu / (8*sizeof(KAFFINITY))] &= + ~( (KAFFINITY)1 << (cpu % (8*sizeof(KAFFINITY))) ); +} + +inline bool +Os::ThreadAffinityMask::isSet(uint cpu) const +{ + return (KAFFINITY)0 != (mask_[cpu / (8*sizeof(KAFFINITY))] & + ((KAFFINITY)1 << (cpu % (8*sizeof(KAFFINITY))))); +} + +inline bool +Os::ThreadAffinityMask::isEmpty() const +{ + for (uint i = 0; i < sizeof(mask_) / sizeof(KAFFINITY); ++i) { + if (mask_[i] != (KAFFINITY)0) { + return false; + } + } + return true; +} + +inline void +Os::ThreadAffinityMask::set(size_t group, KAFFINITY affinity) +{ + mask_[group] |= affinity; +} + +inline void +Os::ThreadAffinityMask::adjust(size_t group, KAFFINITY& affinity) const +{ + affinity &= mask_[group]; +} + +inline uint +Os::ThreadAffinityMask::countSet() const +{ + uint count = 0; + for (uint i = 0; i < sizeof(mask_) / sizeof(KAFFINITY); ++i) { + count += countBitsSet(mask_[i]); + } + return count; +} + +inline uint +Os::ThreadAffinityMask::getFirstSet() const +{ + for (uint i = 0; i < sizeof(mask_) / sizeof(KAFFINITY); ++i) { + if (mask_[i] != 0) { + return leastBitSet(mask_[i]) + (i * (8*sizeof(KAFFINITY))); + } + } + return (uint)-1; +} + +inline uint +Os::ThreadAffinityMask::getNextSet(uint cpu) const +{ + ++cpu; + uint j = cpu % (8*sizeof(KAFFINITY)); + for (uint i = cpu / (8*sizeof(KAFFINITY)); + i < sizeof(mask_) / sizeof(KAFFINITY); ++i) { + if (mask_[i] != 0) { + for (; j < (8*sizeof(KAFFINITY)); ++j) { + if (0 != (mask_[i] & ((KAFFINITY)1 << j))) { + return i * (8*sizeof(KAFFINITY)) + j; + } + } + } + j = 0; + } + return (uint)-1; +} + +#endif + +} // namespace amd + +#endif /*OS_HPP_*/ diff --git a/rocclr/compiler/lib/promotions/oclutils/os/os_posix.cpp b/rocclr/compiler/lib/promotions/oclutils/os/os_posix.cpp new file mode 100644 index 0000000000..97cb9ba02e --- /dev/null +++ b/rocclr/compiler/lib/promotions/oclutils/os/os_posix.cpp @@ -0,0 +1,828 @@ +// +// Copyright (c) 2008 Advanced Micro Devices, Inc. All rights reserved. +// + +#if !defined(_WIN32) && !defined(__CYGWIN__) + +#include "os/os.hpp" +#include "thread/thread.hpp" + +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include + +#include +#include +#include +#ifndef DT_GNU_HASH +# define DT_GNU_HASH 0x6ffffef5 +#endif // DT_GNU_HASH + +#include +#include +#include +#include // for strncmp +#include +#include // for tempnam +#include + +#ifdef ANDROID +//#include +#endif + +namespace amd { + +static struct sigaction oldSigAction; + +static bool +callOldSignalHandler(int sig, siginfo_t* info, void* ptr) +{ + if (oldSigAction.sa_handler == SIG_DFL) { + // no signal handler was previously installed. + return false; + } + else if (oldSigAction.sa_handler != SIG_IGN) { + + if ((oldSigAction.sa_flags & SA_NODEFER) == 0) { + sigaddset(&oldSigAction.sa_mask, sig); + } + + void (*handler)(int) = oldSigAction.sa_handler; + if (oldSigAction.sa_flags & SA_RESETHAND) { + oldSigAction.sa_handler = SIG_DFL; + } + + sigset_t savedSigSet; + pthread_sigmask(SIG_SETMASK, &oldSigAction.sa_mask, &savedSigSet); + + if (oldSigAction.sa_flags & SA_SIGINFO) { + oldSigAction.sa_sigaction(sig, info, ptr); + } + else { + handler(sig); + } + + pthread_sigmask(SIG_SETMASK, &savedSigSet, NULL); + } + + return true; +} + +static void +divisionErrorHandler(int sig, siginfo_t* info, void* ptr) +{ +#ifdef ANDROID + assert(false && "ucontext_t undefined for Android"); + return; +#else + assert(info != NULL && ptr != NULL && "just checking"); + ucontext_t* uc = (ucontext_t*) ptr; + address insn; + +#if defined(ATI_ARCH_X86) + insn = (address)uc->uc_mcontext.gregs[LP64_SWITCH(REG_EIP,REG_RIP)]; +#else + assert(!"Unimplemented"); +#endif + + // Call the chained signal handler + if (callOldSignalHandler(sig, info, ptr)) { + return; + } + + // @todo: only handle exception in the generated code. + // + //if (!isKernelCode(insn)) { + // return; + //} + + if (sig == SIGFPE && info->si_code == FPE_INTDIV) { + if (Os::skipIDIV(insn)) { +#if defined(ATI_ARCH_X86) + uc->uc_mcontext.gregs[LP64_SWITCH(REG_EIP,REG_RIP)] = (greg_t)insn; +#else + assert(!"Unimplemented"); +#endif + return; + } + } + + std::cerr << "Unhandled signal in divisionErrorHandler()" << std::endl; + ::abort(); +#endif // !ANDROID +} + +typedef int (*pthread_setaffinity_fn)(pthread_t, size_t , const cpu_set_t *); +static pthread_setaffinity_fn pthread_setaffinity_fptr; + +static void init() __attribute__((constructor(101))); +static void init() { Os::init(); } + +bool +Os::init() +{ + static bool initialized_ = false; + + // We could use pthread_once here: + if (initialized_) { + return true; + } + initialized_ = true; + + pageSize_ = (size_t) ::sysconf(_SC_PAGESIZE); + processorCount_ = ::sysconf(_SC_NPROCESSORS_CONF); + + // Install a SIGFPE signal handler @todo: Chain the handlers + struct sigaction sa; + sigfillset(&sa.sa_mask); + sa.sa_handler = SIG_DFL; + sa.sa_sigaction = divisionErrorHandler; + sa.sa_flags = SA_SIGINFO | SA_RESTART; + + if (sigaction(SIGFPE, &sa, &oldSigAction) != 0) { + return false; + } + + pthread_setaffinity_fptr = (pthread_setaffinity_fn) + dlsym(RTLD_NEXT, "pthread_setaffinity_np"); + + return Thread::init(); +} + +static void __exit() __attribute__((destructor(101))); +static void __exit() { Os::tearDown(); } + +void +Os::tearDown() +{ + Thread::tearDown(); +} + +bool +Os::iterateSymbols(void* handle, Os::SymbolCallback callback, void* data) +{ +#ifdef ANDROID + assert(false && "dlinfo undefined for Android in dlfcn.h"); + return false; +#else + const char magic[] = "__OpenCL_"; + const size_t len = sizeof(magic) - 1; + + struct link_map *link_map = NULL; + if (::dlinfo(handle, RTLD_DI_LINKMAP, &link_map) != 0) { + return false; + } + + assert(link_map != NULL && "just checking"); + const ElfW(Dyn)* dyn = (ElfW(Dyn)*)(link_map->l_ld); + + const Elf32_Word* gnuhash = NULL; + const Elf_Symndx* hash = NULL; + const ElfW(Sym)* symbols = NULL; + const char* stringTable = NULL; + size_t tableSize = 0; + + // Search for the string table address and size. + while (dyn->d_tag != DT_NULL) { + switch (dyn->d_tag) { + case DT_HASH: + hash = (Elf_Symndx*) dyn->d_un.d_ptr; + break; + case DT_GNU_HASH: + gnuhash = (Elf32_Word*) dyn->d_un.d_ptr; + break; + case DT_SYMTAB: + symbols = (ElfW(Sym)*) dyn->d_un.d_ptr; + break; + case DT_STRTAB: + stringTable = (const char*) dyn->d_un.d_ptr; + break; + case DT_STRSZ: + tableSize = dyn->d_un.d_val; + break; + default: + break; + } + ++dyn; + } + if (stringTable == NULL || tableSize == 0 || symbols == NULL + || (hash == NULL && gnuhash == NULL)) { + // Could not find the string table + return false; + } + + if (gnuhash == NULL) { + // Read the defined symbols out of the classic SYSV hashtable. + + Elf_Symndx nbuckets = hash[1]; + for (Elf_Symndx i = 0; i < nbuckets; ++i) { + + if (symbols[i].st_shndx == SHN_UNDEF + && symbols[i].st_value == 0) { + continue; + } + + const char* name = &stringTable[symbols[i].st_name]; + if (::strncmp(name, magic, len) == 0) { + callback(name, (const void*) + (link_map->l_addr + symbols[i].st_value), data); + } + } + return true; + } + + // Read the defined symbols out of the GNU hashtable. + + Elf_Symndx nbuckets = gnuhash[0]; + Elf32_Word bias = gnuhash[1]; + Elf32_Word nwords = gnuhash[2]; + const Elf32_Word* buckets = &gnuhash[4 + __ELF_NATIVE_CLASS / 32 * nwords]; + const Elf32_Word* chain0 = &buckets[nbuckets] - bias; + + for (Elf_Symndx i = 0; i < nbuckets; ++i) { + size_t index = buckets[i]; + const Elf32_Word *hasharr = &chain0[index]; + do { + if (symbols[index].st_shndx != SHN_UNDEF + || symbols[index].st_value != 0) { + const char* name = &stringTable[symbols[index].st_name]; + if (::strncmp(name, magic, len) == 0) { + callback(name, (const void*) + (link_map->l_addr + symbols[index].st_value), data); + } + } + ++index; + } while ((*hasharr++ & 1) == 0); + } + +#endif // !ANDROID + return true; +} + +void* +Os::loadLibrary_(const char *filename) +{ + return (*filename == '\0') ? NULL : ::dlopen(filename, RTLD_LAZY); +} + +void +Os::unloadLibrary(void* handle) +{ + ::dlclose(handle); +} + +void* +Os::getSymbol(void* handle, const char* name) +{ + return ::dlsym(handle, name); +} + +static inline int +memProtToOsProt(Os::MemProt prot) +{ + switch (prot) { + case Os::MEM_PROT_NONE: return PROT_NONE; + case Os::MEM_PROT_READ: return PROT_READ; + case Os::MEM_PROT_RW: return PROT_READ | PROT_WRITE; + case Os::MEM_PROT_RWX: return PROT_READ | PROT_WRITE | PROT_EXEC; + default: break; + } + ShouldNotReachHere(); + return -1; +} + +address +Os::reserveMemory(size_t size, MemProt prot) +{ + address mem = (address) ::mmap(NULL, size, memProtToOsProt(prot), + MAP_PRIVATE | MAP_ANONYMOUS, 0, 0); + + assert(mem != NULL && "out of memory"); + return mem; +} + +bool +Os::releaseMemory(void* addr, size_t size) +{ + // Needs to calculate the size and actual address. + return 0 == ::munmap(addr, size); +} + +bool +Os::protectMemory(void* addr, size_t size, MemProt prot) +{ + return 0 == ::mprotect(addr, size, memProtToOsProt(prot)); +} + +uint64_t +Os::hostTotalPhysicalMemory() +{ + static uint64_t totalPhys = 0; + + if (totalPhys != 0) { + return totalPhys; + } + + totalPhys = sysconf(_SC_PAGESIZE) * sysconf(_SC_PHYS_PAGES); + return totalPhys; +} + +void* +Os::alignedMalloc(size_t size, size_t alignment) +{ + void * ptr = NULL; + if (0 == ::posix_memalign(&ptr, alignment, size)) { + return ptr; + } + return NULL; +} + +void +Os::alignedFree(void *mem) +{ + ::free(mem); +} + +void +Os::currentStackInfo(address* base, size_t *size) +{ + // There could be some issue trying to get the pthread_attr of + // the primordial thread if the pthread library is not present + // at load time (a binary loads the OpenCL app/runtime dynamically. + // We should look into this... -laurent + + pthread_t self = ::pthread_self(); + + pthread_attr_t threadAttr; + if (0 != ::pthread_getattr_np(self, &threadAttr)) { + fatal("pthread_getattr_np() failed"); + } + + if (0 != ::pthread_attr_getstack(&threadAttr, + (void **) base, size)) { + fatal("pthread_attr_getstack() failed"); + } + *base += *size; + + ::pthread_attr_destroy(&threadAttr); + + assert(Os::currentStackPtr() >= *base - *size + && Os::currentStackPtr() < *base + && "just checking"); +} + +void +Os::setCurrentThreadName(const char* name) +{ + ::prctl(PR_SET_NAME, name); +} + + +void* +Thread::entry(Thread* thread) +{ + sigset_t set; + + sigfillset(&set); + pthread_sigmask(SIG_BLOCK, &set, NULL); + + sigemptyset(&set); + sigaddset(&set, SIGFPE); + pthread_sigmask(SIG_UNBLOCK, &set, NULL); + + return thread->main(); +} + +const void* +Os::createOsThread(amd::Thread* thread) +{ + pthread_attr_t threadAttr; + ::pthread_attr_init(&threadAttr); + + if (thread->stackSize_ != 0) { + size_t guardsize = 0; + if (0 != ::pthread_attr_getguardsize(&threadAttr, + &guardsize)) { + fatal("pthread_attr_getguardsize() failed"); + } + ::pthread_attr_setstacksize(&threadAttr, thread->stackSize_ + guardsize); + } + + // We never plan the use join, so free the resources now. + ::pthread_attr_setdetachstate(&threadAttr, PTHREAD_CREATE_DETACHED); + + pthread_t handle = (pthread_t)NULL; + if (0 != ::pthread_create(&handle, &threadAttr, + (void* (*)(void*)) &Thread::entry, thread)) { + thread->setState(Thread::FAILED); + } + + ::pthread_attr_destroy(&threadAttr); + return reinterpret_cast(handle); +} + + +void +Os::setThreadAffinity(const void* handle, const Os::ThreadAffinityMask& mask) +{ + if (pthread_setaffinity_fptr != NULL) { + pthread_setaffinity_fptr((pthread_t)handle, sizeof(cpu_set_t), &mask.mask_); + } +} + +void +Os::yield() +{ + ::sched_yield(); +} + +uint64_t +Os::timeNanos() +{ + struct timespec tp; + ::clock_gettime(CLOCK_MONOTONIC, &tp); + return (uint64_t) tp.tv_sec * (1000ULL*1000ULL*1000ULL) + + (uint64_t) tp.tv_nsec; +} + +uint64_t +Os::timerResolutionNanos() +{ + static uint64_t resolution = 0; + if (resolution == 0) { + struct timespec tp; + ::clock_getres(CLOCK_MONOTONIC, &tp); + resolution = (uint64_t) tp.tv_sec * (1000ULL*1000ULL*1000ULL) + + (uint64_t) tp.tv_nsec; + } + return resolution; +} + + +const char* +Os::libraryExtension() +{ + return MACOS_SWITCH(".dylib", ".so"); +} + +const char* +Os::libraryPrefix() +{ + return "lib"; +} + +const char* +Os::objectExtension() +{ + return ".o"; +} + +char +Os::fileSeparator() +{ + return '/'; +} + +char +Os::pathSeparator() +{ + return ':'; +} + +bool Os::pathExists(const std::string& path) +{ + struct stat st; + if (stat(path.c_str(), &st) != 0) + return false; + return S_ISDIR(st.st_mode); +} + +bool Os::createPath(const std::string& path) +{ + mode_t mode = S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH; + size_t pos = 0; + while (true) { + pos = path.find(fileSeparator(), pos); + const std::string currPath = path.substr(0, pos); + if (!currPath.empty() && !pathExists(currPath)) { + int ret = mkdir(currPath.c_str(), mode); + if (ret == -1) return false; + } + if (pos == std::string::npos) break; + ++pos; + } + return true; +} + +bool Os::removePath(const std::string& path) +{ + size_t pos = std::string::npos; + bool removed =false; + while (true) { + const std::string currPath = path.substr(0, pos); + if (!currPath.empty()) { + int ret = rmdir(currPath.c_str()); + if (ret == -1) return removed; + removed = true; + } + if (pos == 0) break; + pos = path.rfind(fileSeparator(), pos == std::string::npos?pos:pos-1); + if (pos == std::string::npos) break; + } + return true; +} + +int Os::printf(const char* fmt, ...) +{ + va_list ap; + + va_start(ap, fmt); + int len = ::vprintf(fmt, ap); + va_end(ap); + + return len; +} + +// Os::systemCall() +// ================ +// Execute a program and return the program exitcode or -1 if there were problems. +// The input argument 'command' is expected to be a space separated string of +// command-line arguments with arguments containing spaces between double-quotes. +// +// In order to avoid duplication of memory, we use vfork()+exec(). vfork() has +// potiential security risks; read the following for details: +// +// https://www.securecoding.cert.org/confluence/display/seccode/POS33-C.+Do+not+use+vfork() +// +// In spite of these risks, the alternatives (system() or fork()) create resource +// issues when running conformance test_allocation which stretches the system +// memory to its limits. Thus we will accept this compromise under the condition +// that the runtime will soon remove any need to call out to external commands. +// +// Note that stdin/stdout/stderr of the command are sent to /dev/null. +// +int +Os::systemCall(const std::string& command) +{ +#if 1 + size_t len = command.size(); + char* cmd = new char[len + 1]; + fastMemcpy(cmd, command.c_str(), len); + cmd[len] = 0; + + // Split the command into arguments. This is a very + // simple parser that only takes care of quotes and + // doesn't support escaping with back-slash. In + // the future, Os::systemCall() will either + // disappear or it will be replaced with an + // argc/argv interface. This parser also assumes + // that if an argument is quoted, the whole + // argument starts and ends with a double-quote. + bool inQuote = false; + int argLength = 0; + int n = 0; + char* cp = cmd; + while(*cp) { + switch(static_cast(*cp)) { + case ' ': + if(inQuote) { + ++argLength; + } + else { + *cp = '\0'; + argLength = 0; + } + break; + case '"': + if(inQuote) { + inQuote = false; + *cp = '\0'; + } + else { + inQuote = true; + *cp = '\0'; + argLength = 1; + ++n; + } + break; + default: + if(++argLength == 1) { + ++n; + } + break; + } + ++cp; + } + + char** argv = new char*[n + 1]; + int argc = 0; + cp = cmd; + do { + while('\0' == *cp) { + ++cp; + } + argv[argc++] = cp; + while('\0' != *cp) { + ++cp; + } + } while(argc < n); + argv[argc] = NULL; + + int ret = -1; + pid_t pid = vfork(); + if(0 == pid) { + // Child. Redirect stdin/stdout/stderr to /dev/null + int fdIn = open("/dev/null", O_RDONLY); + int fdOut = open("/dev/null", O_WRONLY); + if(0 <= fdIn || 0 <= fdOut) { + dup2(fdIn, 0); + dup2(fdOut, 1); + dup2(fdOut, 2); + + // Execute the program + execvp(argv[0], argv); + } + _exit(-1); + } + else if(0 > pid) { + // Can't vfork + } + else { + // Parent - wait for program to complete and get exit code. + int exitCode; + if(0 <= waitpid(pid, &exitCode, 0)) { + ret = exitCode; + } + } + delete [] argv; + delete [] cmd; + + return ret; +#else + return ::system(command.c_str()); +#endif +} + +std::string +Os::getEnvironment(const std::string& name) +{ + char* dstBuf; + + dstBuf = ::getenv(name.c_str()); + if (dstBuf == NULL) { + return std::string(""); + } + return std::string(dstBuf); +} + +std::string +Os::getTempPath() +{ + std::string tempFolder = amd::Os::getEnvironment("TEMP"); + if (tempFolder.empty()) { + tempFolder = amd::Os::getEnvironment("TMP"); + } + + if (tempFolder.empty()) { + tempFolder = "/tmp";; + } + return tempFolder; +} + +std::string +Os::getTempFileName() +{ + std::string tempPath = getTempPath(); + char* tempBuf = ::tempnam(tempPath.c_str(), "OCL"); + + if (tempBuf == NULL) { + static amd::Atomic counter = 0; + + std::stringstream ss; + ss << tempPath << "/OCL" << ::getpid() << 'T' << counter++; + return ss.str(); + } + + std::string tempFileName = tempBuf; + free(tempBuf); + return tempFileName; +} + +int +Os::unlink(const std::string& path) +{ + return ::unlink(path.c_str()); +} + +#if defined(ATI_ARCH_X86) +void +Os::cpuid(int regs[4], int info) +{ +#ifdef _LP64 + __asm__ __volatile__ ( + "movq %%rbx, %%rsi;" + "cpuid;" + "xchgq %%rbx, %%rsi;" + : "=a" (regs[0]), "=S" (regs[1]), "=c" (regs[2]), "=d" (regs[3]) + : "a" (info)); +#else + __asm__ __volatile__ ( + "movl %%ebx, %%esi;" + "cpuid;" + "xchgl %%ebx, %%esi;" + : "=a" (regs[0]), "=S" (regs[1]), "=c" (regs[2]), "=d" (regs[3]) + : "a" (info)); +#endif +} + +uint64_t +Os::xgetbv(uint32_t ecx) +{ + uint32_t eax, edx; + + __asm__ __volatile__( + ".byte 0x0f,0x01,0xd0" // in case assembler doesn't recognize xgetbv + : "=a"(eax), "=d"(edx) + : "c"(ecx)); + + return ((uint64_t)edx << 32) | (uint64_t)eax; +} +#endif // ATI_ARCH_X86 + +void* +Os::fastMemcpy(void *dest, const void *src, size_t n) +{ + return memcpy(dest, src, n); +} + +uint64_t +Os::offsetToEpochNanos() +{ + static uint64_t offset = 0; + + if (offset != 0) { + return offset; + } + + struct timeval now; + if (::gettimeofday(&now, NULL) != 0) { + return 0; + } + + offset = (now.tv_sec * UINT64_C(1000000) + now.tv_usec) + * UINT64_C(1000) - timeNanos(); + + return offset; +} + +void +Os::setCurrentStackPtr(address sp) +{ + sp -= sizeof(void*); + *(void**) sp = __builtin_return_address(0); + +#if defined(ATI_ARCH_ARM) + assert(!"Unimplemented"); +#else + __asm__ __volatile__ ( +#if !defined(OMIT_FRAME_POINTER) + LP64_SWITCH("movl (%%ebp),%%ebp;","movq (%%rbp),%%rbp;") +#endif // !OMIT_FRAME_POINTER + LP64_SWITCH("movl %0,%%esp; ret;","movq %0,%%rsp; ret;") + :: "r"(sp) + ); +#endif +} + +size_t Os::getPhysicalMemSize() +{ + struct ::sysinfo si; + + if (::sysinfo(&si) != 0) { + return 0; + } + + if (si.mem_unit == 0) { + // Linux kernels prior to 2.3.23 return sizes in bytes. + si.mem_unit = 1; + } + + return (size_t) si.totalram * si.mem_unit; +} + +} // namespace amd + +#endif // !defined(_WIN32) && !defined(__CYGWIN__) diff --git a/rocclr/compiler/lib/promotions/oclutils/os/os_win32.cpp b/rocclr/compiler/lib/promotions/oclutils/os/os_win32.cpp new file mode 100644 index 0000000000..f627ea8bf6 --- /dev/null +++ b/rocclr/compiler/lib/promotions/oclutils/os/os_win32.cpp @@ -0,0 +1,1019 @@ +// +// Copyright (c) 2008 Advanced Micro Devices, Inc. All rights reserved. +// + +#if defined(_WIN32) || defined(__CYGWIN__) + +#include "os/os.hpp" +#include "thread/thread.hpp" + +#include +#include +#include +#include +#include + +#include +#include +#include +#include + +#ifndef WINAPI +#define WINAPI +#endif + + +BOOL (WINAPI *pfnGetNumaNodeProcessorMaskEx)(USHORT,PGROUP_AFFINITY) = NULL; + +namespace amd { + +static LONG WINAPI divExceptionFilter(struct _EXCEPTION_POINTERS* ep); + +#ifdef _WIN64 +PVOID divExceptionHandler = NULL; +#endif // _WIN64 + +static double PerformanceFrequency; + +typedef BOOL (WINAPI *SetThreadGroupAffinity_fn)( + __in HANDLE, __in CONST GROUP_AFFINITY *, __out_opt PGROUP_AFFINITY); +static SetThreadGroupAffinity_fn pfnSetThreadGroupAffinity = NULL; + +#pragma section(".CRT$XCU",long,read) +__declspec(allocate(".CRT$XCU")) bool (*__init)(void) = Os::init; + +bool +Os::init() +{ + static bool initialized_ = false; + + // We could use InitOnceExecuteOnce here: + if (initialized_) { + return true; + } + initialized_ = true; + + SYSTEM_INFO si; + ::GetSystemInfo(&si); + pageSize_ = si.dwPageSize; + processorCount_ = si.dwNumberOfProcessors; + + LARGE_INTEGER frequency; + QueryPerformanceFrequency(&frequency); + PerformanceFrequency = (double) frequency.QuadPart; + +#ifdef _WIN64 + divExceptionHandler = AddVectoredExceptionHandler(1, divExceptionFilter); +#endif // _WIN64 + + HMODULE handle = ::LoadLibrary("kernel32.dll"); + if (handle != NULL) { + pfnSetThreadGroupAffinity = (SetThreadGroupAffinity_fn) + ::GetProcAddress(handle, "SetThreadGroupAffinity"); + pfnGetNumaNodeProcessorMaskEx = (BOOL(WINAPI *)(USHORT,PGROUP_AFFINITY)) + ::GetProcAddress(handle, "GetNumaNodeProcessorMaskEx"); + } + + return Thread::init(); +} + +#pragma section(".CRT$XTU",long,read) +__declspec(allocate(".CRT$XTU")) void (*__exit)(void) = Os::tearDown; + +void +Os::tearDown() +{ +#ifdef _WIN64 + if (divExceptionHandler != NULL) { + RemoveVectoredExceptionHandler(divExceptionHandler); + divExceptionHandler = NULL; + } +#endif // _WIN64 + + Thread::tearDown(); +} + +//#define DEBUG_getExportsFromMemory +/** + get export symbols from dll given by start address \param dosHeader + of dll in memory and push_back + addresses and names of exports into \param kernels +*/ +static void +getExportsFromMemory( + PIMAGE_DOS_HEADER dosHeader, + Os::SymbolCallback callback, + void* data) +{ + PCHAR base = (PCHAR)dosHeader; + PIMAGE_NT_HEADERS pNTHeader = (PIMAGE_NT_HEADERS) + (base + dosHeader->e_lfanew); + + DWORD exportsStart = pNTHeader->OptionalHeader. + DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT].VirtualAddress; + + if (exportsStart == 0) { return; } + + PIMAGE_EXPORT_DIRECTORY exportDir = (PIMAGE_EXPORT_DIRECTORY) + (base + exportsStart); + + PSTR filename = (PSTR)(exportDir->Name + base); + + #if defined(DEBUG_getExportsFromMemory) + printf("\nExports Table:\n"); + printf(" Name: %s\n", filename); + printf(" Characteristics: %08X\n", exportDir->Characteristics); + printf(" TimeDateStamp: %08X -> %s", + exportDir->TimeDateStamp, + ctime((const time_t *)&exportDir->TimeDateStamp) ); + printf(" Version: %u.%02u\n", exportDir->MajorVersion, + exportDir->MinorVersion); + printf(" Ordinal base: %08X\n", exportDir->Base); + printf(" # of functions: %08X\n", exportDir->NumberOfFunctions); + printf(" # of Names: %08X\n", exportDir->NumberOfNames); + #endif + + /* address of Export Address table (EAT). */ + PDWORD functions = (PDWORD)(base + (DWORD)exportDir->AddressOfFunctions); + DWORD numberOfFunctions = exportDir->NumberOfFunctions; + + /* address of the Export Name Table (ENT). + ENT is an array of RVAs to ASCII strings - each string corresponds to + a symbol (function or variable) exported by name. */ + DWORD* name = (DWORD *)(base + (DWORD)exportDir->AddressOfNames); + /* \note: number below is always <= numberOfFunctions */ + DWORD numberOfNames = exportDir->NumberOfNames; + + /* address of the Export Ordinal Table. + This table maps an array index from ENT into + the corresponding index in EAT. + */ + PWORD ordinals = (PWORD)(base + (DWORD)exportDir->AddressOfNameOrdinals); + + #if defined (DEBUG_getExportsFromMemory) + /* \note On Ordinals and Algorithm Below. + + Each exported symbol has an ordinal number associated with it that can + be used to look the exported symbol up. Also, there is almost always + an ASCII name associated with the symbol. Expectedly, the exported + symbol name is the same as the name of the function or variable, but + in general it is not guaranteed. Usually, when an executable imports + a symbol, it uses the symbol name rather than its ordinal. If it was + always a case the algorithm below could be much simple - just go over + all the names and print them, but some functions may be exported only + by ordinals. When importing by name, the system just uses the name to + look up the export ordinal of the desired symbol, and retrieves the + address using the ordinal value. It might be slightly faster if an + ordinal had been used in the first place. Exporting and importing by + name is solely a convenience for programmers. + The use of the ORDINAL keyword in the Exports section of a .DEF file + tells the linker to create an import library that forces an API to be + imported by ordinal, not by name. + The algorithm in the comments shows how to retrieve all the exports in + the general case. If we assume that all is exported by names then a + simple version (code below) is sufficient. + + \note removed file exportdump.cpp contains examples of reading + exported symbols from DLL loaded in memory or file. + */ + DWORD exportsEnd = pNTHeader->OptionalHeader. + DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT].Size; + + printf("\n Entry Pt Ordn Name\n"); + for (DWORD ii=0; ii < numberOfFunctions; ii++) { + DWORD entryPoint = functions[ii]; + + if (entryPoint == 0) { // Skip over gaps in exported function + continue; // ordinals (the entrypoint is 0 for + } // these functions). + printf(" %08X %4u", entryPoint, ii + exportDir->Base); + + // Browse thru all names and check out if a function has + // an associated exported name. + for (DWORD jj=0; jj < exportDir->NumberOfNames; jj++) { + if (ordinals[jj] == ii) { + printf(" %s", name[jj] + base); + } + } + // Is it a forwarder? If so, the entry point RVA is inside the + // .edata section, and is an RVA to the DllName.EntryPointName + if ((entryPoint >= exportsStart) && (entryPoint <= exportsEnd)) { + printf(" (forwarder -> %s)", entryPoint + base); + } + printf("\n"); + } + #endif + + char OpenCL_prefix[] = "___OpenCL_"; + size_t OpenCL_prefix_sz = sizeof( OpenCL_prefix ) - 1; + + for (DWORD jj=0; jj < numberOfNames; jj++) { + const char* OpenCL_name = (const char*)(base + name[jj]); + if (strncmp(OpenCL_name, OpenCL_prefix, OpenCL_prefix_sz) == 0) { + address addr = (address)(base + functions[ordinals[jj]]); + + unsigned char opcode = *(unsigned char*)addr; + if (opcode == 0xE9) { // jmp instruction at address of export name + long disp = *(long*)(addr+1); // dislacement in jmp + addr += 5 /* skip instruction */ + disp; + } + + #if defined (DEBUG_getExportsFromMemory) + printf("%08X: %s\n", addr, OpenCL_name); + #endif + callback(&OpenCL_name[1], (const void*)addr, data); + } + else if (strncmp( + OpenCL_name, &OpenCL_prefix[1], OpenCL_prefix_sz-1) == 0) { + + address addr = (address)(base + functions[ordinals[jj]]); + #if defined (DEBUG_getExportsFromMemory) + printf("%08X: %s\n", addr, OpenCL_name); + #endif + callback(OpenCL_name, (const void*)addr, data); + } + } +} + +bool +Os::iterateSymbols(void* handle, SymbolCallback callback, void* data) +{ + PIMAGE_DOS_HEADER dosHeader = (PIMAGE_DOS_HEADER)handle; + if (dosHeader->e_magic == IMAGE_DOS_SIGNATURE) { + // checking validity of NT header was removed since we do not want + // exception handling. It can be found in rev #21. + getExportsFromMemory((PIMAGE_DOS_HEADER)handle, callback, data); + return TRUE; + } + return FALSE; +} + +void* +Os::loadLibrary_(const char *filename) +{ + if (filename != NULL) { + HMODULE hModule = ::LoadLibrary(filename); + return hModule; + } + return NULL; +} + +void +Os::unloadLibrary(void* handle) +{ + ::FreeLibrary((HMODULE) handle); +} + +void* +Os::getSymbol(void* handle, const char* name) +{ + return ::GetProcAddress((HMODULE) handle, name); +} + +static inline int +memProtToOsProt(Os::MemProt prot) +{ + switch (prot) { + case Os::MEM_PROT_NONE: return PAGE_NOACCESS; + case Os::MEM_PROT_READ: return PAGE_READONLY; + case Os::MEM_PROT_RW: return PAGE_READWRITE; + case Os::MEM_PROT_RWX: return PAGE_EXECUTE_READWRITE; + default: break; + } + ShouldNotReachHere(); + return -1; +} + +address +Os::reserveMemory(size_t size, MemProt prot) +{ + // Needs to be COMMITed otherwise the protection will fail. + return (address)VirtualAlloc(NULL, size, MEM_COMMIT, PAGE_READWRITE); +} + +bool +Os::releaseMemory(void* addr, size_t size) +{ + BOOL error = VirtualFree(addr, 0, MEM_RELEASE); + return (error == 0) ? false : true; +} + + +bool +Os::protectMemory(void* addr, size_t size, MemProt prot) +{ + DWORD OldProtect; + BOOL error = VirtualProtect(addr, size, memProtToOsProt(prot), &OldProtect); + return (error == 0) ? false : true; +} + + +uint64_t +Os::hostTotalPhysicalMemory() +{ + static uint64_t totalPhys = 0; + + if (totalPhys != 0) { + return totalPhys; + } + + MEMORYSTATUSEX mstatus; + mstatus.dwLength = sizeof(mstatus); + + ::GlobalMemoryStatusEx (&mstatus); + + totalPhys = mstatus.ullTotalPhys; + return totalPhys; +} + +void* +Os::alignedMalloc(size_t size, size_t alignment) +{ + return ::_aligned_malloc(size, alignment); +} + +void +Os::alignedFree(void *mem) +{ + ::_aligned_free(mem); +} + + +void +Os::currentStackInfo(address* base, size_t *size) +{ + MEMORY_BASIC_INFORMATION mbInfo; + + address currentStackPage = (address) alignDown( + (intptr_t) currentStackPtr(), pageSize()); + + ::VirtualQuery(currentStackPage, &mbInfo, sizeof(mbInfo)); + + address stackBottom = (address) mbInfo.AllocationBase; + size_t stackSize = 0; + + do { + stackSize += mbInfo.RegionSize; + ::VirtualQuery(stackBottom + stackSize, &mbInfo, sizeof(mbInfo)); + } while (stackBottom == (address) mbInfo.AllocationBase); + + *base = stackBottom + stackSize; + *size = stackSize; + + assert(currentStackPtr() >= *base - *size && currentStackPtr() < *base + && "just checking"); +} + +#define MS_VC_EXCEPTION 0x406D1388 +#pragma pack(push,8) +struct THREADNAME_INFO +{ + DWORD dwType; // Must be 0x1000. + LPCSTR szName; // Pointer to name (in user addr space). + DWORD dwThreadID; // Thread ID (-1=caller thread). + DWORD dwFlags; // Reserved for future use, must be zero. +}; +#pragma pack(pop) + +static void +SetThreadName(DWORD threadId, const char* name) +{ + if (name == NULL || *name == '\0') { + return; + } + + THREADNAME_INFO info; + info.dwType = 0x1000; + info.szName = name; + info.dwThreadID = threadId; + info.dwFlags = 0; + + __try { + ::RaiseException( + 0x406D1388, 0, sizeof(info)/sizeof(ULONG_PTR), (ULONG_PTR*)&info); + } + __except(EXCEPTION_EXECUTE_HANDLER) { } +} + +void +Os::setCurrentThreadName(const char* name) +{ + SetThreadName(GetCurrentThreadId(), name); +} + +static LONG WINAPI +divExceptionFilter(struct _EXCEPTION_POINTERS* ep) +{ + DWORD code = ep->ExceptionRecord->ExceptionCode; + + if (code == EXCEPTION_INT_DIVIDE_BY_ZERO + || code == EXCEPTION_INT_OVERFLOW) { + // @todo: only handle exception in the generated code. + // + //if (!isKernelCode(insn)) { + // return; + //} + + address insn = (address)ep->ContextRecord->LP64_SWITCH(Eip,Rip); + if (Os::skipIDIV(insn)) { + ep->ContextRecord->LP64_SWITCH(Eip,Rip) = (uintptr_t)insn; + return EXCEPTION_CONTINUE_EXECUTION; + } + } + return EXCEPTION_CONTINUE_SEARCH; +} + +void* +Thread::entry(Thread* thread) +{ + void* ret = NULL; + // @todo: We only need this for CPU worker threads. +#if !defined(_WIN64) + if (true /*thread->isWorkerThread()*/) { + __try { + ret = thread->main(); + } + __except(divExceptionFilter(GetExceptionInformation())) { + // nothing to do here. + } + } + else { +#else // _WIN64 + { +#endif // _WIN64 + ret = thread->main(); + } + return ret; +} + +const void* +Os::createOsThread(Thread* thread) +{ + HANDLE handle = ::CreateThread(NULL, thread->stackSize_, + (LPTHREAD_START_ROUTINE) Thread::entry, thread, 0, NULL); + if (handle == NULL) { + thread->setState(Thread::FAILED); + } + return reinterpret_cast(handle); +} + +void +Os::setThreadAffinity(const void* handle, const Os::ThreadAffinityMask& mask) +{ + if (pfnSetThreadGroupAffinity != NULL) { + GROUP_AFFINITY group = {0}; + for (WORD i = 0; i < sizeof(mask.mask_) / sizeof(KAFFINITY); ++i) { + group.Mask = mask.mask_[i]; + group.Group = i; + if (group.Mask != 0) { + pfnSetThreadGroupAffinity((HANDLE)handle, &group, NULL); + } + } + } + else { // pfnSetThreadGroupAffinity == NULL + DWORD_PTR threadAffinityMask = (DWORD_PTR)mask.mask_[0]; + if (threadAffinityMask != 0) { + ::SetThreadAffinityMask((HANDLE)handle, threadAffinityMask); + } + } +} + +void +Os::yield() +{ + ::SwitchToThread(); +} + +uint64_t +Os::timeNanos() +{ + LARGE_INTEGER current; + QueryPerformanceCounter(¤t); + return (uint64_t) ((double) current.QuadPart / PerformanceFrequency * 1e9); +} + +uint64_t +Os::timerResolutionNanos() +{ + return (uint64_t) (1e9 / PerformanceFrequency); +} + + +const char* +Os::libraryExtension() +{ + return ".DLL"; +} + +const char* +Os::libraryPrefix() +{ + return NULL; +} + +const char* +Os::objectExtension() +{ + return ".OBJ"; +} + +char +Os::fileSeparator() +{ + return '\\'; +} + +char +Os::pathSeparator() +{ + return ';'; +} + +bool Os::pathExists(const std::string& path) +{ + return GetFileAttributes(path.c_str()) != INVALID_FILE_ATTRIBUTES; +} + +bool Os::createPath(const std::string& path) +{ + size_t pos = 0; + while (true) { + pos = path.find(fileSeparator(), pos); + const std::string currPath = path.substr(0, pos); + if (!currPath.empty() && !pathExists(currPath)) { + if (!CreateDirectory(currPath.c_str(), NULL)) return false; + } + if (pos == std::string::npos) break; + ++pos; + } + return true; +} + +bool Os::removePath(const std::string& path) +{ + size_t pos = std::string::npos; + bool removed = false; + while (true) { + const std::string currPath = path.substr(0, pos); + if (!currPath.empty()) { + if (!RemoveDirectory(currPath.c_str())) return removed; + removed = true; + } + if (pos == 0) break; + pos = path.rfind(fileSeparator(), pos == std::string::npos?pos:pos-1); + if (pos == std::string::npos) break; + } + return true; +} + +int Os::printf(const char* fmt, ...) +{ + va_list ap; + DWORD dwBytesWritten; + + va_start(ap, fmt); + int len = ::_vsnprintf(NULL, 0, fmt, ap); + va_end(ap); + if (len <= 0) return len; + + va_start(ap, fmt); + char* str = static_cast(alloca(len + 1)); + len = ::_vsnprintf(str, len + 1, fmt, ap); + va_end(ap); + if (len <= 0) return len; + + ::WriteFile(::GetStdHandle(STD_OUTPUT_HANDLE), str, len, &dwBytesWritten, NULL); + + return len; +} + +int +Os::systemCall(const std::string& command) +{ +#if 1 + char* cmd = new char[command.size()+1]; + fastMemcpy(cmd, command.c_str(), command.size()); + cmd[command.size()] = 0; + + STARTUPINFO si = {0}; + si.cb = sizeof(si); + PROCESS_INFORMATION pi; + + if (::CreateProcess( NULL, cmd, NULL, NULL, + FALSE, CREATE_NO_WINDOW, NULL, NULL, + &si, &pi ) == 0 ) { + delete [] cmd; + return -1; // failed + }; + + // Wait until child process exits. + ::WaitForSingleObject( pi.hProcess, INFINITE ); + + DWORD ExitCode = 0; + ::GetExitCodeProcess(pi.hProcess, &ExitCode); + + // Close process and thread handles. + ::CloseHandle( pi.hProcess ); + ::CloseHandle( pi.hThread ); + + delete [] cmd; + return (int)ExitCode; +#else + std::stringstream str; + str << "\"" << command << "\""; + return ::system(str.str().c_str()); +#endif +} + +std::string +Os::getEnvironment(const std::string& name) +{ + char dstBuf[MAX_PATH]; + size_t dstSize; + + if (::getenv_s(&dstSize, dstBuf, MAX_PATH, name.c_str())) { + return std::string(""); + } + return std::string(dstBuf); +} + +std::string +Os::getTempPath() +{ + char tempPath[MAX_PATH]; + uint ret = GetTempPath(MAX_PATH, tempPath); + if (ret == 0 || (ret == 1 && tempPath[0] == '?')) { + return std::string("."); + } + + // If the app was started from an UNC path instead of a DOS path, + // the temp env var won't be set correctly and will point to windows + // system directory instead (usually c:/windows/temp), which will be + // blocked. So we check if the temp path returned by GetTempPath is + // under windows directory, use . instead + std::string tempPathStr(tempPath); + char winPath[MAX_PATH]; + ret = GetWindowsDirectory(winPath, MAX_PATH); + if (ret > 0) { + size_t len = strlen(winPath); + if (strlen(tempPath) >= len) { + tempPath[len] = 0; + if (_stricmp(tempPath, winPath) == 0) { + return std::string("."); + } + } + } + return tempPathStr; +} + +std::string +Os::getTempFileName() +{ + char tempBuf[MAX_PATH]; + std::string tempPath = getTempPath(); + + if (0 == GetTempFileName(tempPath.c_str(), "OCL", 0, tempBuf)) { + static amd::Atomic counter = 0; + + std::stringstream ss; + ss << tempPath << "\\OCL" << ::_getpid() << 'T' << counter++; + return ss.str(); + } + + return tempBuf; +} + +int +Os::unlink(const std::string& path) +{ + return ::_unlink(path.c_str()); +} + +void +Os::cpuid(int regs[4], int info) +{ + return __cpuid(regs, info); +} + +uint64_t +Os::xgetbv(uint32_t ecx) +{ + return (uint64_t)_xgetbv(ecx); +} + +// Various "fast" memcpy implementation (currently win32 only due to compiler limitations) + +// (dgladdin - "recent" below means MMX and later) + +// Very optimized memcpy() routine for all AMD Athlon and Duron family. +// This code uses any of FOUR different basic copy methods, depending +// on the transfer size. +// NOTE: Since this code uses MOVNTQ (also known as "Non-Temporal MOV" or +// "Streaming Store"), and also uses the software prefetchnta instructions, +// be sure youre running on Athlon/Duron or other recent CPU before calling! + +#define TINY_BLOCK_COPY 64 // upper limit for movsd type copy +// The smallest copy uses the X86 "movsd" instruction, in an optimized +// form which is an "unrolled loop". + +#define IN_CACHE_COPY 64 * 1024 // upper limit for movq/movq copy w/SW prefetch +// Next is a copy that uses the MMX registers to copy 8 bytes at a time, +// also using the "unrolled loop" optimization. This code uses +// the software prefetch instruction to get the data into the cache. + +#define UNCACHED_COPY 197 * 1024 // upper limit for movq/movntq w/SW prefetch +// For larger blocks, which will spill beyond the cache, its faster to +// use the Streaming Store instruction MOVNTQ. This write instruction +// bypasses the cache and writes straight to main memory. This code also +// uses the software prefetch instruction to pre-read the data. +// USE 64 * 1024 FOR THIS VALUE IF YOURE ALWAYS FILLING A "CLEAN CACHE" + +#define BLOCK_PREFETCH_COPY infinity // no limit for movq/movntq w/block prefetch +#define CACHEBLOCK 80h // number of 64-byte blocks (cache lines) for block prefetch +// For the largest size blocks, a special technique called Block Prefetch +// can be used to accelerate the read operations. Block Prefetch reads +// one address per cache line, for a series of cache lines, in a short loop. +// This is faster than using software prefetch. The technique is great for +// getting maximum read bandwidth, especially in DDR memory systems. + +// Inline assembly syntax for use with Visual C++ + +void* +Os::fastMemcpy(void *dest, const void *src, size_t n) +{ +#if !defined(_WIN64) + + __asm { + + mov ecx, [n] ; number of bytes to copy + mov edi, [dest] ; destination + mov esi, [src] ; source + mov ebx, ecx ; keep a copy of count + + cld + cmp ecx, TINY_BLOCK_COPY + jb $memcpy_ic_3 ; tiny? skip mmx copy + + cmp ecx, 32*1024 ; dont align between 32k-64k because + jbe $memcpy_do_align ; it appears to be slower + cmp ecx, 64*1024 + jbe $memcpy_align_done +$memcpy_do_align: + mov ecx, 8 ; a trick thats faster than rep movsb... + sub ecx, edi ; align destination to qword + and ecx, 111b ; get the low bits + sub ebx, ecx ; update copy count + neg ecx ; set up to jump into the array + add ecx, offset $memcpy_align_done + jmp ecx ; jump to array of movsbs + +align 4 + movsb + movsb + movsb + movsb + movsb + movsb + movsb + movsb + +$memcpy_align_done: ; destination is dword aligned + mov ecx, ebx ; number of bytes left to copy + shr ecx, 6 ; get 64-byte block count + jz $memcpy_ic_2 ; finish the last few bytes + + cmp ecx, IN_CACHE_COPY/64 ; too big 4 cache? use uncached copy + jae $memcpy_uc_test + +// This is small block copy that uses the MMX registers to copy 8 bytes +// at a time. It uses the "unrolled loop" optimization, and also uses +// the software prefetch instruction to get the data into the cache. +align 16 +$memcpy_ic_1: ; 64-byte block copies, in-cache copy + + prefetchnta [esi + (200*64/34+192)] ; start reading ahead + + movq mm0, [esi+0] ; read 64 bits + movq mm1, [esi+8] + movq [edi+0], mm0 ; write 64 bits + movq [edi+8], mm1 ; note: the normal movq writes the + movq mm2, [esi+16] ; data to cache; a cache line will be + movq mm3, [esi+24] ; allocated as needed, to store the data + movq [edi+16], mm2 + movq [edi+24], mm3 + movq mm0, [esi+32] + movq mm1, [esi+40] + movq [edi+32], mm0 + movq [edi+40], mm1 + movq mm2, [esi+48] + movq mm3, [esi+56] + movq [edi+48], mm2 + movq [edi+56], mm3 + + add esi, 64 ; update source pointer + add edi, 64 ; update destination pointer + dec ecx ; count down + jnz $memcpy_ic_1 ; last 64-byte block? + +$memcpy_ic_2: + mov ecx, ebx ; has valid low 6 bits of the byte count +$memcpy_ic_3: + shr ecx, 2 ; dword count + and ecx, 1111b ; only look at the "remainder" bits + neg ecx ; set up to jump into the array + add ecx, offset $memcpy_last_few + jmp ecx ; jump to array of movsds + +$memcpy_uc_test: + cmp ecx, UNCACHED_COPY/64 ; big enough? use block prefetch copy + jae $memcpy_bp_1 + +$memcpy_64_test: + or ecx, ecx ; tail end of block prefetch will jump here + jz $memcpy_ic_2 ; no more 64-byte blocks left + +// For larger blocks, which will spill beyond the cache, its faster to +// use the Streaming Store instruction MOVNTQ. This write instruction +// bypasses the cache and writes straight to main memory. This code also +// uses the software prefetch instruction to pre-read the data. +align 16 +$memcpy_uc_1: ; 64-byte blocks, uncached copy + + prefetchnta [esi + (200*64/34+192)] ; start reading ahead + + movq mm0,[esi+0] ; read 64 bits + add edi,64 ; update destination pointer + movq mm1,[esi+8] + add esi,64 ; update source pointer + movq mm2,[esi-48] + movntq [edi-64], mm0 ; write 64 bits, bypassing the cache + movq mm0,[esi-40] ; note: movntq also prevents the CPU + movntq [edi-56], mm1 ; from READING the destination address + movq mm1,[esi-32] ; into the cache, only to be over-written + movntq [edi-48], mm2 ; so that also helps performance + movq mm2,[esi-24] + movntq [edi-40], mm0 + movq mm0,[esi-16] + movntq [edi-32], mm1 + movq mm1,[esi-8] + movntq [edi-24], mm2 + movntq [edi-16], mm0 + dec ecx + movntq [edi-8], mm1 + jnz $memcpy_uc_1 ; last 64-byte block? + + jmp $memcpy_ic_2 ; almost done + +// For the largest size blocks, a special technique called Block Prefetch +// can be used to accelerate the read operations. Block Prefetch reads +// one address per cache line, for a series of cache lines, in a short loop. +// This is faster than using software prefetch, in this case. +// The technique is great for getting maximum read bandwidth, +// especially in DDR memory systems. +$memcpy_bp_1: ; large blocks, block prefetch copy + + cmp ecx, CACHEBLOCK ; big enough to run another prefetch loop? + jl $memcpy_64_test ; no, back to regular uncached copy + + mov eax, CACHEBLOCK / 2 ; block prefetch loop, unrolled 2X + add esi, CACHEBLOCK * 64 ; move to the top of the block +align 16 +$memcpy_bp_2: + mov edx, [esi-64] ; grab one address per cache line + mov edx, [esi-128] ; grab one address per cache line + sub esi, 128 ; go reverse order + dec eax ; count down the cache lines + jnz $memcpy_bp_2 ; keep grabbing more lines into cache + + mov eax, CACHEBLOCK ; now that its in cache, do the copy +align 16 +$memcpy_bp_3: + movq mm0, [esi ] ; read 64 bits + movq mm1, [esi+ 8] + movq mm2, [esi+16] + movq mm3, [esi+24] + movq mm4, [esi+32] + movq mm5, [esi+40] + movq mm6, [esi+48] + movq mm7, [esi+56] + add esi, 64 ; update source pointer + movntq [edi ], mm0 ; write 64 bits, bypassing cache + movntq [edi+ 8], mm1 ; note: movntq also prevents the CPU + movntq [edi+16], mm2 ; from READING the destination address + movntq [edi+24], mm3 ; into the cache, only to be over-written, + movntq [edi+32], mm4 ; so that also helps performance + movntq [edi+40], mm5 + movntq [edi+48], mm6 + movntq [edi+56], mm7 + add edi, 64 ; update dest pointer + + dec eax ; count down + + jnz $memcpy_bp_3 ; keep copying + sub ecx, CACHEBLOCK ; update the 64-byte block count + jmp $memcpy_bp_1 ; keep processing chunks + +// The smallest copy uses the X86 "movsd" instruction, in an optimized +// form which is an "unrolled loop". Then it handles the last few bytes. +align 4 + movsd + movsd ; perform last 1-15 dword copies + movsd + movsd + movsd + movsd + movsd + movsd + movsd + movsd ; perform last 1-7 dword copies + movsd + movsd + movsd + movsd + movsd + movsd + +$memcpy_last_few: ; dword aligned from before movsds + mov ecx, ebx ; has valid low 2 bits of the byte count + and ecx, 11b ; the last few cows must come home + jz $memcpy_final ; no more, lets leave + rep movsb ; the last 1, 2, or 3 bytes + +$memcpy_final: + emms ; clean up the MMX state + sfence ; flush the write buffer + mov eax, [dest] ; ret value = destination pointer + + } +#else // !defined(_WIN64)) + + return memcpy(dest, src, n); + +#endif +} + +uint64_t +Os::offsetToEpochNanos() +{ + static uint64_t offset = 0; + + if (offset != 0) { + return offset; + } + + FILETIME ft; + GetSystemTimeAsFileTime(&ft); + + LARGE_INTEGER li; + li.LowPart = ft.dwLowDateTime; + li.HighPart = ft.dwHighDateTime; + + uint64_t now = (li.QuadPart - 116444736000000000ull) * 100; + offset = now - timeNanos(); + + return offset; +} + +#ifdef _WIN64 + +address +Os::currentStackPtr() +{ + return (address) _AddressOfReturnAddress() + sizeof(void*); +} + +#else // !_WIN64 + +#pragma warning(disable:4731) + +void __stdcall +Os::setCurrentStackPtr(address newSp) +{ + newSp -= sizeof(void*); + *(void**) newSp = *(void**) _AddressOfReturnAddress(); + __asm { + mov esp,newSp + mov ebp,[ebp] + ret + } +} + +#endif // !_WIN64 + +size_t Os::getPhysicalMemSize() +{ + MEMORYSTATUSEX statex; + + statex.dwLength = sizeof (statex); + + if (GlobalMemoryStatusEx (&statex) == 0) { + return 0; + } + + return (size_t) statex.ullTotalPhys; +} + + +} // namespace amd + +#endif // _WIN32 || __CYGWIN__ diff --git a/rocclr/compiler/lib/promotions/oclutils/os/setjmp.S b/rocclr/compiler/lib/promotions/oclutils/os/setjmp.S new file mode 100644 index 0000000000..3029b1c1e7 --- /dev/null +++ b/rocclr/compiler/lib/promotions/oclutils/os/setjmp.S @@ -0,0 +1,63 @@ +# +# Copyright (c) 2010 Advanced Micro Devices, Inc. All rights reserved. +# + + .text +.globl _StackContext_setjmp + .type _StackContext_setjmp, @function +_StackContext_setjmp: + +#if defined(_LP64) + movq (%rsp), %rsi + movq %rbx, (%rdi) + lea 8(%rsp), %rax + movq %rax, 8(%rdi) + movq %rbp, 16(%rdi) + movq %r12, 24(%rdi) + movq %r13, 32(%rdi) + movq %r14, 40(%rdi) + movq %r15, 48(%rdi) + movq %rsi, 56(%rdi) +#else // _LP64 + movl (%esp), %ecx + movl 4(%esp), %edx + movl %ebx, (%edx) + lea 4(%esp), %eax + movl %eax, 4(%edx) + movl %ebp, 8(%edx) + movl %edi, 12(%edx) + movl %esi, 16(%edx) + movl %ecx, 20(%edx) +#endif // _LP64 + xor %eax, %eax + ret + +.globl _StackContext_longjmp + .type _StackContext_longjmp, @function +_StackContext_longjmp: + +#if defined(_LP64) + mov %rsi, %rax + movq (%rdi), %rbx + movq 8(%rdi), %rsp + movq 16(%rdi), %rbp + movq 24(%rdi), %r12 + movq 32(%rdi), %r13 + movq 40(%rdi), %r14 + movq 48(%rdi), %r15 + movq 56(%rdi), %r8 + jmp *%r8 +#else // !_LP64 + movl 4(%esp), %edx + movl 8(%esp), %eax + movl (%edx), %ebx + movl 4(%edx), %esp + movl 8(%edx), %ebp + movl 12(%edx), %edi + movl 16(%edx), %esi + movl 20(%edx), %ecx + jmp *%ecx +#endif // !_LP64 + +.section .note.GNU-stack,"",%progbits + diff --git a/rocclr/compiler/lib/promotions/oclutils/os/setjmp.asm b/rocclr/compiler/lib/promotions/oclutils/os/setjmp.asm new file mode 100644 index 0000000000..dcc22a8c95 --- /dev/null +++ b/rocclr/compiler/lib/promotions/oclutils/os/setjmp.asm @@ -0,0 +1,110 @@ +; +; Copyright (c) 2010 Advanced Micro Devices, Inc. All rights reserved. +; + +ifndef _WIN64 + .386 + .model flat, c +endif ; !_WIN64 + +OPTION PROLOGUE:NONE +OPTION EPILOGUE:NONE +.code + +ifndef _WIN64 + +_StackContext_setjmp proc + mov ecx,[esp] + mov edx,4[esp] + mov [edx],ebx + lea eax,4[esp] + mov 4[edx],eax + mov 8[edx],ebp + mov 0Ch[edx],edi + mov 10h[edx],esi + mov 14h[edx],ecx + xor eax,eax + ret +_StackContext_setjmp endp + +_StackContext_longjmp proc + mov edx,4[esp] + mov eax,8[esp] + mov ebx,[edx] + mov esp,4[edx] + mov ebp,8[edx] + mov edi,0Ch[edx] + mov esi,10h[edx] + mov ecx,14h[edx] + jmp ecx +_StackContext_longjmp endp + +else ; _WIN64 + +_Os_setCurrentStackPtr proc + pop r8 + mov rsp,rcx + push r8 + ret +_Os_setCurrentStackPtr endp + +_StackContext_setjmp proc + mov r8,[rsp] + mov [rcx],rbx + lea r9,8[rsp] + mov 8[rcx],r9 + mov 10h[rcx],rbp + mov 18h[rcx],rsi + mov 20h[rcx],rdi + mov 28h[rcx],r12 + mov 30h[rcx],r13 + mov 38h[rcx],r14 + mov 40h[rcx],r15 + mov 48h[rcx],r8 + stmxcsr 50h[rcx] + fnstcw 54h[rcx] + movdqa 60h[rcx],xmm6 + movdqa 70h[rcx],xmm7 + movdqa 80h[rcx],xmm8 + movdqa 90h[rcx],xmm9 + movdqa 0A0h[rcx],xmm10 + movdqa 0B0h[rcx],xmm11 + movdqa 0C0h[rcx],xmm12 + movdqa 0D0h[rcx],xmm13 + movdqa 0E0h[rcx],xmm14 + movdqa 0F0h[rcx],xmm15 + xor rax,rax + ret +_StackContext_setjmp endp + +_StackContext_longjmp proc + mov rax,rdx + mov rbx,[rcx] + mov rsp,8[rcx] + mov rbp,10h[rcx] + mov rsi,18h[rcx] + mov rdi,20h[rcx] + mov r12,28h[rcx] + mov r13,30h[rcx] + mov r14,38h[rcx] + mov r15,40h[rcx] + mov rdx,48h[rcx] + ldmxcsr 50h[rcx] + fnclex + fldcw 54h[rcx] + movdqa xmm6,60h[rcx] + movdqa xmm7,70h[rcx] + movdqa xmm8,80h[rcx] + movdqa xmm9,90h[rcx] + movdqa xmm10,0A0h[rcx] + movdqa xmm11,0B0h[rcx] + movdqa xmm12,0C0h[rcx] + movdqa xmm13,0D0h[rcx] + movdqa xmm14,0E0h[rcx] + movdqa xmm15,0F0h[rcx] + jmp rdx +_StackContext_longjmp endp + +endif ; _WIN64 + +end diff --git a/rocclr/compiler/lib/promotions/oclutils/thread/atomic.hpp b/rocclr/compiler/lib/promotions/oclutils/thread/atomic.hpp new file mode 100644 index 0000000000..144f0b6dde --- /dev/null +++ b/rocclr/compiler/lib/promotions/oclutils/thread/atomic.hpp @@ -0,0 +1,669 @@ +// +// Copyright (c) 2009 Advanced Micro Devices, Inc. All rights reserved. +// + +/*! \file atomic.hpp + * \brief Declarations for Memory order access and Atomic operations. + * + * \author Laurent Morichetti (laurent.morichetti@amd.com) + * \date October 2008 + */ + +#ifndef ATOMIC_HPP_ +#define ATOMIC_HPP_ + +#include "top.hpp" +#include "utils/traits.hpp" + +#ifdef _WIN32 +# include +#elif defined(ATI_ARCH_X86) +# include +# include +#endif // !_WIN32 +namespace amd { + +/*! \addtogroup Threads + * @{ + * + * \defgroup MemOrder Memory ordering + * @{ + */ + +/*! \brief Memory order access operations. + */ +class MemoryOrder : AllStatic +{ +public: + /*! \brief Execute a memory fence. + * + * Perform a serializing operation on loads and stores which guarantees + * that all memory operations dispatched prior to the fence will be + * globally visible before any other memory operation following the fence. + */ + static void fence() { +# if defined(ATI_ARCH_X86) + _mm_mfence(); +# else // !ATI_ARCH_X86 + __sync_synchronize(); +# endif // !ATI_ARCH_X86 + } + + /*! \brief Execute a loads fence. + * + * Perform a serializing operation on loads which guarantees that all + * load from memory operations dispatched prior to the lfence will be + * globally visible before any other load following the lfence. + */ + static void lfence() { +# if defined(ATI_ARCH_X86) + _mm_lfence(); +# else // !ATI_ARCH_X86 + fence(); +# endif // !ATI_ARCH_X86 + } + + /*! \brief Execute a stores fence. + * + * Perform a serializing operation on stores which guarantees that all + * store to memory operations dispatched prior to the sfence will be + * globally visible before any other store following the sfence. + */ + static void sfence() { +# if defined(ATI_ARCH_X86) + _mm_sfence(); +# else // !ATI_ARCH_X86 + fence(); +# endif // !ATI_ARCH_X86 + } +}; + +/*! @} + * \addtogroup Atomic Atomic Operations + * @{ + */ + +/*! \brief Static functions for atomic operations. + */ +class AtomicOperation : AllStatic +{ +private: + + //! Template to specialize atomic intrinsics on register size. + template + struct Intrinsics { + /*! \brief %Atomic add. + * + * Atomically add \a inc to \a *dest and return the prior value. + */ + template + static inline T add(T increment, volatile T* dest); + + /*! \brief %Atomic exchange. + * + * Atomically exchange value with *dest and return the prior value. + */ + template + static inline T swap(T value, volatile T* dest); + + /*! \brief %Atomic compare and exchange. + * + * Atomically compare and xchge value with *dest if *dest == compare. + * Return the prior value. + */ + template + static inline T compareAndSwap(T compare, volatile T* dest, T value); + + /*! \brief %Atomic increment. + * + * Atomically increment *dest and return the prior value. + */ + template + static inline T increment(volatile T* dest); + + /*! \brief %Atomic exchange. + * + * Atomically decrement *dest and return the prior value. + */ + template + static inline T decrement(volatile T* dest); + + /*! \brief %Atomic or. + * + * Atomically or \a mask to \a *dest and return the prior value. + */ + template + static inline T _or(T mask, volatile T* dest); + + /*! \brief %Atomic and. + * + * Atomically and \a mask to \a *dest and return the prior value. + */ + template + static inline T _and(T mask, volatile T* dest); +}; + +public: + /*! \brief %Atomic add. + * + * Atomically add \a inc to \a *dest and return the prior value. + */ + template + static T add(typename make_arithmetic::type inc, volatile T* dest) + { + return Intrinsics::add((T) inc, dest); + } + + /*! \brief %Atomic exchange. + * + * Atomically exchange value with *dest and return the prior value. + */ + template + static T swap(T value, volatile T* dest) + { + return Intrinsics::swap(value, dest); + } + + /*! \brief %Atomic compare and exchange. + * + * Atomically compare and exchange value with *dest if *dest == compare. + * Return the prior value. + */ + template + static T compareAndSwap(T compare, volatile T* dest, T value) + { + return Intrinsics::compareAndSwap(compare, dest, value); + } + + /*! \brief %Atomic increment. + * + * Atomically increment *dest and return the prior value. + */ + template + static T increment(volatile T* dest) + { + return Intrinsics::increment(dest); + } + + /*! \brief %Atomic decrement. + * + * Atomically decrement *dest and return the prior value. + */ + template + static T decrement(volatile T* dest) + { + return Intrinsics::decrement(dest); + } + + /*! \brief %Atomic or. + * + * Atomically or \a mask to \a *dest and return the prior value. + */ + template + static T _or(typename make_arithmetic::type mask, volatile T* dest) + { + return Intrinsics::_or((T) mask, dest); + } + + /*! \brief %Atomic and. + * + * Atomically or \a mask to \a *dest and return the prior value. + */ + template + static T _and(typename make_arithmetic::type mask, volatile T* dest) + { + return Intrinsics::_and((T) mask, dest); + } +}; + +/*@}*/ + +#if defined(_MSC_VER) + +template <> +template +inline T +AtomicOperation::Intrinsics<4>::add(T increment, volatile T* dest) +{ + return (T)_InterlockedExchangeAdd( + (volatile long*)dest, (long)increment); +} + +template <> +template +inline T +AtomicOperation::Intrinsics<4>::swap(T value, volatile T* dest) +{ + return (T)_InterlockedExchange( + (volatile long*)dest, (long)value); +} + +template <> +template +inline T +AtomicOperation::Intrinsics<4>::compareAndSwap( + T compare, volatile T* dest, T value) +{ + return (T)_InterlockedCompareExchange( + (volatile long*)dest, (long)value, (long)compare); +} + +template <> +template +inline T +AtomicOperation::Intrinsics<4>::increment(volatile T* dest) +{ + return (T)(_InterlockedIncrement((volatile long*)dest) - 1L); +} + +template <> +template +inline T +AtomicOperation::Intrinsics<4>::decrement(volatile T* dest) +{ + return (T)(_InterlockedDecrement((volatile long*)dest) + 1L); +} + +template <> +template +inline T +AtomicOperation::Intrinsics<4>::_or(T mask, volatile T* dest) +{ + return (T)_InterlockedOr( + (volatile long*)dest, (long)mask); +} + +template <> +template +inline T +AtomicOperation::Intrinsics<4>::_and(T mask, volatile T* dest) +{ + return (T)_InterlockedAnd( + (volatile long*)dest, (long)mask); +} + +#ifdef _WIN64 + +template <> +template +inline T +AtomicOperation::Intrinsics<8>::add(T increment, volatile T* dest) +{ + return (T)_InterlockedExchangeAdd64( + (volatile __int64*)dest, (__int64)increment); +} + +template <> +template +inline T +AtomicOperation::Intrinsics<8>::swap(T value, volatile T* dest) +{ + return (T)_InterlockedExchange64( + (volatile __int64*)dest, (__int64)value); +} + +template <> +template +inline T +AtomicOperation::Intrinsics<8>::compareAndSwap( + T compare, volatile T* dest, T value) +{ + return (T)_InterlockedCompareExchange64( + (volatile __int64*)dest, (__int64)value, (__int64)compare); +} + +template <> +template +inline T +AtomicOperation::Intrinsics<8>::increment(volatile T* dest) +{ + return (T)(_InterlockedIncrement64((volatile __int64*)dest) - 1LL); +} + +template <> +template +inline T +AtomicOperation::Intrinsics<8>::decrement(volatile T* dest) +{ + return (T)(_InterlockedDecrement64((volatile __int64*)dest) + 1LL); +} + +template <> +template +inline T +AtomicOperation::Intrinsics<8>::_or(T mask, volatile T* dest) +{ + return (T)_InterlockedOr64( + (volatile long*)dest, (long)mask); +} + +template <> +template +inline T +AtomicOperation::Intrinsics<8>::_and(T mask, volatile T* dest) +{ + return (T)_InterlockedAnd64( + (volatile long*)dest, (long)mask); +} + +#endif // _LP64 + +#elif defined(__GNUC__) + +template +template +inline T +AtomicOperation::Intrinsics::add(T inc, volatile T* dest) +{ + return __sync_fetch_and_add(dest, inc); +} + +template +template +inline T +AtomicOperation::Intrinsics::swap(T value, volatile T* dest) +{ + return __sync_lock_test_and_set(dest, value); +} + +template +template +inline T +AtomicOperation::Intrinsics::compareAndSwap( + T compare, volatile T* dest, T value) +{ + return __sync_val_compare_and_swap(dest, compare, value); +} + +template +template +inline T +AtomicOperation::Intrinsics::increment(volatile T* dest) +{ + return add(T(1), dest); +} + +template +template +inline T +AtomicOperation::Intrinsics::decrement(volatile T* dest) +{ + return add(T(-1), dest); +} + +template +template +inline T +AtomicOperation::Intrinsics::_or(T mask, volatile T* dest) +{ + return __sync_fetch_and_or(dest, mask); +} + +template +template +inline T +AtomicOperation::Intrinsics::_and(T mask, volatile T* dest) +{ + return __sync_fetch_and_and(dest, mask); +} + +#else +# error Unimplemented +#endif + +/*! \addtogroup Atomic Atomic Operations + * @{ + */ + +/*! \brief A variable of type T with atomic properties. + */ +template +class Atomic +{ +private: + + typedef typename add_volatile::type value_type; + value_type value_; //!< \brief The variable. + +public: + //! Construct a new %Atomic variable of type T. + Atomic() : value_(T(0)) {} + //! Construct a new %Atomic variable of type T from \a value. + Atomic(T value) : value_(value) {} + //! Construct a new %Atomic variable of type T from another %Atomic. + Atomic(const Atomic& atomic) : value_(atomic.value_) { } + //! Copy value into this %Atomic variable. + Atomic& operator = (T value) + { + value_ = value; + return *this; + } + + //! Return the %Atomic variable value. + operator T () const { return T(value_); } + //! Return the %Atomic variable value. + T operator ->() const { return T(value_); } + //! Return the %Atomic variable's address. + typename add_pointer::type operator &() { return &value_; } + + //! Atomically add \a inc to this variable. + Atomic& operator += (typename make_arithmetic::type inc) + { + if (is_pointer::value) { + inc *= sizeof(typename remove_pointer::type); + } + AtomicOperation::add(inc, &value_); + return *this; + } + + //! Atomically subtract \a inc to this variable. + Atomic& operator -= (typename make_arithmetic::type inc) + { + typename make_arithmetic::type modifier = 0; + if (is_pointer::value) { + inc *= sizeof(typename remove_pointer::type); + } + AtomicOperation::add(modifier - inc, &value_); + return *this; + } + + //! Atomically OR \a value to this variable. + Atomic& operator |= (typename make_arithmetic::type mask) + { + AtomicOperation::_or(mask, &value_); + return *this; + } + + //! Atomically AND \a value to this variable. + Atomic& operator &= (typename make_arithmetic::type mask) + { + AtomicOperation::_and(mask, &value_); + return *this; + } + + //! Atomically increment this variable and return its new value. + typename remove_reference::type operator ++ () + { + if (is_pointer::value) { + typename make_arithmetic::type inc = 1; + return AtomicOperation::add( + inc * sizeof(typename remove_pointer::type), &value_) + 1; + } + else { + return AtomicOperation::increment(&value_) + 1; + } + } + + //! Atomically decrement this variable and return its new value. + typename remove_reference::type operator -- () + { + if (is_pointer::value) { + typename make_arithmetic::type inc = -1; + return AtomicOperation::add( + inc * sizeof(typename remove_pointer::type), &value_) - 1; + } + else { + return AtomicOperation::decrement(&value_) - 1; + } + } + + //! Atomically increment this variable and return its previous value. + typename remove_reference::type operator ++ (int) + { + if (is_pointer::value) { + typename make_arithmetic::type inc = 1; + return AtomicOperation::add( + inc * sizeof(typename remove_pointer::type), &value_); + } + else { + return AtomicOperation::increment(&value_); + } + } + + //! Atomically decrement this variable and return its previous value. + T operator -- (int) + { + if (is_pointer::value) { + typename make_arithmetic::type inc = -1; + return AtomicOperation::add( + inc * sizeof(typename remove_pointer::type), &value_); + } + else { + return AtomicOperation::decrement(&value_); + } + } + + /*! \brief Atomically compare this variable with \a compare and set + * to value if equals + */ + bool compareAndSet(T compare, T value) + { + return compare == AtomicOperation::compareAndSwap( + compare, &value_, value); + } + + //! Atomically set this variable to \a value and return its previous value. + T swap(T value) + { + return AtomicOperation::swap(value, &value_); + } + + /*! \brief Execute a stores fence followed by a store to this variable. + * + * This storeRelease operation ensures that all store to memory operations + * preceding this function will be globally visible before the update to + * this variable's value. + */ + void storeRelease(T value) + { + MemoryOrder::fence(); + value_ = value; + } + + /*! \brief Execute a load from this variable followed by a loads fence. + * + * This loadAcquire operation ensures that all load from memory operations + * following this function will be globally visible after the read from + * this variable's value. + */ + T loadAcquire() const + { + T value = value_; + MemoryOrder::fence(); + return value; + } +}; + +//! Helper function to tie an Atomic to a variable of type T. +template +inline Atomic +make_atomic(T& t) +{ + return Atomic(t); +} + + +template +class AtomicMarkableReference +{ +private: + static const intptr_t kMarkBitMask = 0x1; + +private: + Atomic reference_; + +private: + static intptr_t markMask(bool mark) + { + return mark ? kMarkBitMask : intptr_t(0); + } + +public: + AtomicMarkableReference() + : reference_(NULL) + { } + + AtomicMarkableReference(T* ptr, bool mark = false) + : reference_((T*)((intptr_t) ptr | markMask(mark))) + { } + + bool compareAndSet( + T* expectedPtr, T* newPtr, + bool expectedMark, bool newMark) + { + return reference_.compareAndSet( + (T*)((intptr_t) expectedPtr | markMask(expectedMark)), + (T*)((intptr_t) newPtr | markMask(newMark))); + } + + pair swap(T* newPtr, bool newMark) + { + T* prev = reference_.swap( + (T*)((intptr_t) newPtr | markMask(newMark))); + return make_pair( + (T*) ((intptr_t) prev & ~kMarkBitMask), + ((intptr_t) prev & kMarkBitMask) != 0); + } + + bool tryMark(T* expectedPtr, bool newMark) + { + T* current = reference_; + if (((intptr_t) current & ~kMarkBitMask) != (intptr_t) expectedPtr) { + return false; + } + bool currentMark = ((intptr_t) current & kMarkBitMask) != 0; + return currentMark == newMark || reference_.compareAndSet(current, + (T*)((intptr_t) expectedPtr | markMask(newMark))); + } + + bool isMarked() const + { + return ((intptr_t)(T*) reference_ & kMarkBitMask) != 0; + } + + pair get() const + { + T* current = reference_; + return make_pair( + (T*) ((intptr_t) current & ~kMarkBitMask), + ((intptr_t) current & kMarkBitMask) != 0); + } + + T* getReference() const + { + return (T*) ((intptr_t)(T*) reference_ & ~kMarkBitMask); + } + + void set(T* ptr, bool mark) + { + reference_ = (T*)((intptr_t) ptr | markMask(mark)); + } +}; + +/*! @} + * @} + */ + +} // namespace amd + +#endif /*ATOMIC_HPP_*/ diff --git a/rocclr/compiler/lib/promotions/oclutils/thread/monitor.cpp b/rocclr/compiler/lib/promotions/oclutils/thread/monitor.cpp new file mode 100644 index 0000000000..571d5e2a18 --- /dev/null +++ b/rocclr/compiler/lib/promotions/oclutils/thread/monitor.cpp @@ -0,0 +1,311 @@ +// +// Copyright (c) 2008 Advanced Micro Devices, Inc. All rights reserved. +// + +#include "thread/monitor.hpp" +#include "thread/atomic.hpp" +#include "thread/semaphore.hpp" +#include "thread/thread.hpp" +#include "utils/util.hpp" + +#include + +namespace amd { + +Monitor::Monitor(const char* name, bool recursive) : + contendersList_(NULL), onDeck_(NULL), waitersList_(NULL), + owner_(NULL), recursive_(recursive) +{ + const size_t maxNameLen = sizeof(name_); + if (name == NULL) { + const char* unknownName = "@unknown@"; + assert(sizeof(unknownName) < maxNameLen && "just checking"); + strcpy(name_, unknownName); + } + else { + strncpy(name_, name, maxNameLen - 1); + name_[maxNameLen - 1] = '\0'; + } +} + +bool +Monitor::trySpinLock() +{ + if (tryLock()) { + return true; + } + + for (int s = kMaxSpinIter; s > 0; --s) { + // First, be SMT friendly + if (s >= (kMaxSpinIter - kMaxReadSpinIter)) { + Os::spinPause(); + } + // and then SMP friendly + else { + Thread::yield(); + } + if (!isLocked()) { + return tryLock(); + } + } + + // We could not acquire the lock in the spin loop. + return false; +} + +void +Monitor::finishLock() +{ + Thread* thread = Thread::current(); + assert(thread != NULL && "cannot lock() from (null)"); + + if (trySpinLock()) { + return; // We succeeded, we are done. + } + + /* The lock is contended. Push the thread's semaphore onto + * the contention list. + */ + Semaphore& sem = thread->lockSemaphore(); + sem.reset(); + + LinkedNode newHead; + newHead.setItem(&sem); + + while (true) { + LinkedNode* head; bool isLocked; + + // The assumption is that lockWord is locked. Make sure we do not + // continue unless the lock bit is set. + tie(head, isLocked) = contendersList_.get(); + if (!isLocked) { + if (tryLock()) { + return; + } + continue; + } + + // Set the new contention list head if lockWord is unchanged. + newHead.setNext(head); + if (contendersList_.compareAndSet(head, &newHead, kLocked, kLocked)) { + break; + } + + // We failed the CAS. yield/pause before trying again. + Thread::yield(); + } + + int32_t spinCount = 0; + // Go to sleep until we become the on-deck thread. + while (onDeck_.getReference() != &sem) { + // First, be SMT friendly + if (spinCount < kMaxReadSpinIter) { + Os::spinPause(); + } + // and then SMP friendly + else if (spinCount < kMaxSpinIter) { + Thread::yield(); + } + // now go to sleep + else { + sem.wait(); + } + spinCount++; + } + + spinCount = 0; + // + // From now-on, we are the on-deck thread. It will stay that way until + // we successfuly acquire the lock. + // + while (true) { + assert(onDeck_.getReference() == &sem && "just checking"); + if (tryLock()) { + break; + } + + // Somebody beat us to it. Since we are on-deck, we can just go + // back to sleep. + // First, be SMT friendly + if (spinCount < kMaxReadSpinIter) { + Os::spinPause(); + } + // and then SMP friendly + else if (spinCount < kMaxSpinIter) { + Thread::yield(); + } + // now go to sleep + else { + sem.wait(); + } + spinCount++; + } + + assert(newHead.next() == NULL && "Should not be linked"); + onDeck_ = NULL; +} + +void +Monitor::finishUnlock() +{ + // If we get here, it means that there might be a thread in the contention + // list waiting to acquire the lock. We need to select a successor and + // place it on-deck. + + while (true) { + // Grab the onDeck_ microlock to protect the next loop (make sure only + // one semaphore is removed from the contention list). + // + if (!onDeck_.compareAndSet(NULL, NULL, kUnlocked, kLocked)) { + return; // Somebody else has the microlock, let him select onDeck_ + } + + LinkedNode* head; bool isLocked; + while (true) { + tie(head, isLocked) = contendersList_.get(); + + if (head == NULL) { + break; // There's nothing else to do. + } + + if (isLocked) { + // Somebody could have acquired then released the lock + // and failed to grab the onDeck_ microlock. + head = NULL; + break; + } + + if (contendersList_.compareAndSet( + head, head->next(), kUnlocked, kUnlocked)) { + #ifdef ASSERT + head->setNext(NULL); + #endif // ASSERT + break; + } + } + + Semaphore* sem = (head != NULL) ? head->item() : NULL; + onDeck_ = sem; + MemoryOrder::fence(); + // + // Release the onDeck_ microlock (end of critical region); + + if (sem != NULL) { + sem->post(); + return; + } + + // We do not have an on-deck thread (sem == NULL). Return if + // the contention list is empty or if the lock got acquired again. + tie(head, isLocked) = contendersList_.get(); + if (isLocked || head == NULL) { + return; + } + } +} + +void +Monitor::wait() +{ + Thread* thread = Thread::current(); + assert(isLocked() && owner_ == thread && "just checking"); + + // Add the thread's resume semaphore to the list. + Semaphore& suspend = thread->suspendSemaphore(); + suspend.reset(); + + LinkedNode newHead; + newHead.setItem(&suspend); + newHead.setNext(waitersList_); + waitersList_ = &newHead; + + // Preserve the lock count (for recursive mutexes) + uint32_t lockCount = lockCount_; + lockCount_ = 1; + + // Release the lock and go to sleep. + unlock(); + + // Go to sleep until we become the on-deck thread. + int32_t spinCount = 0; + while (onDeck_.getReference() != &suspend) { + // First, be SMT friendly + if (spinCount < kMaxReadSpinIter) { + Os::spinPause(); + } + // and then SMP friendly + else if (spinCount < kMaxSpinIter) { + Thread::yield(); + } + // now go to sleep + else { + suspend.wait(); + } + spinCount++; + } + + spinCount = 0; + while (true) { + assert(onDeck_.getReference() == &suspend && "just checking"); + + if (trySpinLock()) { + break; + } + + // Somebody beat us to it. Since we are on-deck, we can just go + // back to sleep. + // First, be SMT friendly + if (spinCount < kMaxReadSpinIter) { + Os::spinPause(); + } + // and then SMP friendly + else if (spinCount < kMaxSpinIter) { + Thread::yield(); + } + // now go to sleep + else { + suspend.wait(); + } + spinCount++; + } + + // Restore the lock count (for recursive mutexes) + lockCount_ = lockCount; + + onDeck_ = NULL; + MemoryOrder::fence(); +} + +void +Monitor::notify() +{ + assert(isLocked() && owner_ == Thread::current() && "just checking"); + + LinkedNode* waiter = waitersList_; + if (waiter == NULL) { + return; + } + + // Dequeue a waiter from the wait list and add it to the contention list. + waitersList_ = waiter->next(); + while (true) { + LinkedNode* node = contendersList_.getReference(); + + waiter->setNext(node); + if (contendersList_.compareAndSet(node, waiter, kLocked, kLocked)) { + break; + } + } +} + +void +Monitor::notifyAll() +{ + // NOTE: We could CAS the whole list in 1 shot but this is + // not critical code. Optimize this if it becomes hot. + while (waitersList_ != NULL) { + notify(); + } +} + +} // namespace amd diff --git a/rocclr/compiler/lib/promotions/oclutils/thread/monitor.hpp b/rocclr/compiler/lib/promotions/oclutils/thread/monitor.hpp new file mode 100644 index 0000000000..5525a18dfa --- /dev/null +++ b/rocclr/compiler/lib/promotions/oclutils/thread/monitor.hpp @@ -0,0 +1,230 @@ +// +// Copyright (c) 2008 Advanced Micro Devices, Inc. All rights reserved. +// + +#ifndef MONITOR_HPP_ +#define MONITOR_HPP_ + +#include "top.hpp" +#include "atomic.hpp" +#include "thread/semaphore.hpp" +#include "thread/thread.hpp" + +namespace amd { + +/*! \addtogroup Threads + * @{ + * + * \addtogroup Synchronization + * @{ + */ + +class Monitor : public HeapObject +{ + typedef SimplyLinkedNode LinkedNode; + +private: + static const bool kUnlocked = false; + static const bool kLocked = true; + + static const int kMaxSpinIter = 55; //!< Total number of spin iterations. + static const int kMaxReadSpinIter = 50; //!< Read iterations before yielding + + /*! Linked list of semaphores the contending threads are waiting on + * and main lock. + */ + AtomicMarkableReference contendersList_; + //! The Mutex's name + char name_[64]; + + //! Semaphore of the next thread to contend for the lock. + AtomicMarkableReference onDeck_; + //! Linked list of the suspended threads resume semaphores. + LinkedNode* volatile waitersList_; + + //! Thread owning this monitor. + Thread* volatile owner_; + //! The amount of times this monitor was acquired by the owner. + uint32_t lockCount_; + //! True if this is a recursive mutex, false otherwise. + const bool recursive_; + +private: + //! Finish locking the mutex (contented case). + void finishLock(); + //! Finish unlocking the mutex (contented case). + void finishUnlock(); + +protected: + //! Try to spin-acquire the lock, return true if successful. + bool trySpinLock(); + + /*! \brief Return true if the lock is owned. + * + * \note The user is responsible for the memory ordering. + */ + bool isLocked() const { return contendersList_.isMarked(); } + + //! Return this monitor's owner thread (NULL if unlocked). + Thread* owner() const { return owner_; } + + //! Set the owner. + void setOwner(Thread* thread) { owner_ = thread; } + +public: + explicit Monitor(const char* name = NULL, bool recursive = false); + ~Monitor() {} + + //! Try to acquire the lock, return true if successful. + inline bool tryLock(); + + //! Acquire the lock or suspend the calling thread. + inline void lock(); + + //! Release the lock and wake a single waiting thread if any. + inline void unlock(); + + /*! \brief Give up the lock and go to sleep. + * + * Calling wait() causes the current thread to go to sleep until + * another thread calls notify()/notifyAll(). + * + * \note The monitor must be owned before calling wait(). + */ + void wait(); + /*! \brief Wake up a single thread waiting on this monitor. + * + * \note The monitor must be owned before calling notify(). + */ + void notify(); + /*! \brief Wake up all threads that are waiting on this monitor. + * + * \note The monitor must be owned before calling notifyAll(). + */ + void notifyAll(); + + //! Return this lock's name. + const char* name() const { return name_; } +}; + +class ScopedLock : StackObject +{ +private: + Monitor* lock_; + +public: + ScopedLock(Monitor& lock) + : lock_(&lock) + { + lock_->lock(); + } + + ScopedLock(Monitor* lock) + : lock_(lock) + { + if (lock_) lock_->lock(); + } + + ~ScopedLock() + { + if (lock_) lock_->unlock(); + } +}; + +/*! @} + * @} + */ + +inline bool +Monitor::tryLock() +{ + Thread* thread = Thread::current(); + assert(thread != NULL && "cannot lock() from (null)"); + + LinkedNode* ptr; bool isLocked; + tie(ptr, isLocked) = contendersList_.get(); + + if (unlikely(isLocked)) { + if (recursive_ && thread == owner_) { + // Recursive lock: increment the lock count and return. + ++lockCount_; + return true; + } + return false; // Already locked! + } + + if (unlikely(!contendersList_.compareAndSet( + ptr, ptr, kUnlocked, kLocked))) { + return false; // We failed the CAS from unlocked to locked. + } + + setOwner(thread); // cannot move above the CAS. + lockCount_ = 1; + + return true; +} + +inline void +Monitor::lock() +{ + if (unlikely(!tryLock())) { + // The lock is contented. + finishLock(); + } + + // This is the beginning of the critical region. From now-on, everything + // executes single-threaded! + // +} + +inline void +Monitor::unlock() +{ + assert(isLocked() && owner_ == Thread::current() && "invariant"); + + if (recursive_ && --lockCount_ > 0) { + // was a recursive lock case, simply return. + return; + } + + setOwner(NULL); + + while (true) { + LinkedNode* ptr = contendersList_.getReference(); + // Clear the lock bit. + if (contendersList_.compareAndSet(ptr, ptr, kLocked, kUnlocked)) { + break; // We succeeded the CAS from locked to unlocked. + } + } + // + // This is the end of the critical region. + + // Check if we have an on-deck thread that needs signaling. + Semaphore* onDeck; bool isMarked; + tie(onDeck, isMarked) = onDeck_.get(); + if (onDeck != NULL) { + if (!isMarked) { + // Only signal if it is unmarked. + onDeck->post(); + } + return; // We are done. + } + + // We do not have an on-deck thread yet, we might have to walk the list in + // order to select the next onDeck_. Only one thread needs to fill onDeck_, + // so return if the list is empty or if the lock got acquired again (it's + // somebody else's problem now!) + + LinkedNode* head; bool isLocked; + amd::tie(head, isLocked) = contendersList_.get(); + if (isLocked || head == NULL) { + return; + } + + // Finish the unlock operation: find a thread to wake up. + finishUnlock(); +} + +} // namespace amd + +#endif /*MONITOR_HPP_*/ diff --git a/rocclr/compiler/lib/promotions/oclutils/thread/semaphore.cpp b/rocclr/compiler/lib/promotions/oclutils/thread/semaphore.cpp new file mode 100644 index 0000000000..f6d1a0955b --- /dev/null +++ b/rocclr/compiler/lib/promotions/oclutils/thread/semaphore.cpp @@ -0,0 +1,96 @@ +// +// Copyright (c) 2008,2010 Advanced Micro Devices, Inc. All rights reserved. +// + +#include "thread/semaphore.hpp" +#include "thread/atomic.hpp" +#include "thread/thread.hpp" + +#if defined(_WIN32) || defined(__CYGWIN__) +# include +#else // !_WIN32 +# include +# include +#endif // !_WIN32 + +namespace amd { + +Semaphore::Semaphore() + : state_(0) +{ +#ifdef _WIN32 + handle_ = static_cast(CreateSemaphore(NULL, 0, LONG_MAX, NULL)); + assert(handle_ != NULL && "CreateSemaphore failed"); +#else // !_WIN32 + if (sem_init(&sem_, 0, 0) != 0) { + fatal("sem_init() failed"); + } +#endif // !_WIN32 +} + +Semaphore::~Semaphore() +{ +#ifdef _WIN32 + if (!CloseHandle(static_cast(handle_))) { + fatal("CloseHandle() failed"); + } +#else // !_WIN32 + if (sem_destroy(&sem_) != 0) { + fatal("sem_destroy() failed"); + } +#endif // !WIN32 +} + +void +Semaphore::post() +{ + int state; + while (true) { + state = state_; + if (state > 0) { + // Do a load acquire. + MemoryOrder::fence(); + if (state == state_) { + return; + } + continue; + } + if (state_.compareAndSet(state, state+1)) { + break; + } + } + + if (state < 0) { + // We have threads waiting on this event. +#ifdef _WIN32 + ReleaseSemaphore(static_cast(handle_), 1, NULL); +#else // !_WIN32 + if (0 != sem_post(&sem_)) { + fatal("sem_post() failed"); + } +#endif // !_WIN32 + } +} + +void +Semaphore::wait() +{ + if (state_-- > 0) { + return; + } + +#ifdef _WIN32 + if (WAIT_OBJECT_0 != WaitForSingleObject( + static_cast(handle_), INFINITE)) { + fatal("WaitForSingleObject failed"); + } +#else // !_WIN32 + while (0 != sem_wait(&sem_)) { + if (EINTR != errno) { + fatal("sem_wait() failed"); + } + } +#endif // !_WIN32 +} + +} // namespace amd diff --git a/rocclr/compiler/lib/promotions/oclutils/thread/semaphore.hpp b/rocclr/compiler/lib/promotions/oclutils/thread/semaphore.hpp new file mode 100644 index 0000000000..ede63ff2b5 --- /dev/null +++ b/rocclr/compiler/lib/promotions/oclutils/thread/semaphore.hpp @@ -0,0 +1,65 @@ +// +// Copyright (c) 2008,2010 Advanced Micro Devices, Inc. All rights reserved. +// + +#ifndef SEMAPHORE_HPP_ +#define SEMAPHORE_HPP_ + +#include "top.hpp" +#include "thread/atomic.hpp" +#include "utils/util.hpp" + +#if defined(__linux__) +# include +#endif /*linux*/ + + +namespace amd { + +/*! \addtogroup Threads + * @{ + * + * \addtogroup Synchronization + * @{ + */ + +class Thread; + +//! \brief Counting semaphore +class Semaphore : public HeapObject +{ +private: + Atomic state_; //!< This semaphore's value. + +#ifdef _WIN32 + void* handle_; //!< The semaphore object's handle. + char padding_[64-sizeof(void*)-sizeof(Atomic)]; +#else // !_WIN32 + sem_t sem_; //!< The semaphore object's identifier. + char padding_[64-sizeof(sem_t)-sizeof(Atomic)]; +#endif /*!_WIN32*/ + +public: + Semaphore(); + ~Semaphore(); + + //! \brief Decrement this semaphore + void wait(); + + //! \brief Increment this semaphore + void post(); + + //! \brief Reset this semaphore. + void reset() + { + state_.swap(0); + } +}; + +/*! @} + * @} + */ + +} // namespace amd + +#endif /*SEMAPHORE_HPP_*/ diff --git a/rocclr/compiler/lib/promotions/oclutils/thread/thread.cpp b/rocclr/compiler/lib/promotions/oclutils/thread/thread.cpp new file mode 100644 index 0000000000..ee1e6abde3 --- /dev/null +++ b/rocclr/compiler/lib/promotions/oclutils/thread/thread.cpp @@ -0,0 +1,191 @@ +// +// Copyright (c) 2008 Advanced Micro Devices, Inc. All rights reserved. +// + +#include "thread/thread.hpp" +#include "thread/semaphore.hpp" +#include "thread/monitor.hpp" +#include "os/os.hpp" + +#if defined(_WIN32) || defined(__CYGWIN__) +# include +#endif // _WIN32 + +namespace amd { + +HostThread::HostThread() + : Thread("HostThread", 0, false) +{ + setCurrent(); + Os::currentStackInfo(&stackBase_, &stackSize_); + setState(RUNNABLE); +} + +void +Thread::create() +{ + created_ = new Semaphore(); + lock_ = new Semaphore(); + suspend_ = new Semaphore(); + + selfSuspendLock_ = new Monitor(); + + data_ = NULL; + handle_ = NULL; + setState(CREATED); +} + +Thread::Thread(const std::string& name, size_t stackSize, bool spawn) + : handle_(NULL), name_(name), stackSize_(stackSize) +{ + create(); + + if (!spawn) return; + + if ((handle_ = Os::createOsThread(this))) { + // Now we need to wait for Thread::main to report back. + while (state() != Thread::INITIALIZED) { + created_->wait(); + } + } +} + +Thread::~Thread() +{ +#if defined(_WIN32) + if (handle_ != NULL) { + ::CloseHandle((HANDLE) handle_); + } +#endif + delete created_; + delete lock_; + delete suspend_; + + delete selfSuspendLock_; +} + +void* +Thread::main() +{ +#ifdef DEBUG + Os::setCurrentThreadName(name().c_str()); +#endif // DEBUG + Os::currentStackInfo(&stackBase_, &stackSize_); + setCurrent(); + + // Notify the parent thread that we are up and running. + { + ScopedLock sl(selfSuspendLock_); + setState(INITIALIZED); + created_->post(); + selfSuspendLock_->wait(); + } + + if (state() == RUNNABLE) { + run(data_); + } + + setState(FINISHED); + return NULL; +} + +bool +Thread::start(void* data) +{ + if (state() != INITIALIZED) { + return false; + } + + data_ = data; + { + ScopedLock sl(selfSuspendLock_); + setState(RUNNABLE); + selfSuspendLock_->notify(); + } + + return true; +} + +void +Thread::resume() +{ + ScopedLock sl(selfSuspendLock_); + selfSuspendLock_->notify(); +} + +#if defined(__linux__) + +namespace details { + +__thread Thread* thread_ __attribute__((tls_model("initial-exec"))); + +} // namespace details + +void +Thread::registerStack(address base, address top) +{ + // Nothing to do. +} + +void +Thread::setCurrent() +{ + details::thread_ = this; +} + +#elif defined(_WIN32) + +namespace details { + +#if defined(USE_DECLSPEC_THREAD) +__declspec(thread) Thread* thread_; +#else // !USE_DECLSPEC_THREAD +DWORD threadIndex_ = TlsAlloc(); +#endif // !USE_DECLSPEC_THREAD + +} // namespace details + +void +Thread::registerStack(address base, address top) +{ + // Nothing to do. +} + +void +Thread::setCurrent() +{ +#if defined(USE_DECLSPEC_THREAD) + details::thread_ = this; +#else // !USE_DECLSPEC_THREAD + TlsSetValue(details::threadIndex_, this); +#endif // !USE_DECLSPEC_THREAD +} + +#endif + +bool +Thread::init() +{ + static bool initialized_ = false; + + // We could use InitOnceExecuteOnce/pthread_once here: + if (initialized_) { + return true; + } + initialized_ = true; + + // Register the main thread + return NULL != new HostThread(); +} + +void +Thread::tearDown() +{ +#if defined(_WIN32) && !defined(USE_DECLSPEC_THREAD) + if (details::threadIndex_ != TLS_OUT_OF_INDEXES) { + TlsFree(threadIndex_); + } +#endif // _WIN32 && !USE_DECLSPEC_THREAD +} + +} // namespace amd diff --git a/rocclr/compiler/lib/promotions/oclutils/thread/thread.hpp b/rocclr/compiler/lib/promotions/oclutils/thread/thread.hpp new file mode 100644 index 0000000000..11dad2b562 --- /dev/null +++ b/rocclr/compiler/lib/promotions/oclutils/thread/thread.hpp @@ -0,0 +1,228 @@ +// +// Copyright (c) 2008 Advanced Micro Devices, Inc. All rights reserved. +// + +#ifndef THREAD_HPP_ +#define THREAD_HPP_ + +#include "top.hpp" +#include "thread/semaphore.hpp" +#include "os/os.hpp" + +#include + +#if defined(_WIN32) +# define USE_DECLSPEC_THREAD 1 +# if !defined(USE_DECLSPEC_THREAD) +# include +# endif /*!USE_DECLSPEC_THREAD*/ +#endif /*_WIN32*/ + +namespace amd { + +/*! \addtogroup Threads Threading package + * @{ + * + * \addtogroup OsThread Native Threads + * @{ + */ + +class Monitor; + +class Thread : public HeapObject +{ + friend const void* Os::createOsThread(Thread*); + +public: + enum ThreadState + { + CREATED, + INITIALIZED, + RUNNABLE, + SUSPENDED, + FINISHED, + FAILED + }; + +private: + //! System thread handle. + const void* handle_; + //! The thread's name. + const std::string name_; + //! Current running state. + volatile ThreadState state_; + //! The argument passed to run() + void* data_; + + //! \cond ignore + Semaphore* created_; //!< To notify the parent thread. + Semaphore* lock_; //!< For mutex support (during contention). + Semaphore* suspend_; //!< For wait/suspend support. + //! \endcond + + Monitor* selfSuspendLock_; //!< For self suspend/resume. + +protected: + address stackBase_; //!< Main stack base. + size_t stackSize_; //!< Main stack size. + +private: + + /*! \brief The start wrapper for all newly create threads. + * This is called from the pthread_create start_thread. + */ + static void* entry(Thread* thread); + + /*! \brief Thread main (called from the main function). + * Setup the thread for running and wait for the semaphore to be signaled. + */ + void* main(); + + //! The entry point for this thread. + virtual void run(void* data) = 0; + +protected: + //! Bring this thread to the created state. + void create(); + + //! Set the current thread state. + void setState(ThreadState state) { state_ = state; } + + //! Set the thread-local _thread variable (used by current()). + void setCurrent(); + + //! Register the given memory region as a valid stack. + void registerStack(address base, address top); + + /*! \brief Construct a new thread. + * If \a spawn is false, do not create a new OS thread, instead, + * bind to the currently running on. + */ + explicit Thread( + const std::string& name, + size_t stackSize = 0 /*use system default*/, + bool spawn = true /* create a new Os::thread */); + + //! Destroy this thread. + virtual ~Thread(); + +public: + //! Return the currently running thread instance. + static inline Thread* current(); + + //! Initialize the OsThread package. + static bool init(); + + //! Tear down the OsThread package. + static void tearDown(); + + //! Return the thread's name + const std::string& name() const { return name_; } + + //! Get the system thread handle. + const void* handle() const { return handle_; } + + //! Start the thread execution + bool start(void *data = NULL); + + //! Resume the thread + void resume(); + + //! Return true is this is the host thread. + virtual bool isHostThread() const { return false; } + + //! Get the current thread state. + ThreadState state() const { return state_; } + + //! Return this thread's stack base. + address stackBase() const { return stackBase_; } + //! Return this thread's stack size. + size_t stackSize() const { return stackSize_; } + //! Return this thread's stack bottom. + address stackBottom() const { return stackBase() - stackSize(); } + + //! Return this thread's contend semaphore. + Semaphore& lockSemaphore() const { return *lock_; } + //! Return this thread's resume semaphore. + Semaphore& suspendSemaphore() const { return *suspend_; } + + //! Set this thread's affinity to the given cpu. + void setAffinity(uint cpu_id) const + { + Os::setThreadAffinity(handle_, cpu_id); + } + + //! Set this thread's affinity to the given cpu mask. + void setAffinity(const Os::ThreadAffinityMask& mask) const + { + Os::setThreadAffinity(handle_, mask); + } + + //! Yield to threads of the same priority of higher + static void yield() + { + Os::yield(); + } +}; + +class HostThread : public Thread +{ +private: + //! A HostThread does not have a run function + virtual void run(void* data) { ShouldNotCallThis(); } + +public: + //! Construct a new HostThread + HostThread(); + + //! Return true is this is the host thread. + bool isHostThread() const { return true; }; +}; + +/*! @} + * @} + */ + +namespace details { + +#if defined(__linux__) + +extern __thread Thread* thread_ __attribute__((tls_model("initial-exec"))); + +static inline Thread* +currentThread() +{ + return thread_; +} + +#elif defined(_WIN32) + +#if defined(USE_DECLSPEC_THREAD) +extern __declspec(thread) Thread* thread_; +#else // !USE_DECLSPEC_THREAD +extern DWORD threadIndex_; +#endif // !USE_DECLSPEC_THREAD + +static inline Thread* +currentThread() +{ +#if defined(USE_DECLSPEC_THREAD) + return thread_; +#else // !USE_DECLSPEC_THREAD + return (Thread*) TlsGetValue(threadIndex_); +#endif // !USE_DECLSPEC_THREAD +} + +#endif // _WIN32 + +} // namespace details + +inline Thread* +Thread::current() +{ + return details::currentThread(); +} + +} // namespace amd + +#endif /*THREAD_HPP_*/ diff --git a/rocclr/compiler/lib/promotions/oclutils/top.hpp b/rocclr/compiler/lib/promotions/oclutils/top.hpp new file mode 100644 index 0000000000..62f9048c47 --- /dev/null +++ b/rocclr/compiler/lib/promotions/oclutils/top.hpp @@ -0,0 +1,219 @@ +// +// Copyright (c) 2008 Advanced Micro Devices, Inc. All rights reserved. +// + +#ifndef TOP_HPP_ +#define TOP_HPP_ + +#if defined(ATI_ARCH_ARM) +# define __EXPORTED_HEADERS__ 1 +#endif /*ATI_ARCH_ARM*/ + +#ifdef _WIN32 +#define NOMINMAX 1 +#define WIN32_LEAN_AND_MEAN 1 +#endif /*_WIN32*/ + +#include "utils/macros.hpp" +#if 0 // FIXME_lmoriche +#include "CL/opencl.h" +#include "amdocl/cl_open_video_amd.h" +#endif + +#ifdef _WIN32 +# include +#else /*!_WIN32*/ +# include +#endif /*!_WIN32*/ + +#if !defined(ATI_ARCH_ARM) +#include +#endif /*!ATI_ARCH_ARM*/ +#include +#include + +typedef unsigned char* address; +typedef const unsigned char* const_address; +typedef void * pointer; +typedef const void * const_pointer; +typedef unsigned int uint; +typedef unsigned long ulong; +typedef const char* cstring; + + +#ifdef _WIN32 +#if _MSC_VER >= 1600 +# include +#else // _MSC_VER < 1600 +typedef signed __int8 int8_t; +typedef unsigned __int8 uint8_t; +typedef signed __int16 int16_t; +typedef unsigned __int16 uint16_t; +typedef signed __int32 int32_t; +typedef unsigned __int32 uint32_t; +typedef signed __int64 int64_t; +typedef unsigned __int64 uint64_t; +#endif // _MSC_VER < 1600 +#ifndef _WIN64 +typedef int32_t ssize_t; +#else // _WIN64 +typedef int64_t ssize_t; +#endif // _WIN64 +#endif /*_WIN32*/ + +#ifdef _WIN32 +# define SIZE_T_FMT "%Iu" +# define PTR_FMT "0x%p" +# define snprintf sprintf_s +#else /*!_WIN32*/ +# define SIZE_T_FMT "%zu" +# define PTR_FMT "%p" +#endif /*!_WIN32*/ + +typedef uint32_t cl_mem_fence_flags; + +//! \cond ignore +#define _BAD_INT32 0xBAADBAAD +#define _BAD_INT64 0XBAADBAADBAADBAADLL +#define _BAD_INTPTR LP64_SWITCH(_BAD_INT32,_BAD_INT64) + +const pointer badPointer = (pointer)(intptr_t) _BAD_INTPTR; +const address badAddress = (address)(intptr_t) _BAD_INTPTR; +//! \endcond + +const size_t Ki = 1024; +const size_t Mi = Ki*Ki; +const size_t Gi = Ki*Ki*Ki; + +const size_t K = 1000; +const size_t M = K*K; +const size_t G = K*K*K; + +#include "utils/debug.hpp" + +//! \addtogroup Utils + +//! Namespace for AMD's OpenCL platform +namespace amd {/*@{*/ + +//! \brief The default Null object type (!= void*); +struct Null {}; + +//! \brief Return a const Null object (null) +inline const Null null() { return Null(); } + +/*! \brief A struct to hold 2 objects of arbitrary type. + */ +template +struct pair +{ + F first; /*!< \brief first element. */ + S second; /*!< \brief second element. */ + + pair() : first(), second() { } + pair(const F& f, const S& s) : first(f), second(s) { } + + template + pair(const pair& p) : first(p.first), second(p.second) { } +}; + +template +inline pair +make_pair(F first, S second) +{ + return pair(first, second); +} + +/*! \brief Equivalent to a namespace (All member functions are static). + */ +class AllStatic +{ +WINDOWS_SWITCH(public,private): + AllStatic() { ShouldNotCallThis(); } + AllStatic(const AllStatic&) { ShouldNotCallThis(); } + ~AllStatic() { ShouldNotCallThis(); } +}; + +/*! \brief For embedded objects. + */ +class EmbeddedObject +{ +WINDOWS_SWITCH(public,private): + void* operator new(size_t) { ShouldNotCallThis(); return badPointer; } + void operator delete(void *) { ShouldNotCallThis(); } +}; + +/*! \brief For stack allocated objects. + */ +class StackObject +{ +WINDOWS_SWITCH(public,private): + void* operator new(size_t) { ShouldNotCallThis(); return badPointer; } + void operator delete(void *) { ShouldNotCallThis(); } +}; + +/*! \brief for objects allocated in a dedicate memory pool. + the standard 'new' should not be called, + only the in place version 'new (allocation_pointer) ()' + , delete should only invoke the destructors and not release memory + */ +class MemoryPoolObject +{ +public: + void* operator new(size_t) { ShouldNotCallThis(); return badPointer; } + void* operator new(size_t size,void * address) { return address; } + void operator delete(void *) { } + void operator delete( void *,void * address) { } +}; + +/*! \brief For objects allocated on the C-heap. + */ +class HeapObject +{ +public: + void* operator new(size_t size); + void operator delete(void* obj); +}; + +/*! \brief For all reference counted objects. + */ +class ReferenceCountedObject +{ + volatile uint referenceCount_; + +protected: + virtual ~ReferenceCountedObject() { } + virtual bool terminate() { return true; } + +public: + ReferenceCountedObject() : referenceCount_(1) { } + + void* operator new(size_t size) { return ::operator new(size); } + void operator delete(void* p) { return ::operator delete(p); } + + uint referenceCount() const { return referenceCount_; } + + uint retain(); + uint release(); +}; + +/*@}*/} // namespace amd + +#ifdef FOR_DOXYGEN_ONLY +namespace std +{ +template struct pair { F first; S second; }; +template struct vector { public: T data; }; +template class deque { public: T data; }; +template class list { public: T data; }; +template class slist { public: T data; }; +template class set { public: T data; }; +template class map { public: Key key; Data data; }; +} +#endif // FOR_DOXYGEN_ONLY + +#undef min // using std::min +#undef max // using std::max + +#endif /*TOP_HPP_*/ + diff --git a/rocclr/compiler/lib/promotions/oclutils/utils/debug.cpp b/rocclr/compiler/lib/promotions/oclutils/utils/debug.cpp new file mode 100644 index 0000000000..ef532eb8e2 --- /dev/null +++ b/rocclr/compiler/lib/promotions/oclutils/utils/debug.cpp @@ -0,0 +1,102 @@ +// +// Copyright (c) 2008 Advanced Micro Devices, Inc. All rights reserved. +// + +#include "top.hpp" +#include "utils/debug.hpp" +#include "os/os.hpp" + +#if !defined(LOG_LEVEL) +# include "utils/flags.hpp" +#endif + +#include +#include +#include + +#ifdef _WIN32 +#include +#endif // _WIN32 + +namespace amd { + +//! \cond ignore +extern "C" void +breakpoint(void) +{ +#ifdef _MSC_VER + DebugBreak(); +#endif // _MSC_VER +} +//! \endcond + +bool +ShouldBreak(LogLevel level) +{ + if ((level == LOG_WARNING && BREAK_ON_LOG_WARNING) + || (level == LOG_ERROR && BREAK_ON_LOG_ERROR)) { + return true; + } + return false; +} + +void +report_fatal(const char* file, int line, const char* message) +{ + // FIXME_lmoriche: Obfuscate the message string + fprintf(stderr, "%s:%d: %s\n", file, line, message); + ::abort(); +} + +void +report_warning(const char* message) +{ + fprintf(stderr, "Warning: %s\n", message); +} + +void +log_entry(LogLevel level, const char* file, int line, const char* message) +{ + if (level == LOG_NONE) { + return; + } + fprintf(stderr, ":%d:%s:%d: %s\n", level, file, line, message); +} + +void +log_timestamped(LogLevel level, const char* file, int line, const char* message) +{ + static bool gotstart = false; // not thread-safe, but not scary if fails + static uint64_t start; + + if (!gotstart) { + start = Os::timeNanos(); + gotstart = true; + } + + uint64_t time = Os::timeNanos() - start; + if (level == LOG_NONE) { + return; + } +#if 0 + fprintf(stderr, ":%d:%s:%d: (%010lld) %s\n", level, file, line, time, message); +#else // if you prefer fixed-width fields + fprintf(stderr, ":% 2d:%15s:% 5d: (%010lld) %s\n", + level, file, line, time/100ULL, message); // timestamp is 100ns units +#endif +} + +void +log_printf(LogLevel level, const char* file, int line, const char* format, ...) +{ + va_list ap; + + va_start(ap, format); + char message[1024]; + vsprintf(message, format, ap); + va_end(ap); + + fprintf(stderr, ":%d:%s:%d: %s\n", level, file, line, message); +} + +} // namespace amd diff --git a/rocclr/compiler/lib/promotions/oclutils/utils/debug.hpp b/rocclr/compiler/lib/promotions/oclutils/utils/debug.hpp new file mode 100644 index 0000000000..d0302afeaa --- /dev/null +++ b/rocclr/compiler/lib/promotions/oclutils/utils/debug.hpp @@ -0,0 +1,193 @@ +// +// Copyright (c) 2008 Advanced Micro Devices, Inc. All rights reserved. +// + +#ifndef DEBUG_HPP_ +#define DEBUG_HPP_ + + +#include + +//! \addtogroup Utils + +namespace amd {/*@{*/ + +enum LogLevel { + LOG_NONE = 0, + LOG_ERROR = 1, + LOG_WARNING = 2, + LOG_INFO = 3, + LOG_DEBUG = 4 +}; + +//! \cond ignore +extern "C" void +breakpoint(); +//! \endcond + +//! \brief Report a Fatal exception message and abort. +extern void +report_fatal(const char* file, int line, const char* message); + +//! \brief Display a warning message. +extern void +report_warning(const char* message); + +//! \brief Insert a log entry. +extern void +log_entry(LogLevel level, const char* file, int line, const char* messsage); + +//! \brief Insert a timestamped log entry. +extern void +log_timestamped(LogLevel level, const char* file, int line, const char* messsage); + +//! \brief Insert a printf-style log entry. +extern void +log_printf( + LogLevel level, + const char* file, + int line, + const char* format, + ...); + +/*@}*/} // namespace amd + +#if __INTEL_COMPILER + +// Disable ICC's warning #279: controlling expression is constant +// (0!=1 && "msg") +// ^ +#pragma warning ( disable : 279 ) + +#endif // __INTEL_COMPILER + +//! \brief Abort the program if the invariant \a cond is false. +#define guarantee(cond) \ + if (!(cond)) \ + { \ + amd::report_fatal(__FILE__, __LINE__, \ + "guarantee(" XSTR(cond) ")"); \ + amd::breakpoint(); \ + } + +#define fixme_guarantee(cond) guarantee(cond) + +//! \brief Abort the program with a fatal error message. +#define fatal(msg) do { assert(false && msg); } while (0) + + +//! \brief Display a warning message. +inline void +warning(const char* msg) +{ + amd::report_warning(msg); +} + +/*! \brief Abort the program with a "ShouldNotReachHere" message. + * \hideinitializer + */ +#define ShouldNotReachHere() fatal("ShouldNotReachHere()") + +/*! \brief Abort the program with a "ShouldNotCallThis" message. + * \hideinitializer + */ +#define ShouldNotCallThis() fatal("ShouldNotCallThis()") + +/*! \brief Abort the program with an "Unimplemented" message. + * \hideinitializer + */ +#define Unimplemented() fatal("Unimplemented()") + +/*! \brief Display an "Untested" warning message. + * \hideinitializer + */ +#ifndef NDEBUG +# define Untested(msg) \ + warning("Untested(\"" msg "\")") +#else /*NDEBUG*/ +# define Untested(msg) (void)(0) +#endif /*NDEBUG*/ + +#ifdef DEBUG +namespace amd { +extern bool ShouldBreak(LogLevel level); +} // namespace amd +#endif // DEBUG + +#ifdef DEBUG +# define Log(level,msg) \ +do \ +{ \ + if (LOG_LEVEL >= level) { \ + amd::log_entry(level, __FILE__, __LINE__, msg); \ + if (amd::ShouldBreak(level)) { \ + amd::breakpoint(); \ + } \ + } \ +} while (false) +#else // !DEBUG +# define Log(level,msg) (void)(0) +#endif // !DEBUG + +#ifdef DEBUG +# define LogTS(level,msg) \ +do \ +{ \ + if (LOG_LEVEL >= level) { \ + amd::log_timestamped(level, __FILE__, __LINE__, msg); \ + if (amd::ShouldBreak(level)) { \ + amd::breakpoint(); \ + } \ + } \ +} while (false) +#else // !DEBUG +# define Log(level,msg) (void)(0) +#endif // !DEBUG + +#ifdef DEBUG +# define Logf(level, format, ...) \ +do \ +{ \ + if (LOG_LEVEL >= level) { \ + amd::log_printf(level, __FILE__, __LINE__, format, __VA_ARGS__); \ + if (amd::ShouldBreak(level)) { \ + amd::breakpoint(); \ + } \ + } \ +} while (false) +#else // !DEBUG +# define Logf(level, format, ...) (void)(0) +#endif // !DEBUG + +#define CondLog(cond,msg) \ +do { \ + if (false DEBUG_ONLY(|| (cond))) { \ + Log(amd::LOG_INFO,msg); \ + } \ +} while (false) + +#define LogInfo(msg) Log(amd::LOG_INFO,msg) +#define LogError(msg) Log(amd::LOG_ERROR,msg) +#define LogWarning(msg) Log(amd::LOG_WARNING,msg) + +#define LogTSInfo(msg) LogTS(amd::LOG_INFO,msg) +#define LogTSError(msg) LogTS(amd::LOG_ERROR,msg) +#define LogTSWarning(msg) LogTS(amd::LOG_WARNING,msg) + +#define LogPrintfDebug(format, ...) Logf(amd::LOG_DEBUG, format, __VA_ARGS__) +#define LogPrintfError(format, ...) Logf(amd::LOG_ERROR, format, __VA_ARGS__) +#define LogPrintfWarning(format, ...) Logf(amd::LOG_WARNING, format, __VA_ARGS__) +#define LogPrintfInfo(format, ...) Logf(amd::LOG_INFO, format, __VA_ARGS__) + +//! Used by objects that have split constructors for sanity-checking +//! their construction state + +enum ConstructionState { + CS_CONSTRUCTED, //!< alloc (regular) constructor called ok + CS_ACTIVE, //!< create constructor called ok + CS_DYING, //!< in destructor (use to trap races) + CS_BROKEN, //!< something went wrong during construction +}; + +#endif /*DEBUG_HPP_*/ + diff --git a/rocclr/compiler/lib/promotions/oclutils/utils/macros.hpp b/rocclr/compiler/lib/promotions/oclutils/utils/macros.hpp new file mode 100644 index 0000000000..e7765f8384 --- /dev/null +++ b/rocclr/compiler/lib/promotions/oclutils/utils/macros.hpp @@ -0,0 +1,171 @@ +// +// Copyright (c) 2011 Advanced Micro Devices, Inc. All rights reserved. +// + +#ifndef MACROS_HPP_ +#define MACROS_HPP_ + +#ifndef OPENCL_EXPORTS +# define OPENCL_EXPORTS 1 +#endif // OPENCL_EXPORTS + +#if defined(NDEBUG) +# define RELEASE 1 +#else// !NDEBUG +# define ASSERT 1 +# define DEBUG 1 +#endif // !NDEBUG + +#if defined(_WIN64) && !defined(_LP64) +# define _LP64 1 +#endif + +#if defined(_DEBUG) && !defined(DEBUG) +# define DEBUG 1 +#endif // _DEBUG && !DEBUG + +#if defined(DEBUG) && defined(RELEASE) +# error "Build Error: cannot have both -DDEBUG and -DRELEASE" +#endif /*DEBUG && RELEASE*/ + +#if !defined(DEBUG) && !defined(RELEASE) +# error "Build Error: must have either -DDEBUG or -DRELEASE" +#endif /*DEBUG && RELEASE*/ + +#ifdef DEBUG +# define DEBUG_ONLY(x) x +# define RELEASE_ONLY(x) +# define IS_DEBUG true +#else // !DEBUG +# define DEBUG_ONLY(x) +# define RELEASE_ONLY(x) x +# define IS_DEBUG false +#endif /*!DEBUG*/ +#define DEBUG_SWITCH(d,r) DEBUG_ONLY(d)RELEASE_ONLY(r) +#define RELEASE_SWITCH(r,d) RELEASE_ONLY(r)DEBUG_ONLY(d) + +//! \brief Make a c-string of __macro__ +#define STR(__macro__) #__macro__ +//! \brief Make a c-string of the expansion of __macro__ +#define XSTR(__macro__) STR(__macro__) +//! \brief Concatenate 2 symbols +#define CONCAT(a,b) a##b +#define XCONCAT(a,b) CONCAT(a,b) + + +//! \cond ignore +#ifdef _LP64 +# define LP64_ONLY(x) x +# define NOT_LP64(x) +#else // !_LP64 +# define LP64_ONLY(x) +# define NOT_LP64(x) x +#endif /*!_LP64*/ +#define LP64_SWITCH(lp32,lp64) NOT_LP64(lp32)LP64_ONLY(lp64) + +#ifdef __linux__ +# define IS_LINUX true +# define LINUX_ONLY(x) x +# define NOT_LINUX(x) +#else // !__linux__ +# define LINUX_ONLY(x) +# define NOT_LINUX(x) x +#endif /*!__linux__*/ + +#ifdef __APPLE__ +# define IS_MACOS true +# define MACOS_ONLY(x) x +# define NOT_MACOS(x) +#else // !__APPLE__ +# define MACOS_ONLY(x) +# define NOT_MACOS(x) x +#endif /*!__APPLE__*/ + +#ifdef _WIN32 +# define IS_WINDOWS true +# define WINDOWS_ONLY(x) x +# define NOT_WINDOWS(x) +#else // !_WIN32 +# define WINDOWS_ONLY(x) +# define NOT_WINDOWS(x) x +#endif /*!_WIN32*/ + +#ifdef _WIN64 +# define WIN64_ONLY(x) x +# define NOT_WIN64(x) +#else // !_WIN64 +# define WIN64_ONLY(x) +# define NOT_WIN64(x) x +#endif /*!_WIN64*/ + +#ifndef IS_LINUX +# define IS_LINUX false +#endif +#ifndef IS_MACOS +# define IS_MACOS false +#endif +#ifndef IS_WINDOWS +# define IS_WINDOWS false +#endif + +#define IF_LEFT_true(x) x +#define IF_LEFT_false(x) +#define IF_RIGHT_true(x) +#define IF_RIGHT_false(x) x + +#define IF_LEFT(cond,x) IF_LEFT_##cond(x) +#define IF_RIGHT(cond,x) IF_RIGHT_##cond(x) +#define IF(cond,x,y) IF_LEFT(cond,x)IF_RIGHT(cond,y) + +#define LINUX_SWITCH(x,other) LINUX_ONLY(x)NOT_LINUX(other) +#define MACOS_SWITCH(x,other) MACOS_ONLY(x)NOT_MACOS(other) +#define WINDOWS_SWITCH(x,other) WINDOWS_ONLY(x)NOT_WINDOWS(other) + +#ifdef OPENCL_MAINLINE +# define IS_MAINLINE true +#else // OPENCL_STAGING +# define IS_MAINLINE false +#endif + +#ifdef OPTIMIZED +# define OPTIMIZED_ONLY(x) x +# define NOT_OPTIMIZED(x) +# define IS_OPTIMIZED true +#else +# define OPTIMIZED_ONLY(x) +# define NOT_OPTIMIZED(x) x +# define IS_OPTIMIZED false +#endif + +#if defined(__GNUC__) +# define __ALIGNED__(x) __attribute__((aligned(x))) +#elif defined(_MSC_VER) +# define __ALIGNED__(x) __declspec(align(x)) +#elif defined(RC_INVOKED) +# define __ALIGNED__(x) +#else +# error +#endif /*_MSC_VER*/ + +#if defined(__GNUC__) +# define likely(cond) __builtin_expect(!!(cond), 1) +# define unlikely(cond) __builtin_expect(!!(cond), 0) +#else // !__GNUC__ +# define likely(cond) (cond) +# define unlikely(cond) (cond) +#endif // !__GNUC__ + +#if defined(__GNUC__) +# define NOINLINE __attribute__((noinline)) +# define ALWAYSINLINE __attribute__ ((always_inline)) +#elif defined(_MSC_VER) +# define NOINLINE __declspec(noinline) +# define ALWAYSINLINE __forceinline +#else // !_MSC_VER +# define NOINLINE +# define ALWAYSINLINE +#endif // !_MSC_VER + +//! \endcond + +#endif // MACROS_HPP_ diff --git a/rocclr/compiler/lib/promotions/oclutils/utils/traits.hpp b/rocclr/compiler/lib/promotions/oclutils/utils/traits.hpp new file mode 100644 index 0000000000..c63f936b8e --- /dev/null +++ b/rocclr/compiler/lib/promotions/oclutils/utils/traits.hpp @@ -0,0 +1,108 @@ +// +// Copyright (c) 2008 Advanced Micro Devices, Inc. All rights reserved. +// + +#ifndef TRAITS_HPP_ +#define TRAITS_HPP_ + +namespace amd { + +// Type traits: + +//! \cond ignore +template +struct is_pointer +{ static const bool value = false; }; + +template +struct is_pointer +{ static const bool value = true; }; + +template +struct remove_reference +{ typedef T type; }; + +template +struct remove_reference +{ typedef T type; }; + +template +struct remove_volatile +{ typedef T type; }; + +template +struct remove_volatile +{ typedef T type; }; + +template +struct remove_const +{ typedef T type; }; + +template +struct remove_const +{ typedef T type; }; + +template +struct remove_pointer +{ typedef T type; }; + +template +struct remove_pointer +{ typedef T type; }; + +template +struct add_const +{ typedef T const type; }; + +template +struct add_const +{ typedef T& type; }; + +template +struct add_volatile +{ typedef T volatile type; }; + +template +struct add_volatile +{ typedef T& type; }; + +template +struct add_pointer +{ typedef typename remove_reference::type* type; }; + +template +struct add_reference +{ typedef typename remove_reference::type& type; }; + +template <> +struct add_reference +{ typedef void type; }; + +template <> +struct add_reference +{ typedef const void type; }; + +template <> +struct add_reference +{ typedef volatile void type; }; + +template <> +struct add_reference +{ typedef const volatile void type; }; + +template +struct make_arithmetic +{ typedef typename remove_volatile::type type; }; + +template +struct make_arithmetic +{ typedef long int type; }; + +template +struct make_arithmetic +{ typedef typename make_arithmetic::type type; }; +//! \endcond + +} // namespace amd + +#endif /* TRAITS_HPP_ */ diff --git a/rocclr/compiler/lib/promotions/oclutils/utils/util.hpp b/rocclr/compiler/lib/promotions/oclutils/utils/util.hpp new file mode 100644 index 0000000000..2ff53a302c --- /dev/null +++ b/rocclr/compiler/lib/promotions/oclutils/utils/util.hpp @@ -0,0 +1,647 @@ +// +// Copyright (c) 2008 Advanced Micro Devices, Inc. All rights reserved. +// + +#ifndef UTIL_HPP_ +#define UTIL_HPP_ + +#include "top.hpp" +#include "thread/atomic.hpp" + +#include + +namespace amd { + +/*! \addtogroup Utils Utilities + * @{ + */ + +//! \cond ignore +template +struct PairElement; + +template <> +struct PairElement<0> +{ + template + static inline F& get(pair& p) { return p.first; } + template + static inline const F& get(const pair& p) { return p.first; } +}; + +template <> +struct PairElement<1> +{ + template + static inline S& get(pair& p) { return p.second; } + template + static inline const S& get(const pair& p) { return p.second; } +}; + +// Forward declaration of the tuple_elements container class. +template +struct TupleElementsContainer; + +/*! \brief Return the type of the Nth element in the tuple. + */ +template +struct TupleElementType +{ + typedef typename T::tail_t next_element; + typedef typename TupleElementType::type type; +}; + +// break the recursion +template +struct TupleElementType<0,T> +{ + typedef Null next_element; + typedef typename T::head_t type; +}; + +/*! \brief Helper struct to extract the Nth element from a tuple + */ +template +struct TupleElementGetter +{ + template + static R get(TupleElementsContainer& t) + { + return TupleElementGetter::template get(t.tail); + } + template + static R get(const TupleElementsContainer& t) + { + return TupleElementGetter::template get(t.tail); + } +}; + +// break the recursion +template <> +struct TupleElementGetter<0> +{ + template + static R get(TupleElementsContainer& t) + { + return t.head; + } + template + static R get(const TupleElementsContainer& t) + { + return t.head; + } +}; + +/*! \brief Return the Nth element in the tuple. + */ +template +inline typename TupleElementType >::type& +getTupleElement(TupleElementsContainer& t) +{ + return TupleElementGetter::template get< + typename TupleElementType >::type&, + H,T>(t); +} + +template +inline const typename TupleElementType >::type& +getTupleElement(const TupleElementsContainer& t) +{ + return TupleElementGetter::template get< + const typename TupleElementType >::type&, + H,T>(t); +} + +/*! \brief The tuple elements struct + */ +template +struct TupleElementsContainer +{ + typedef H head_t; + typedef T tail_t; + + head_t head; tail_t tail; + + TupleElementsContainer() : head(), tail() { } + + template + TupleElementsContainer(T0& t0, T1& t1, T2& t2, T3& t3) + : head(t0), tail(t1, t2, t3, null()) + { } + + template + TupleElementsContainer& operator= (const TupleElementsContainer& t) + { + head = t.head; + tail = t.tail; + return *this; + } + + template + TupleElementsContainer& operator= (const pair& p) + { + head = p.first; + tail.head = p.second; + return *this; + } + + template + typename TupleElementType::type& + get() { return getTupleElement(*this); } +}; + +// break the recursion +template +struct TupleElementsContainer +{ + typedef H head_t; + typedef Null tail_t; + + H head; + + TupleElementsContainer() : head() { } + + template + TupleElementsContainer(T0& t0, const Null&, const Null&, const Null&) + : head(t0) + { } + + template + TupleElementsContainer& operator = ( + const TupleElementsContainer& t) + { + head = t.head; + return *this; + } + + template + typename TupleElementType::type& + get() { return getTupleElement(*this); } +}; + +/*! \brief Rebind the TupleElementsContainer type. + */ +template +struct TupleElementsBinder +{ + typedef TupleElementsContainer< + T0, typename TupleElementsBinder::type + > type; +}; + +// break the recursion +template<> +struct TupleElementsBinder +{ typedef Null type; }; +//! \endcond + +/*! \brief A simple N-element (1 to 4) tuple. + */ +template +class tuple : public TupleElementsBinder::type +{ +private: + typedef typename TupleElementsBinder::type base_t; + +public: + tuple() { } + tuple(T0 t0) : base_t(t0, null(), null(), null()) { } + tuple(T0 t0, T1 t1) : base_t(t0, t1, null(), null()) { } + tuple(T0 t0, T1 t1, T2 t2) : base_t(t0, t1, t2, null()) { } + tuple(T0 t0, T1 t1, T2 t2, T3 t3) : base_t(t0, t1, t2, t3) { } + + template + tuple(const TupleElementsContainer& te) : base_t(te) + { } + + template + tuple& operator = (const TupleElementsContainer& te) + { + base_t::operator = (te); + return *this; + } + + template + tuple& operator = (const pair& p) + { + base_t::operator = (p); + return *this; + } +}; + +// tuple / pair element getters. + +template +inline typename TupleElementType >::type& +get(TupleElementsContainer& te) +{ + return getTupleElement(te); +} + +template +inline const typename TupleElementType >::type& +get(const TupleElementsContainer& te) +{ + return getTupleElement(te); +} + +template +inline typename TupleElementType >::type& +get(pair& p) +{ + return PairElement::get(p); +} + +template +inline const typename TupleElementType >::type& +get(const pair& p) +{ + return PairElement::get(p); +} + +// Some tuple helpers (make_tuple() and tie()) + +template +inline tuple +make_tuple(const T0& t0) +{ + return tuple(t0); +} + +template +inline tuple +make_tuple(const T0& t0, const T1& t1) +{ + return tuple(t0, t1); +} + +template +inline tuple +make_tuple(const T0& t0, const T1& t1, const T2& t2) +{ + return tuple(t0, t1, t2); +} + +template +inline tuple +make_tuple(const T0& t0, const T1& t1, const T2& t2, const T3& t3) +{ + return tuple(t0, t1, t2, t3); +} + +template +inline tuple +tie(T0& t0) +{ + return tuple(t0); +} + +template +inline tuple +tie(T0& t0, T1& t1) +{ + return tuple(t0, t1); +} + +template +inline tuple +tie(T0& t0, T1& t1, T2& t2) +{ + return tuple(t0, t1, t2); +} + +template +inline tuple +tie(T0& t0, T1& t1, T2& t2, T3& t3) +{ + return tuple(t0, t1, t2, t3); +} + +//! \brief Check if the given value \a val is a power of 2. +template +static inline bool +isPowerOfTwo(T val) +{ + return (val & (val - 1)) == 0; +} + +//! \cond ignore + +// Compute the next power of 2 helper. +template +struct NextPowerOfTwoFunction +{ + template + static T compute(T val) + { + val = NextPowerOfTwoFunction::compute(val); + return (val >> N) | val; + } +}; + +// Specialized version for <1> to break the recursion. +template <> +struct NextPowerOfTwoFunction<1> +{ + template + static T compute(T val) { return (val >> 1) | val; } +}; + +template +struct NextPowerOfTwoHelper +{ + static const uint prev = NextPowerOfTwoHelper::value; + static const uint value = (prev >> S) | prev; +}; +template +struct NextPowerOfTwoHelper +{ + static const int value = (N >> 1) | N; +}; + +template +struct NextPowerOfTwo +{ + static const uint value = NextPowerOfTwoHelper::value + 1; +}; + +//! \endcond + +/*! \brief Return the next power of two for a value of type T. + * + * The compute function is (with n = sizeof(T)*8): + * + * val = (val >> 1) | val; + * val = (val >> 2) | val; + * ... + * val = (val >> n/4) | val; + * val = (val >> n/2) | val; + * + * The next power of two is: 1+compute(val-1) + */ +template +inline T +nextPowerOfTwo(T val) +{ + return NextPowerOfTwoFunction::compute(val - 1) + 1; +} + +// Compute log2(N) +template +struct Log2 +{ + static const uint value = Log2::value + 1; +}; + +// Break the recursion +template <> +struct Log2<1> +{ + static const uint value = 0; +}; + +/*! \brief Return the log2 for a value of type T. + * + * The compute function is (with n = sizeof(T)*8): + * + * uint l = 0; + * if (val >= 1 << n/2) { val >>= n/2; l |= n/2; } + * if (val >= 1 << n/4) { val >>= n/4; l |= n/4; } + * ... + * if (val >= 1 << 2) { val >>= 2; l |= 2; } + * if (val >= 1 << 1) { l |= 1; } + * return l; + */ +template +struct Log2Function +{ + template + static uint compute(T val) + { + uint l = 0; + if (val >= T(1) << N) { + val >>= N; l = N; + } + return l + Log2Function::compute(val); + } +}; + +template <> +struct Log2Function<1> +{ + template + static uint compute(T val) { + return (val >= T(1)<<1) ? 1 : 0; + } +}; + +// log2 helper function +template +inline uint +log2(T val) +{ + return Log2Function::compute(val); +} + +template +inline T +alignDown(T value, size_t alignment) +{ + return (T) (value & ~(alignment - 1)); +} + +template +inline T* +alignDown(T* value, size_t alignment) +{ + return (T*) alignDown((intptr_t) value, alignment); +} + +template +inline T +alignUp(T value, size_t alignment) +{ + return alignDown((T) (value + alignment - 1), alignment); +} + +template +inline T* +alignUp(T* value, size_t alignment) +{ + return (T*) alignDown((intptr_t) (value + alignment - 1), alignment); +} + +template +struct SimplyLinkedNode : public AllocClass +{ + typedef SimplyLinkedNode Node; + +protected: + Atomic next_; /*!< \brief The next element. */ + T volatile item_; + +public: + //! \brief Return the next element in the linked-list. + Node* next() const { return next_; } + //! \brief Return the item. + T item() const { return item_; } + + //! \brief Set the next element pointer. + void setNext(Node* next) { next_ = next; } + //! \brief Set the item. + void setItem(T item) { item_ = item; } + + //! \brief Swap the next element pointer. + Node* swapNext(Node* next) { return next_.swap(next); } + + //! \brief Compare and set the next element pointer. + bool compareAndSetNext(Node* compare, Node* next) + { + return next_.compareAndSet(compare, next); + } +}; + +/* For the implementation of a doubly-linked list, check: + * Lock-Free and Practical + * Deques and Doubly Linked + * Lists using Single-Word + * Compare-And-Swap + * + * Hakan Sundell, Philippas Tsigas + * Department of Computing Science + * Chalmers Univ. of Technol. and Goteborg Univ. + */ + +template +struct DoublyLinkedNode +{ + typedef SimplyLinkedNode Node; + +protected: + Atomic prev_; //!< The previous element. + Atomic next_; //!< The next element. + T volatile item_; + +public: + //! \brief Return the previous element in the linked-list. + Node* prev() const { return prev_; } + //! \brief Return the next element in the linked-list. + Node* next() const { return next_; } + //! \brief Return the item. + T item() const { return item_; } + + //! \brief Set the previous element pointer. + void setPrev(Node* prev) { prev_ = prev; } + //! \brief Set the next element pointer. + void setNext(Node* next) { next_ = next; } + //! \brief Set the item. + void setItem(T item) { item_ = item; } + + //! \brief Swap the previous element pointer. + Node* swapPrev(Node* prev) + { + return prev_.swap(prev); + } + //! \brief Swap the next element pointer. + Node* swapNext( Node* next) + { + return next_.swap(next); + } + + //! \brief Compare and set the previous element pointer. + bool compareAndSetPrev(Node* compare, Node* prev) + { + return prev_.compareAndSet(compare, prev, false, false); + } + //! \brief Compare and set the next element pointer. + bool compareAndSetNext(Node* compare, Node* next) + { + return next_.compareAndSet(compare, next, false, false); + } +}; + +template +struct DeviceMap { + Reference ref_; + Value value_; +}; + + +inline uint +countBitsSet32(uint32_t value) +{ +#if __GNUC__ >= 4 + return (uint)__builtin_popcount(value); +#else + value = value - ((value >> 1) & 0x55555555); + value = (value & 0x33333333) + ((value >> 2) & 0x33333333); + return (uint)(((value + (value >> 4) & 0xF0F0F0F) * 0x1010101) >> 24); +#endif +} + +inline uint +countBitsSet64(uint64_t value) +{ +#if __GNUC__ >= 4 + return (uint)__builtin_popcountll(value); +#else + value = value - ((value >> 1) & 0x5555555555555555ULL); + value = (value & 0x3333333333333333ULL) + ((value >> 2) & 0x3333333333333333ULL); + value = (value + (value >> 4)) & 0x0F0F0F0F0F0F0F0FULL; + return (uint)((uint64_t)(value * 0x0101010101010101ULL) >> 56); +#endif +} + +inline uint +leastBitSet32(uint32_t value) +{ +#if defined(_WIN32) + unsigned long idx; + return _BitScanForward(&idx, (unsigned long)value) ? idx : (uint)-1; +#else + return value ? __builtin_ctz(value) : (uint)-1; +#endif +} + +inline uint +leastBitSet64(uint64_t value) +{ +#if defined(_WIN64) + unsigned long idx; + return _BitScanForward64(&idx, (unsigned __int64)value) ? idx : (uint)-1; +#elif defined (__GNUC__) + return value ? __builtin_ctzll(value) : (uint)-1; +#else + static const uint8_t lookup67[67+1] = { + 64, 0, 1, 39, 2, 15, 40, 23, + 3, 12, 16, 59, 41, 19, 24, 54, + 4, -1, 13, 10, 17, 62, 60, 28, + 42, 30, 20, 51, 25, 44, 55, 47, + 5, 32, -1, 38, 14, 22, 11, 58, + 18, 53, 63, 9, 61, 27, 29, 50, + 43, 46, 31, 37, 21, 57, 52, 8, + 26, 49, 45, 36, 56, 7, 48, 35, + 6, 34, 33, -1 + }; + + return (uint)lookup67[((int64_t)value & -(int64_t)value) % 67]; +#endif +} + +template +inline uint countBitsSet(T value) +{ + return (sizeof(T) == 8) ? countBitsSet64((uint64_t)value) : + countBitsSet32((uint32_t)value); +} + +template +inline uint leastBitSet(T value) +{ + return (sizeof(T) == 8) ? leastBitSet64((uint64_t)value) : + leastBitSet32((uint32_t)value); +} + +/*@}*/} // namespace amd + +#endif /*UTIL_HPP_*/ diff --git a/rocclr/compiler/lib/utils/OPTIONS.def b/rocclr/compiler/lib/utils/OPTIONS.def new file mode 100644 index 0000000000..d9b1b29025 --- /dev/null +++ b/rocclr/compiler/lib/utils/OPTIONS.def @@ -0,0 +1,1091 @@ +// Copyright (c) 2010 Advanced Micro Devices, Inc. All rights reserved. +// + +/* + * Description: + * + * This is the file that contains definitions of all options to clBuildProgram(). + * And any changes to option (add/remove/modify) should be done here. This option + * processing is thread-safe, that is, each option is not implemented as a static + * variable that is assessible to all threads but as a local variable that is + * accessible only to its definining thread. For example, + * + * option::Options localOptions; + * option::parseAllOptions(cppstr, localOptions); + * + * where 'localOptions' is an option object that contains all option variables and any + * code that needs to access the option has to do so via 'localOptions'. (That is why + * 'localOptions' has been passed down to many parts that need to access option + * variables.) For instance, the following will be used to check if -g is present: + * + * if (localOptions.oVariables->EnableDebug) { + * <-g is present>; + * } + * + * + * MACROS for making changes to this file: + * + * Two macros : OPTION for runtime options that have option variables and NOPTION for others + * that do Not have option variables. The OPTION are ones that are referenced by the runtime + * via their option variables like the one shown above. The NOPTION are ones that are either + * passed into component option processors directly, or alias runtime options. An alias runtime + * option is one that refers to another option or a group of others and has no corresponding + * option variable in the above option object. For example, + * -D NAME= is passed into the front end and the runtime has no variable for it in the + * above option object. Another example is -cl-opt-disable, which is a runtime alias option. + * It is equivalent to -O0 and used to set -O0. It has no variable in the option object either. + * + * Here are these two macros: + * OPTION(type, attr, sn, ln, var, ideft, imix, imax, sdeft, desc) + * NOPTION(type, attr, sn, ln, var, ideft, imix, imax, sdeft, desc) + * + * For convenience, FLAG marco is provided as well. FLAG macro is very close to a flag + * used in flags.hpp. It is define as below: + * FLAG(type, vis, sn, var, deft, desc) + * + * type: option type defined as OptionType enum: + * OT_BOOL : bool + * OT_INT32 : int32 + * OT_UINT32 : uint32 + * OT_CSTRING : char* + * + * attr: option attributes, divided in several groups: + * OptionValue : value attribute. Use exactly one. + * OVA_OPTIONAL : value is optional + * OVA_REQUIRED : value is required to appear + * OVA_DISALLOWED : value may not be specified + * + * OptionForm : form attribute. Use exactly one. + * OFA_NORMAL : normal form, no prefix + * OFA_PREFIX_F : -f, machine-independent (-f[no-]