Home | History | Annotate | Download | only in backends
      1 /* Common code for ebl reloc functions.
      2    Copyright (C) 2005, 2006 Red Hat, Inc.
      3    This file is part of Red Hat elfutils.
      4 
      5    Red Hat elfutils is free software; you can redistribute it and/or modify
      6    it under the terms of the GNU General Public License as published by the
      7    Free Software Foundation; version 2 of the License.
      8 
      9    Red Hat elfutils is distributed in the hope that it will be useful, but
     10    WITHOUT ANY WARRANTY; without even the implied warranty of
     11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     12    General Public License for more details.
     13 
     14    You should have received a copy of the GNU General Public License along
     15    with Red Hat elfutils; if not, write to the Free Software Foundation,
     16    Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301 USA.
     17 
     18    Red Hat elfutils is an included package of the Open Invention Network.
     19    An included package of the Open Invention Network is a package for which
     20    Open Invention Network licensees cross-license their patents.  No patent
     21    license is granted, either expressly or impliedly, by designation as an
     22    included package.  Should you wish to participate in the Open Invention
     23    Network licensing program, please visit www.openinventionnetwork.com
     24    <http://www.openinventionnetwork.com>.  */
     25 
     26 #include "libebl_CPU.h"
     27 #include <assert.h>
     28 
     29 #define R_TYPE(name)		PASTE (RELOC_PREFIX, name)
     30 #define PASTE(a, b)		PASTE_1 (a, b)
     31 #define PASTE_1(a, b)		a##b
     32 #define R_NAME(name)		R_NAME_1 (RELOC_PREFIX, name)
     33 #define R_NAME_1(prefix, type)	R_NAME_2 (prefix, type)
     34 #define R_NAME_2(prefix, type)	#prefix #type
     35 
     36 #define RELOC_TYPES		STRINGIFIED_PASTE (BACKEND, reloc.def)
     37 #define STRINGIFIED_PASTE(a, b)	STRINGIFY (PASTE (a, b))
     38 #define STRINGIFY(x)		STRINGIFY_1 (x)
     39 #define STRINGIFY_1(x)		#x
     40 
     41 /* Provide a table of reloc type names, in a PIC-friendly fashion.  */
     42 
     43 static const struct EBLHOOK(reloc_nametable)
     44 {
     45   char zero;
     46 #define	RELOC_TYPE(type, uses) \
     47   char name_##type[sizeof R_NAME (type)];
     48 #include RELOC_TYPES
     49 #undef RELOC_TYPE
     50 } EBLHOOK(reloc_nametable) =
     51   {
     52     '\0',
     53 #define	RELOC_TYPE(type, uses) R_NAME (type),
     54 #include RELOC_TYPES
     55 #undef RELOC_TYPE
     56   };
     57 #define reloc_namestr (&EBLHOOK(reloc_nametable).zero)
     58 
     59 static const uint_fast16_t EBLHOOK(reloc_nameidx)[] =
     60 {
     61 #define	RELOC_TYPE(type, uses) \
     62   [R_TYPE (type)] = offsetof (struct EBLHOOK(reloc_nametable), name_##type),
     63 #include RELOC_TYPES
     64 #undef RELOC_TYPE
     65 };
     66 #define nreloc \
     67   ((int) (sizeof EBLHOOK(reloc_nameidx) / sizeof EBLHOOK(reloc_nameidx)[0]))
     68 
     69 #define REL	(1 << (ET_REL - 1))
     70 #define EXEC	(1 << (ET_EXEC - 1))
     71 #define DYN	(1 << (ET_DYN - 1))
     72 static const uint8_t EBLHOOK(reloc_valid)[] =
     73 {
     74 #define	RELOC_TYPE(type, uses) [R_TYPE (type)] = uses,
     75 #include RELOC_TYPES
     76 #undef RELOC_TYPE
     77 };
     78 #undef REL
     79 #undef EXEC
     80 #undef DYN
     81 
     82 const char *
     83 EBLHOOK(reloc_type_name) (int reloc,
     84 			  char *buf __attribute__ ((unused)),
     85 			  size_t len __attribute__ ((unused)))
     86 {
     87   if (reloc >= 0 && reloc < nreloc && EBLHOOK(reloc_nameidx)[reloc] != 0)
     88     return &reloc_namestr[EBLHOOK(reloc_nameidx)[reloc]];
     89   return NULL;
     90 }
     91 
     92 bool
     93 EBLHOOK(reloc_type_check) (int reloc)
     94 {
     95   return reloc >= 0 && reloc < nreloc && EBLHOOK(reloc_nameidx)[reloc] != 0;
     96 }
     97 
     98 bool
     99 EBLHOOK(reloc_valid_use) (Elf *elf, int reloc)
    100 {
    101   uint8_t uses = EBLHOOK(reloc_valid)[reloc];
    102 
    103   GElf_Ehdr ehdr_mem;
    104   GElf_Ehdr *ehdr = gelf_getehdr (elf, &ehdr_mem);
    105   assert (ehdr != NULL);
    106   uint8_t type = ehdr->e_type;
    107 
    108   return type > ET_NONE && type < ET_CORE && (uses & (1 << (type - 1)));
    109 }
    110 
    111 
    112 bool
    113 EBLHOOK(copy_reloc_p) (int reloc)
    114 {
    115   return reloc == R_TYPE (COPY);
    116 }
    117 
    118 bool
    119 EBLHOOK(none_reloc_p) (int reloc)
    120 {
    121   return reloc == R_TYPE (NONE);
    122 }
    123 
    124 #ifndef NO_RELATIVE_RELOC
    125 bool
    126 EBLHOOK(relative_reloc_p) (int reloc)
    127 {
    128   return reloc == R_TYPE (RELATIVE);
    129 }
    130 #endif
    131 
    132 static void
    133 EBLHOOK(init_reloc) (Ebl *ebl)
    134 {
    135   ebl->reloc_type_name = EBLHOOK(reloc_type_name);
    136   ebl->reloc_type_check = EBLHOOK(reloc_type_check);
    137   ebl->reloc_valid_use = EBLHOOK(reloc_valid_use);
    138   ebl->copy_reloc_p = EBLHOOK(copy_reloc_p);
    139   ebl->none_reloc_p = EBLHOOK(none_reloc_p);
    140 #ifndef NO_RELATIVE_RELOC
    141   ebl->relative_reloc_p = EBLHOOK(relative_reloc_p);
    142 #endif
    143 }
    144