1 /* Generate ELF backend handle. 2 Copyright (C) 2000, 2001, 2002, 2003, 2004 Red Hat, Inc. 3 4 This program is Open Source software; you can redistribute it and/or 5 modify it under the terms of the Open Software License version 1.0 as 6 published by the Open Source Initiative. 7 8 You should have received a copy of the Open Software License along 9 with this program; if not, you may obtain a copy of the Open Software 10 License version 1.0 from http://www.opensource.org/licenses/osl.php or 11 by writing the Open Source Initiative c/o Lawrence Rosen, Esq., 12 3001 King Ranch Road, Ukiah, CA 95482. */ 13 14 #ifdef HAVE_CONFIG_H 15 # include <config.h> 16 #endif 17 18 #include <assert.h> 19 #include <dlfcn.h> 20 #include <error.h> 21 #include <gelf.h> 22 #include <stdlib.h> 23 #include <string.h> 24 25 #include <libeblP.h> 26 27 28 /* This table should contain the complete list of architectures as far 29 as the ELF specification is concerned. */ 30 /* XXX When things are stable replace the string pointers with char 31 arrays to avoid relocations. */ 32 static const struct 33 { 34 const char *dsoname; 35 const char *emulation; 36 const char *prefix; 37 int prefix_len; 38 int em; 39 } machines[] = 40 { 41 { "i386", "elf_i386", "i386", 4, EM_386 }, 42 { "ia64", "elf_ia64", "ia64", 4, EM_IA_64 }, 43 { "alpha", "elf_alpha", "alpha", 5, EM_ALPHA }, 44 { "x86_64", "elf_x86_64", "x86_64", 6, EM_X86_64 }, 45 { "sh", "elf_sh", "sh", 2, EM_SH }, 46 { "arm", "ebl_arm", "arm", 3, EM_ARM }, 47 { "sparc", "elf_sparcv9", "sparc", 5, EM_SPARCV9 }, 48 { "sparc", "elf_sparc", "sparc", 5, EM_SPARC }, 49 { "sparc", "elf_sparcv8plus", "sparc", 5, EM_SPARC32PLUS }, 50 51 { "m32", "elf_m32", "m32", 3, EM_M32 }, 52 { "m68k", "elf_m68k", "m68k", 4, EM_68K }, 53 { "m88k", "elf_m88k", "m88k", 4, EM_88K }, 54 { "i860", "elf_i860", "i860", 4, EM_860 }, 55 { "mips", "elf_mips", "mips", 4, EM_MIPS }, 56 { "s370", "ebl_s370", "s370", 4, EM_S370 }, 57 { "mips", "elf_mipsel", "mips", 4, EM_MIPS_RS3_LE }, 58 { "parisc", "elf_parisc", "parisc", 6, EM_PARISC }, 59 { "vpp500", "elf_vpp500", "vpp500", 5, EM_VPP500 }, 60 { "sparc", "elf_v8plus", "v8plus", 6, EM_SPARC32PLUS }, 61 { "i960", "elf_i960", "i960", 4, EM_960 }, 62 { "ppc", "elf_ppc", "ppc", 3, EM_PPC }, 63 { "ppc64", "elf_ppc64", "ppc64", 5, EM_PPC64 }, 64 { "s390", "ebl_s390", "s390", 4, EM_S390 }, 65 { "v800", "ebl_v800", "v800", 4, EM_V800 }, 66 { "fr20", "ebl_fr20", "fr20", 4, EM_FR20 }, 67 { "rh32", "ebl_rh32", "rh32", 4, EM_RH32 }, 68 { "rce", "ebl_rce", "rce", 3, EM_RCE }, 69 { "tricore", "elf_tricore", "tricore", 7, EM_TRICORE }, 70 { "arc", "elf_arc", "arc", 3, EM_ARC }, 71 { "h8", "elf_h8_300", "h8_300", 6, EM_H8_300 }, 72 { "h8", "elf_h8_300h", "h8_300h", 6, EM_H8_300H }, 73 { "h8", "elf_h8s", "h8s", 6, EM_H8S }, 74 { "h8", "elf_h8_500", "h8_500", 6, EM_H8_500 }, 75 { "mips_x", "elf_mips_x", "mips_x", 6, EM_MIPS_X }, 76 { "coldfire", "elf_coldfire", "coldfire", 8, EM_COLDFIRE }, 77 { "m68k", "elf_68hc12", "68hc12", 6, EM_68HC12 }, 78 { "mma", "elf_mma", "mma", 3, EM_MMA }, 79 { "pcp", "elf_pcp", "pcp", 3, EM_PCP }, 80 { "ncpu", "elf_ncpu", "ncpu", 4, EM_NCPU }, 81 { "ndr1", "elf_ndr1", "ndr1", 4, EM_NDR1 }, 82 { "starcore", "elf_starcore", "starcore", 8, EM_STARCORE }, 83 { "me16", "elf_me16", "em16", 4, EM_ME16 }, 84 { "st100", "elf_st100", "st100", 5, EM_ST100 }, 85 { "tinyj", "elf_tinyj", "tinyj", 5, EM_TINYJ }, 86 { "pdsp", "elf_pdsp", "pdsp", 4, EM_PDSP }, 87 { "fx66", "elf_fx66", "fx66", 4, EM_FX66 }, 88 { "st9plus", "elf_st9plus", "st9plus", 7, EM_ST9PLUS }, 89 { "st7", "elf_st7", "st7", 3, EM_ST7 }, 90 { "m68k", "elf_68hc16", "68hc16", 6, EM_68HC16 }, 91 { "m68k", "elf_68hc11", "68hc11", 6, EM_68HC11 }, 92 { "m68k", "elf_68hc08", "68hc08", 6, EM_68HC08 }, 93 { "m68k", "elf_68hc05", "68hc05", 6, EM_68HC05 }, 94 { "svx", "elf_svx", "svx", 3, EM_SVX }, 95 { "st19", "elf_st19", "st19", 4, EM_ST19 }, 96 { "vax", "elf_vax", "vax", 3, EM_VAX }, 97 { "cris", "elf_cris", "cris", 4, EM_CRIS }, 98 { "javelin", "elf_javelin", "javelin", 7, EM_JAVELIN }, 99 { "firepath", "elf_firepath", "firepath", 8, EM_FIREPATH }, 100 { "zsp", "elf_zsp", "zsp", 3, EM_ZSP}, 101 { "mmix", "elf_mmix", "mmix", 4, EM_MMIX }, 102 { "hunay", "elf_huany", "huany", 5, EM_HUANY }, 103 { "prism", "elf_prism", "prism", 5, EM_PRISM }, 104 { "avr", "elf_avr", "avr", 3, EM_AVR }, 105 { "fr30", "elf_fr30", "fr30", 4, EM_FR30 }, 106 { "dv10", "elf_dv10", "dv10", 4, EM_D10V }, 107 { "dv30", "elf_dv30", "dv30", 4, EM_D30V }, 108 { "v850", "elf_v850", "v850", 4, EM_V850 }, 109 { "m32r", "elf_m32r", "m32r", 4, EM_M32R }, 110 { "mn10300", "elf_mn10300", "mn10300", 7, EM_MN10300 }, 111 { "mn10200", "elf_mn10200", "mn10200", 7, EM_MN10200 }, 112 { "pj", "elf_pj", "pj", 2, EM_PJ }, 113 { "openrisc", "elf_openrisc", "openrisc", 8, EM_OPENRISC }, 114 { "arc", "elf_arc_a5", "arc_a5", 6, EM_ARC_A5 }, 115 { "xtensa", "elf_xtensa", "xtensa", 6, EM_XTENSA }, 116 }; 117 #define nmachines (sizeof (machines) / sizeof (machines[0])) 118 119 120 /* Default callbacks. Mostly they just return the error value. */ 121 static const char *default_object_type_name (int ignore, char *buf, 122 size_t len); 123 static const char *default_reloc_type_name (int ignore, char *buf, size_t len); 124 static bool default_reloc_type_check (int ignore); 125 static bool default_reloc_valid_use (Elf *elf, int ignore); 126 static bool default_gotpc_reloc_check (Elf *elf, int ignore); 127 static const char *default_segment_type_name (int ignore, char *buf, 128 size_t len); 129 static const char *default_section_type_name (int ignore, char *buf, 130 size_t len); 131 static const char *default_section_name (int ignore, int ignore2, char *buf, 132 size_t len); 133 static const char *default_machine_flag_name (Elf64_Word *ignore); 134 static bool default_machine_flag_check (Elf64_Word flags); 135 static const char *default_symbol_type_name (int ignore, char *buf, 136 size_t len); 137 static const char *default_symbol_binding_name (int ignore, char *buf, 138 size_t len); 139 static const char *default_dynamic_tag_name (int64_t ignore, char *buf, 140 size_t len); 141 static bool default_dynamic_tag_check (int64_t ignore); 142 static GElf_Word default_sh_flags_combine (GElf_Word flags1, GElf_Word flags2); 143 static const char *default_osabi_name (int ignore, char *buf, size_t len); 144 static void default_destr (struct ebl *ignore); 145 static const char *default_core_note_type_name (uint32_t, char *buf, 146 size_t len); 147 static const char *default_object_note_type_name (uint32_t, char *buf, 148 size_t len); 149 static bool default_core_note (const char *name, uint32_t type, 150 uint32_t descsz, const char *desc); 151 static bool default_object_note (const char *name, uint32_t type, 152 uint32_t descsz, const char *desc); 153 static bool default_debugscn_p (const char *name); 154 155 156 /* Find an appropriate backend for the file associated with ELF. */ 157 static Ebl * 158 openbackend (elf, emulation, machine) 159 Elf *elf; 160 const char *emulation; 161 GElf_Half machine; 162 { 163 Ebl *result; 164 size_t cnt; 165 166 /* First allocate the data structure for the result. We do this 167 here since this assures that the structure is always large 168 enough. */ 169 result = (Ebl *) calloc (1, sizeof (Ebl)); 170 if (result == NULL) 171 { 172 // XXX uncomment 173 // __libebl_seterror (ELF_E_NOMEM); 174 return NULL; 175 } 176 177 /* Fill in the default callbacks. The initializer for the machine 178 specific module can overwrite the values. */ 179 result->object_type_name = default_object_type_name; 180 result->reloc_type_name = default_reloc_type_name; 181 result->reloc_type_check = default_reloc_type_check; 182 result->reloc_valid_use = default_reloc_valid_use; 183 result->gotpc_reloc_check = default_gotpc_reloc_check; 184 result->segment_type_name = default_segment_type_name; 185 result->section_type_name = default_section_type_name; 186 result->section_name = default_section_name; 187 result->machine_flag_name = default_machine_flag_name; 188 result->machine_flag_check = default_machine_flag_check; 189 result->symbol_type_name = default_symbol_type_name; 190 result->symbol_binding_name = default_symbol_binding_name; 191 result->dynamic_tag_name = default_dynamic_tag_name; 192 result->dynamic_tag_check = default_dynamic_tag_check; 193 result->sh_flags_combine = default_sh_flags_combine; 194 result->osabi_name = default_osabi_name; 195 result->core_note_type_name = default_core_note_type_name; 196 result->object_note_type_name = default_object_note_type_name; 197 result->core_note = default_core_note; 198 result->object_note = default_object_note; 199 result->debugscn_p = default_debugscn_p; 200 result->destr = default_destr; 201 202 /* XXX Currently all we do is to look at 'e_machine' value in the 203 ELF header. With an internal mapping table from EM_* value to 204 DSO name we try to load the appropriate module to handle this 205 binary type. 206 207 Multiple modules for the same machine type are possible and they 208 will be tried in sequence. The lookup process will only stop 209 when a module which can handle the machine type is found or all 210 available matching modules are tried. */ 211 for (cnt = 0; cnt < nmachines; ++cnt) 212 if ((emulation != NULL && strcmp (emulation, machines[cnt].emulation) == 0) 213 || (emulation == NULL && machines[cnt].em == machine)) 214 { 215 /* Well, we know the emulation name now. */ 216 result->emulation = machines[cnt].emulation; 217 218 /* Give it a try. At least the machine type matches. First 219 try to load the module. */ 220 char dsoname[100]; 221 strcpy (stpcpy (stpcpy (dsoname, 222 "$ORIGIN/../" LIBSTR "/elfutils/libebl_"), 223 machines[cnt].dsoname), 224 ".so"); 225 226 void *h = dlopen (dsoname, RTLD_LAZY); 227 if (h == NULL) 228 { 229 strcpy (stpcpy (stpcpy (dsoname, "libebl_"), 230 machines[cnt].dsoname), 231 ".so"); 232 h = dlopen (dsoname, RTLD_LAZY); 233 } 234 235 /* Try without an explicit path. */ 236 if (h != NULL) 237 { 238 /* We managed to load the object. Now see whether the 239 initialization function likes our file. */ 240 ebl_bhinit_t initp; 241 char symname[machines[cnt].prefix_len + sizeof "_init"]; 242 243 strcpy (mempcpy (symname, machines[cnt].prefix, 244 machines[cnt].prefix_len), "_init"); 245 246 initp = (ebl_bhinit_t) dlsym (h, symname); 247 if (initp != NULL 248 && initp (elf, machine, result, sizeof (Ebl)) == 0) 249 { 250 /* We found a module to handle our file. */ 251 result->dlhandle = h; 252 result->elf = elf; 253 254 /* A few entries are mandatory. */ 255 assert (result->name != NULL); 256 assert (result->destr != NULL); 257 258 return result; 259 } 260 261 /* Not the module we need. */ 262 (void) dlclose (h); 263 } 264 265 /* We cannot find a DSO but the emulation/machine ID matches. 266 Return that information. */ 267 result->dlhandle = NULL; 268 result->elf = elf; 269 result->name = machines[cnt].prefix; 270 271 return result; 272 } 273 274 /* Nothing matched. We use only the default callbacks. */ 275 result->dlhandle = NULL; 276 result->elf = elf; 277 result->emulation = "<unknown>"; 278 result->name = "<unknown>"; 279 280 return result; 281 } 282 283 284 /* Find an appropriate backend for the file associated with ELF. */ 285 Ebl * 286 ebl_openbackend (elf) 287 Elf *elf; 288 { 289 GElf_Ehdr ehdr_mem; 290 GElf_Ehdr *ehdr; 291 292 /* Get the ELF header of the object. */ 293 ehdr = gelf_getehdr (elf, &ehdr_mem); 294 if (ehdr == NULL) 295 { 296 // XXX uncomment 297 // __libebl_seterror (elf_errno ()); 298 return NULL; 299 } 300 301 return openbackend (elf, NULL, ehdr->e_machine); 302 } 303 304 305 /* Find backend without underlying ELF file. */ 306 Ebl * 307 ebl_openbackend_machine (machine) 308 GElf_Half machine; 309 { 310 return openbackend (NULL, NULL, machine); 311 } 312 313 314 /* Find backend with given emulation name. */ 315 Ebl * 316 ebl_openbackend_emulation (const char *emulation) 317 { 318 return openbackend (NULL, emulation, EM_NONE); 319 } 320 321 322 /* Default callbacks. Mostly they just return the error value. */ 323 static const char * 324 default_object_type_name (int ignore, char *buf, size_t len) 325 { 326 return NULL; 327 } 328 329 static const char * 330 default_reloc_type_name (int ignore, char *buf, size_t len) 331 { 332 return NULL; 333 } 334 335 static bool 336 default_reloc_type_check (int ignore) 337 { 338 return false; 339 } 340 341 static bool 342 default_reloc_valid_use (Elf *elf, int ignore) 343 { 344 return false; 345 } 346 347 static bool 348 default_gotpc_reloc_check (Elf *elf, int ignore) 349 { 350 return false; 351 } 352 353 static const char * 354 default_segment_type_name (int ignore, char *buf, size_t len) 355 { 356 return NULL; 357 } 358 359 static const char * 360 default_section_type_name (int ignore, char *buf, size_t len) 361 { 362 return NULL; 363 } 364 365 static const char * 366 default_section_name (int ignore, int ignore2, char *buf, size_t len) 367 { 368 return NULL; 369 } 370 371 static const char * 372 default_machine_flag_name (Elf64_Word *ignore) 373 { 374 return NULL; 375 } 376 377 static bool 378 default_machine_flag_check (Elf64_Word flags) 379 { 380 return flags == 0; 381 } 382 383 static const char * 384 default_symbol_type_name (int ignore, char *buf, size_t len) 385 { 386 return NULL; 387 } 388 389 static const char * 390 default_symbol_binding_name (int ignore, char *buf, size_t len) 391 { 392 return NULL; 393 } 394 395 static const char * 396 default_dynamic_tag_name (int64_t ignore, char *buf, size_t len) 397 { 398 return NULL; 399 } 400 401 static bool 402 default_dynamic_tag_check (int64_t ignore) 403 { 404 return false; 405 } 406 407 static GElf_Word 408 default_sh_flags_combine (GElf_Word flags1, GElf_Word flags2) 409 { 410 return SH_FLAGS_COMBINE (flags1, flags2); 411 } 412 413 static void 414 default_destr (struct ebl *ignore) 415 { 416 } 417 418 static const char * 419 default_osabi_name (int ignore, char *buf, size_t len) 420 { 421 return NULL; 422 } 423 424 static const char * 425 default_core_note_type_name (uint32_t ignore, char *buf, size_t len) 426 { 427 return NULL; 428 } 429 430 static const char * 431 default_object_note_type_name (uint32_t ignore, char *buf, size_t len) 432 { 433 return NULL; 434 } 435 436 static bool 437 default_core_note (const char *name, uint32_t type, uint32_t descsz, 438 const char *desc) 439 { 440 return NULL; 441 } 442 443 static bool 444 default_object_note (const char *name, uint32_t type, uint32_t descsz, 445 const char *desc) 446 { 447 return NULL; 448 } 449 450 static bool 451 default_debugscn_p (const char *name) 452 { 453 /* We know by default only about the DWARF debug sections which have 454 fixed names. */ 455 static const char *dwarf_scn_names[] = 456 { 457 /* DWARF 1 */ 458 ".debug", 459 ".line", 460 /* GNU DWARF 1 extensions */ 461 ".debug_srcinfo", 462 ".debug_sfnames", 463 /* DWARF 1.1 and DWARF 2 */ 464 ".debug_aranges", 465 ".debug_pubnames", 466 /* DWARF 2 */ 467 ".debug_info", 468 ".debug_abbrev", 469 ".debug_line", 470 ".debug_frame", 471 ".debug_str", 472 ".debug_loc", 473 ".debug_macinfo", 474 /* DWARF 3 */ 475 ".debug_ranges", 476 /* SGI/MIPS DWARF 2 extensions */ 477 ".debug_weaknames", 478 ".debug_funcnames", 479 ".debug_typenames", 480 ".debug_varnames" 481 }; 482 const size_t ndwarf_scn_names = (sizeof (dwarf_scn_names) 483 / sizeof (dwarf_scn_names[0])); 484 size_t cnt; 485 486 for (cnt = 0; cnt < ndwarf_scn_names; ++cnt) 487 if (strcmp (name, dwarf_scn_names[cnt]) == 0) 488 return true; 489 490 return false; 491 } 492