Changeset 1973 for binutils/trunk/bfd/elf.c
- Timestamp:
- Feb 6, 2017, 1:00:00 PM (8 years ago)
- Location:
- binutils/trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
binutils/trunk ¶
-
Property svn:mergeinfo
set to
/binutils/vendor/current merged eligible
-
Property svn:mergeinfo
set to
-
TabularUnified binutils/trunk/bfd/elf.c ¶
r970 r1973 1 1 /* ELF executable support for BFD. 2 2 3 Copyright (C) 1993-201 4Free Software Foundation, Inc.3 Copyright (C) 1993-2016 Free Software Foundation, Inc. 4 4 5 5 This file is part of BFD, the Binary File Descriptor library. … … 43 43 #include "libiberty.h" 44 44 #include "safe-ctype.h" 45 #include "elf-linux- psinfo.h"45 #include "elf-linux-core.h" 46 46 47 47 #ifdef CORE_HEADER … … 52 52 static bfd_boolean assign_file_positions_except_relocs (bfd *, struct bfd_link_info *); 53 53 static bfd_boolean prep_headers (bfd *); 54 static bfd_boolean swap_out_syms (bfd *, struct bfd_strtab_hash **, int) ;54 static bfd_boolean swap_out_syms (bfd *, struct elf_strtab_hash **, int) ; 55 55 static bfd_boolean elf_read_notes (bfd *, file_ptr, bfd_size_type) ; 56 56 static bfd_boolean elf_parse_notes (bfd *abfd, char *buf, size_t size, … … 298 298 in case the string table is not terminated. */ 299 299 if (shstrtabsize + 1 <= 1 300 || (shstrtab = (bfd_byte *) bfd_alloc (abfd, shstrtabsize + 1)) == NULL301 || bfd_seek (abfd, offset, SEEK_SET) != 0)300 || bfd_seek (abfd, offset, SEEK_SET) != 0 301 || (shstrtab = (bfd_byte *) bfd_alloc (abfd, shstrtabsize + 1)) == NULL) 302 302 shstrtab = NULL; 303 303 else if (bfd_bread (shstrtab, shstrtabsize, abfd) != shstrtabsize) … … 305 305 if (bfd_get_error () != bfd_error_system_call) 306 306 bfd_set_error (bfd_error_file_truncated); 307 bfd_release (abfd, shstrtab); 307 308 shstrtab = NULL; 308 309 /* Once we've failed to read it, make sure we don't keep … … 333 334 hdr = elf_elfsections (abfd)[shindex]; 334 335 335 if (hdr->contents == NULL 336 && bfd_elf_get_str_section (abfd, shindex) == NULL) 337 return NULL; 336 if (hdr->contents == NULL) 337 { 338 if (hdr->sh_type != SHT_STRTAB && hdr->sh_type < SHT_LOOS) 339 { 340 /* PR 17512: file: f057ec89. */ 341 _bfd_error_handler (_("%B: attempt to load strings from a non-string section (number %d)"), 342 abfd, shindex); 343 return NULL; 344 } 345 346 if (bfd_elf_get_str_section (abfd, shindex) == NULL) 347 return NULL; 348 } 338 349 339 350 if (strindex >= hdr->sh_size) … … 390 401 /* Normal syms might have section extension entries. */ 391 402 shndx_hdr = NULL; 392 if (symtab_hdr == &elf_tdata (ibfd)->symtab_hdr) 393 shndx_hdr = &elf_tdata (ibfd)->symtab_shndx_hdr; 403 if (elf_symtab_shndx_list (ibfd) != NULL) 404 { 405 elf_section_list * entry; 406 Elf_Internal_Shdr **sections = elf_elfsections (ibfd); 407 408 /* Find an index section that is linked to this symtab section. */ 409 for (entry = elf_symtab_shndx_list (ibfd); entry != NULL; entry = entry->next) 410 { 411 /* PR 20063. */ 412 if (entry->hdr.sh_link >= elf_numsections (ibfd)) 413 continue; 414 415 if (sections[entry->hdr.sh_link] == symtab_hdr) 416 { 417 shndx_hdr = & entry->hdr; 418 break; 419 }; 420 } 421 422 if (shndx_hdr == NULL) 423 { 424 if (symtab_hdr == & elf_symtab_hdr (ibfd)) 425 /* Not really accurate, but this was how the old code used to work. */ 426 shndx_hdr = & elf_symtab_shndx_list (ibfd)->hdr; 427 /* Otherwise we do nothing. The assumption is that 428 the index table will not be needed. */ 429 } 430 } 394 431 395 432 /* Read the symbols. */ … … 399 436 bed = get_elf_backend_data (ibfd); 400 437 extsym_size = bed->s->sizeof_sym; 401 amt = symcount * extsym_size;438 amt = (bfd_size_type) symcount * extsym_size; 402 439 pos = symtab_hdr->sh_offset + symoffset * extsym_size; 403 440 if (extsym_buf == NULL) … … 418 455 else 419 456 { 420 amt = symcount * sizeof (Elf_External_Sym_Shndx);457 amt = (bfd_size_type) symcount * sizeof (Elf_External_Sym_Shndx); 421 458 pos = shndx_hdr->sh_offset + symoffset * sizeof (Elf_External_Sym_Shndx); 422 459 if (extshndx_buf == NULL) … … 637 674 src = shdr->contents + shdr->sh_size; 638 675 dest = (Elf_Internal_Group *) (shdr->contents + amt); 676 639 677 while (1) 640 678 { … … 806 844 { 807 845 Elf_Internal_Shdr *shdr = elf_tdata (abfd)->group_sect_ptr[i]; 808 Elf_Internal_Group *idx = (Elf_Internal_Group *) shdr->contents; 809 unsigned int n_elt = shdr->sh_size / 4; 846 Elf_Internal_Group *idx; 847 unsigned int n_elt; 848 849 /* PR binutils/18758: Beware of corrupt binaries with invalid group data. */ 850 if (shdr == NULL || shdr->bfd_section == NULL || shdr->contents == NULL) 851 { 852 (*_bfd_error_handler) 853 (_("%B: section group entry number %u is corrupt"), 854 abfd, i); 855 result = FALSE; 856 continue; 857 } 858 859 idx = (Elf_Internal_Group *) shdr->contents; 860 n_elt = shdr->sh_size / 4; 810 861 811 862 while (--n_elt != 0) … … 844 895 } 845 896 897 static char * 898 convert_debug_to_zdebug (bfd *abfd, const char *name) 899 { 900 unsigned int len = strlen (name); 901 char *new_name = bfd_alloc (abfd, len + 2); 902 if (new_name == NULL) 903 return NULL; 904 new_name[0] = '.'; 905 new_name[1] = 'z'; 906 memcpy (new_name + 2, name + 1, len); 907 return new_name; 908 } 909 910 static char * 911 convert_zdebug_to_debug (bfd *abfd, const char *name) 912 { 913 unsigned int len = strlen (name); 914 char *new_name = bfd_alloc (abfd, len); 915 if (new_name == NULL) 916 return NULL; 917 new_name[0] = '.'; 918 memcpy (new_name + 1, name + 2, len - 1); 919 return new_name; 920 } 921 846 922 /* Make a BFD section from an ELF section. We store a pointer to the 847 923 BFD section in the bfd_section field of the header. */ … … 901 977 flags |= SEC_MERGE; 902 978 newsect->entsize = hdr->sh_entsize; 903 if ((hdr->sh_flags & SHF_STRINGS) != 0)904 flags |= SEC_STRINGS; 905 }979 } 980 if ((hdr->sh_flags & SHF_STRINGS) != 0) 981 flags |= SEC_STRINGS; 906 982 if (hdr->sh_flags & SHF_GROUP) 907 983 if (!setup_group (abfd, hdr, newsect)) … … 1030 1106 { 1031 1107 enum { nothing, compress, decompress } action = nothing; 1032 char *new_name; 1033 1034 if (bfd_is_section_compressed (abfd, newsect)) 1108 int compression_header_size; 1109 bfd_size_type uncompressed_size; 1110 bfd_boolean compressed 1111 = bfd_is_section_compressed_with_header (abfd, newsect, 1112 &compression_header_size, 1113 &uncompressed_size); 1114 1115 if (compressed) 1035 1116 { 1036 1117 /* Compressed section. Check if we should decompress. */ … … 1038 1119 action = decompress; 1039 1120 } 1040 else 1041 { 1042 /* Normal section. Check if we should compress. */ 1043 if ((abfd->flags & BFD_COMPRESS) && newsect->size != 0) 1121 1122 /* Compress the uncompressed section or convert from/to .zdebug* 1123 section. Check if we should compress. */ 1124 if (action == nothing) 1125 { 1126 if (newsect->size != 0 1127 && (abfd->flags & BFD_COMPRESS) 1128 && compression_header_size >= 0 1129 && uncompressed_size > 0 1130 && (!compressed 1131 || ((compression_header_size > 0) 1132 != ((abfd->flags & BFD_COMPRESS_GABI) != 0)))) 1044 1133 action = compress; 1045 } 1046 1047 new_name = NULL; 1048 switch (action) 1049 { 1050 case nothing: 1051 break; 1052 case compress: 1134 else 1135 return TRUE; 1136 } 1137 1138 if (action == compress) 1139 { 1053 1140 if (!bfd_init_section_compress_status (abfd, newsect)) 1054 1141 { … … 1058 1145 return FALSE; 1059 1146 } 1060 if (name[1] != 'z') 1061 { 1062 unsigned int len = strlen (name); 1063 1064 new_name = bfd_alloc (abfd, len + 2); 1065 if (new_name == NULL) 1066 return FALSE; 1067 new_name[0] = '.'; 1068 new_name[1] = 'z'; 1069 memcpy (new_name + 2, name + 1, len); 1070 } 1071 break; 1072 case decompress: 1147 } 1148 else 1149 { 1073 1150 if (!bfd_init_section_decompress_status (abfd, newsect)) 1074 1151 { … … 1078 1155 return FALSE; 1079 1156 } 1080 if (name[1] == 'z') 1157 } 1158 1159 if (abfd->is_linker_input) 1160 { 1161 if (name[1] == 'z' 1162 && (action == decompress 1163 || (action == compress 1164 && (abfd->flags & BFD_COMPRESS_GABI) != 0))) 1081 1165 { 1082 unsigned int len = strlen (name); 1083 1084 new_name = bfd_alloc (abfd, len); 1166 /* Convert section name from .zdebug_* to .debug_* so 1167 that linker will consider this section as a debug 1168 section. */ 1169 char *new_name = convert_zdebug_to_debug (abfd, name); 1085 1170 if (new_name == NULL) 1086 1171 return FALSE; 1087 new_name[0] = '.'; 1088 memcpy (new_name + 1, name + 2, len - 1); 1172 bfd_rename_section (abfd, newsect, new_name); 1089 1173 } 1090 break; 1091 } 1092 if (new_name != NULL) 1093 bfd_rename_section (abfd, newsect, new_name); 1174 } 1175 else 1176 /* For objdump, don't rename the section. For objcopy, delay 1177 section rename to elf_fake_sections. */ 1178 newsect->flags |= SEC_ELF_RENAME; 1094 1179 } 1095 1180 … … 1097 1182 } 1098 1183 1099 const char *const bfd_elf_section_type_names[] = { 1184 const char *const bfd_elf_section_type_names[] = 1185 { 1100 1186 "SHT_NULL", "SHT_PROGBITS", "SHT_SYMTAB", "SHT_STRTAB", 1101 1187 "SHT_RELA", "SHT_HASH", "SHT_DYNAMIC", "SHT_NOTE", … … 1135 1221 1136 1222 1223 /* Returns TRUE if section A matches section B. 1224 Names, addresses and links may be different, but everything else 1225 should be the same. */ 1226 1227 static bfd_boolean 1228 section_match (const Elf_Internal_Shdr * a, 1229 const Elf_Internal_Shdr * b) 1230 { 1231 return 1232 a->sh_type == b->sh_type 1233 && (a->sh_flags & ~ SHF_INFO_LINK) 1234 == (b->sh_flags & ~ SHF_INFO_LINK) 1235 && a->sh_addralign == b->sh_addralign 1236 && a->sh_size == b->sh_size 1237 && a->sh_entsize == b->sh_entsize 1238 /* FIXME: Check sh_addr ? */ 1239 ; 1240 } 1241 1242 /* Find a section in OBFD that has the same characteristics 1243 as IHEADER. Return the index of this section or SHN_UNDEF if 1244 none can be found. Check's section HINT first, as this is likely 1245 to be the correct section. */ 1246 1247 static unsigned int 1248 find_link (const bfd * obfd, const Elf_Internal_Shdr * iheader, const unsigned int hint) 1249 { 1250 Elf_Internal_Shdr ** oheaders = elf_elfsections (obfd); 1251 unsigned int i; 1252 1253 if (section_match (oheaders[hint], iheader)) 1254 return hint; 1255 1256 for (i = 1; i < elf_numsections (obfd); i++) 1257 { 1258 Elf_Internal_Shdr * oheader = oheaders[i]; 1259 1260 if (section_match (oheader, iheader)) 1261 /* FIXME: Do we care if there is a potential for 1262 multiple matches ? */ 1263 return i; 1264 } 1265 1266 return SHN_UNDEF; 1267 } 1268 1269 /* PR 19938: Attempt to set the ELF section header fields of an OS or 1270 Processor specific section, based upon a matching input section. 1271 Returns TRUE upon success, FALSE otherwise. */ 1272 1273 static bfd_boolean 1274 copy_special_section_fields (const bfd *ibfd, 1275 bfd *obfd, 1276 const Elf_Internal_Shdr *iheader, 1277 Elf_Internal_Shdr *oheader, 1278 const unsigned int secnum) 1279 { 1280 const struct elf_backend_data *bed = get_elf_backend_data (obfd); 1281 const Elf_Internal_Shdr **iheaders = (const Elf_Internal_Shdr **) elf_elfsections (ibfd); 1282 bfd_boolean changed = FALSE; 1283 unsigned int sh_link; 1284 1285 if (oheader->sh_type == SHT_NOBITS) 1286 { 1287 /* This is a feature for objcopy --only-keep-debug: 1288 When a section's type is changed to NOBITS, we preserve 1289 the sh_link and sh_info fields so that they can be 1290 matched up with the original. 1291 1292 Note: Strictly speaking these assignments are wrong. 1293 The sh_link and sh_info fields should point to the 1294 relevent sections in the output BFD, which may not be in 1295 the same location as they were in the input BFD. But 1296 the whole point of this action is to preserve the 1297 original values of the sh_link and sh_info fields, so 1298 that they can be matched up with the section headers in 1299 the original file. So strictly speaking we may be 1300 creating an invalid ELF file, but it is only for a file 1301 that just contains debug info and only for sections 1302 without any contents. */ 1303 if (oheader->sh_link == 0) 1304 oheader->sh_link = iheader->sh_link; 1305 if (oheader->sh_info == 0) 1306 oheader->sh_info = iheader->sh_info; 1307 return TRUE; 1308 } 1309 1310 /* Allow the target a chance to decide how these fields should be set. */ 1311 if (bed->elf_backend_copy_special_section_fields != NULL 1312 && bed->elf_backend_copy_special_section_fields 1313 (ibfd, obfd, iheader, oheader)) 1314 return TRUE; 1315 1316 /* We have an iheader which might match oheader, and which has non-zero 1317 sh_info and/or sh_link fields. Attempt to follow those links and find 1318 the section in the output bfd which corresponds to the linked section 1319 in the input bfd. */ 1320 if (iheader->sh_link != SHN_UNDEF) 1321 { 1322 sh_link = find_link (obfd, iheaders[iheader->sh_link], iheader->sh_link); 1323 if (sh_link != SHN_UNDEF) 1324 { 1325 oheader->sh_link = sh_link; 1326 changed = TRUE; 1327 } 1328 else 1329 /* FIXME: Should we install iheader->sh_link 1330 if we could not find a match ? */ 1331 (* _bfd_error_handler) 1332 (_("%B: Failed to find link section for section %d"), obfd, secnum); 1333 } 1334 1335 if (iheader->sh_info) 1336 { 1337 /* The sh_info field can hold arbitrary information, but if the 1338 SHF_LINK_INFO flag is set then it should be interpreted as a 1339 section index. */ 1340 if (iheader->sh_flags & SHF_INFO_LINK) 1341 { 1342 sh_link = find_link (obfd, iheaders[iheader->sh_info], 1343 iheader->sh_info); 1344 if (sh_link != SHN_UNDEF) 1345 oheader->sh_flags |= SHF_INFO_LINK; 1346 } 1347 else 1348 /* No idea what it means - just copy it. */ 1349 sh_link = iheader->sh_info; 1350 1351 if (sh_link != SHN_UNDEF) 1352 { 1353 oheader->sh_info = sh_link; 1354 changed = TRUE; 1355 } 1356 else 1357 (* _bfd_error_handler) 1358 (_("%B: Failed to find info section for section %d"), obfd, secnum); 1359 } 1360 1361 return changed; 1362 } 1363 1137 1364 /* Copy the program header and other data from one object module to 1138 1365 another. */ … … 1141 1368 _bfd_elf_copy_private_bfd_data (bfd *ibfd, bfd *obfd) 1142 1369 { 1370 const Elf_Internal_Shdr **iheaders = (const Elf_Internal_Shdr **) elf_elfsections (ibfd); 1371 Elf_Internal_Shdr **oheaders = elf_elfsections (obfd); 1372 const struct elf_backend_data *bed; 1373 unsigned int i; 1374 1143 1375 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour 1144 1376 || bfd_get_flavour (obfd) != bfd_target_elf_flavour) 1145 1377 return TRUE; 1146 1378 … … 1157 1389 elf_elfheader (ibfd)->e_ident[EI_OSABI]; 1158 1390 1391 /* If set, copy the EI_ABIVERSION field. */ 1392 if (elf_elfheader (ibfd)->e_ident[EI_ABIVERSION]) 1393 elf_elfheader (obfd)->e_ident[EI_ABIVERSION] 1394 = elf_elfheader (ibfd)->e_ident[EI_ABIVERSION]; 1395 1159 1396 /* Copy object attributes. */ 1160 1397 _bfd_elf_copy_obj_attributes (ibfd, obfd); 1398 1399 if (iheaders == NULL || oheaders == NULL) 1400 return TRUE; 1401 1402 bed = get_elf_backend_data (obfd); 1403 1404 /* Possibly copy other fields in the section header. */ 1405 for (i = 1; i < elf_numsections (obfd); i++) 1406 { 1407 unsigned int j; 1408 Elf_Internal_Shdr * oheader = oheaders[i]; 1409 1410 /* Ignore ordinary sections. SHT_NOBITS sections are considered however 1411 because of a special case need for generating separate debug info 1412 files. See below for more details. */ 1413 if (oheader == NULL 1414 || (oheader->sh_type != SHT_NOBITS 1415 && oheader->sh_type < SHT_LOOS)) 1416 continue; 1417 1418 /* Ignore empty sections, and sections whose 1419 fields have already been initialised. */ 1420 if (oheader->sh_size == 0 1421 || (oheader->sh_info != 0 && oheader->sh_link != 0)) 1422 continue; 1423 1424 /* Scan for the matching section in the input bfd. 1425 First we try for a direct mapping between the input and output sections. */ 1426 for (j = 1; j < elf_numsections (ibfd); j++) 1427 { 1428 const Elf_Internal_Shdr * iheader = iheaders[j]; 1429 1430 if (iheader == NULL) 1431 continue; 1432 1433 if (oheader->bfd_section != NULL 1434 && iheader->bfd_section != NULL 1435 && iheader->bfd_section->output_section != NULL 1436 && iheader->bfd_section->output_section == oheader->bfd_section) 1437 { 1438 /* We have found a connection from the input section to the 1439 output section. Attempt to copy the header fields. If 1440 this fails then do not try any further sections - there 1441 should only be a one-to-one mapping between input and output. */ 1442 if (! copy_special_section_fields (ibfd, obfd, iheader, oheader, i)) 1443 j = elf_numsections (ibfd); 1444 break; 1445 } 1446 } 1447 1448 if (j < elf_numsections (ibfd)) 1449 continue; 1450 1451 /* That failed. So try to deduce the corresponding input section. 1452 Unfortunately we cannot compare names as the output string table 1453 is empty, so instead we check size, address and type. */ 1454 for (j = 1; j < elf_numsections (ibfd); j++) 1455 { 1456 const Elf_Internal_Shdr * iheader = iheaders[j]; 1457 1458 if (iheader == NULL) 1459 continue; 1460 1461 /* Try matching fields in the input section's header. 1462 Since --only-keep-debug turns all non-debug sections into 1463 SHT_NOBITS sections, the output SHT_NOBITS type matches any 1464 input type. */ 1465 if ((oheader->sh_type == SHT_NOBITS 1466 || iheader->sh_type == oheader->sh_type) 1467 && (iheader->sh_flags & ~ SHF_INFO_LINK) 1468 == (oheader->sh_flags & ~ SHF_INFO_LINK) 1469 && iheader->sh_addralign == oheader->sh_addralign 1470 && iheader->sh_entsize == oheader->sh_entsize 1471 && iheader->sh_size == oheader->sh_size 1472 && iheader->sh_addr == oheader->sh_addr 1473 && (iheader->sh_info != oheader->sh_info 1474 || iheader->sh_link != oheader->sh_link)) 1475 { 1476 if (copy_special_section_fields (ibfd, obfd, iheader, oheader, i)) 1477 break; 1478 } 1479 } 1480 1481 if (j == elf_numsections (ibfd) && oheader->sh_type >= SHT_LOOS) 1482 { 1483 /* Final attempt. Call the backend copy function 1484 with a NULL input section. */ 1485 if (bed->elf_backend_copy_special_section_fields != NULL) 1486 bed->elf_backend_copy_special_section_fields (ibfd, obfd, NULL, oheader); 1487 } 1488 } 1489 1161 1490 return TRUE; 1162 1491 } … … 1255 1584 1256 1585 extdyn = dynbuf; 1586 /* PR 17512: file: 6f427532. */ 1587 if (s->size < extdynsize) 1588 goto error_return; 1257 1589 extdynend = extdyn + s->size; 1258 for (; extdyn < extdynend; extdyn += extdynsize) 1590 /* PR 17512: file: id:000006,sig:06,src:000000,op:flip4,pos:5664. 1591 Fix range check. */ 1592 for (; extdyn <= (extdynend - extdynsize); extdyn += extdynsize) 1259 1593 { 1260 1594 Elf_Internal_Dyn dyn; … … 1424 1758 } 1425 1759 1760 /* Get version string. */ 1761 1762 const char * 1763 _bfd_elf_get_symbol_version_string (bfd *abfd, asymbol *symbol, 1764 bfd_boolean *hidden) 1765 { 1766 const char *version_string = NULL; 1767 if (elf_dynversym (abfd) != 0 1768 && (elf_dynverdef (abfd) != 0 || elf_dynverref (abfd) != 0)) 1769 { 1770 unsigned int vernum = ((elf_symbol_type *) symbol)->version; 1771 1772 *hidden = (vernum & VERSYM_HIDDEN) != 0; 1773 vernum &= VERSYM_VERSION; 1774 1775 if (vernum == 0) 1776 version_string = ""; 1777 else if (vernum == 1) 1778 version_string = "Base"; 1779 else if (vernum <= elf_tdata (abfd)->cverdefs) 1780 version_string = 1781 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename; 1782 else 1783 { 1784 Elf_Internal_Verneed *t; 1785 1786 version_string = ""; 1787 for (t = elf_tdata (abfd)->verref; 1788 t != NULL; 1789 t = t->vn_nextref) 1790 { 1791 Elf_Internal_Vernaux *a; 1792 1793 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr) 1794 { 1795 if (a->vna_other == vernum) 1796 { 1797 version_string = a->vna_nodename; 1798 break; 1799 } 1800 } 1801 } 1802 } 1803 } 1804 return version_string; 1805 } 1806 1426 1807 /* Display ELF-specific fields of a symbol. */ 1427 1808 … … 1450 1831 unsigned char st_other; 1451 1832 bfd_vma val; 1833 const char *version_string; 1834 bfd_boolean hidden; 1452 1835 1453 1836 section_name = symbol->section ? symbol->section->name : "(*none*)"; … … 1475 1858 1476 1859 /* If we have version information, print it. */ 1477 if (elf_dynversym (abfd) != 0 1478 && (elf_dynverdef (abfd) != 0 1479 || elf_dynverref (abfd) != 0)) 1860 version_string = _bfd_elf_get_symbol_version_string (abfd, 1861 symbol, 1862 &hidden); 1863 if (version_string) 1480 1864 { 1481 unsigned int vernum; 1482 const char *version_string; 1483 1484 vernum = ((elf_symbol_type *) symbol)->version & VERSYM_VERSION; 1485 1486 if (vernum == 0) 1487 version_string = ""; 1488 else if (vernum == 1) 1489 version_string = "Base"; 1490 else if (vernum <= elf_tdata (abfd)->cverdefs) 1491 version_string = 1492 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename; 1493 else 1494 { 1495 Elf_Internal_Verneed *t; 1496 1497 version_string = ""; 1498 for (t = elf_tdata (abfd)->verref; 1499 t != NULL; 1500 t = t->vn_nextref) 1501 { 1502 Elf_Internal_Vernaux *a; 1503 1504 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr) 1505 { 1506 if (a->vna_other == vernum) 1507 { 1508 version_string = a->vna_nodename; 1509 break; 1510 } 1511 } 1512 } 1513 } 1514 1515 if ((((elf_symbol_type *) symbol)->version & VERSYM_HIDDEN) == 0) 1865 if (!hidden) 1516 1866 fprintf (file, " %-11s", version_string); 1517 1867 else … … 1546 1896 } 1547 1897 1548 /* Allocate an ELF string table--force the first byte to be zero. */1549 1550 struct bfd_strtab_hash *1551 _bfd_elf_stringtab_init (void)1552 {1553 struct bfd_strtab_hash *ret;1554 1555 ret = _bfd_stringtab_init ();1556 if (ret != NULL)1557 {1558 bfd_size_type loc;1559 1560 loc = _bfd_stringtab_add (ret, "", TRUE, FALSE);1561 BFD_ASSERT (loc == 0 || loc == (bfd_size_type) -1);1562 if (loc == (bfd_size_type) -1)1563 {1564 _bfd_stringtab_free (ret);1565 ret = NULL;1566 }1567 }1568 return ret;1569 }1570 1571 1898 1572 1899 /* ELF .o/exec file reading */ … … 1592 1919 { 1593 1920 /* PR17512: A corrupt ELF binary might contain a recursive group of 1594 sections, each the string indicies pointing to the next in the1921 sections, with each the string indicies pointing to the next in the 1595 1922 loop. Detect this here, by refusing to load a section that we are 1596 1923 already in the process of loading. We only trigger this test if … … 1715 2042 } 1716 2043 1717 BFD_ASSERT (elf_onesymtab (abfd) == 0); 2044 /* PR 18854: A binary might contain more than one symbol table. 2045 Unusual, but possible. Warn, but continue. */ 2046 if (elf_onesymtab (abfd) != 0) 2047 { 2048 (*_bfd_error_handler) 2049 (_("%B: warning: multiple symbol tables detected - ignoring the table in section %u"), 2050 abfd, shindex); 2051 goto success; 2052 } 1718 2053 elf_onesymtab (abfd) = shindex; 1719 elf_ tdata (abfd)->symtab_hdr= *hdr;1720 elf_elfsections (abfd)[shindex] = hdr = & elf_tdata (abfd)->symtab_hdr;2054 elf_symtab_hdr (abfd) = *hdr; 2055 elf_elfsections (abfd)[shindex] = hdr = & elf_symtab_hdr (abfd); 1721 2056 abfd->flags |= HAS_SYMS; 1722 2057 … … 1736 2071 can't read symbols without that section loaded as well. It 1737 2072 is most likely specified by the next section header. */ 1738 if (elf_elfsections (abfd)[elf_symtab_shndx (abfd)]->sh_link != shindex) 1739 { 1740 unsigned int i, num_sec; 1741 1742 num_sec = elf_numsections (abfd); 1743 for (i = shindex + 1; i < num_sec; i++) 2073 { 2074 elf_section_list * entry; 2075 unsigned int i, num_sec; 2076 2077 for (entry = elf_symtab_shndx_list (abfd); entry != NULL; entry = entry->next) 2078 if (entry->hdr.sh_link == shindex) 2079 goto success; 2080 2081 num_sec = elf_numsections (abfd); 2082 for (i = shindex + 1; i < num_sec; i++) 2083 { 2084 Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i]; 2085 2086 if (hdr2->sh_type == SHT_SYMTAB_SHNDX 2087 && hdr2->sh_link == shindex) 2088 break; 2089 } 2090 2091 if (i == num_sec) 2092 for (i = 1; i < shindex; i++) 1744 2093 { 1745 2094 Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i]; 2095 1746 2096 if (hdr2->sh_type == SHT_SYMTAB_SHNDX 1747 2097 && hdr2->sh_link == shindex) 1748 2098 break; 1749 2099 } 1750 if (i == num_sec) 1751 for (i = 1; i < shindex; i++) 1752 { 1753 Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i]; 1754 if (hdr2->sh_type == SHT_SYMTAB_SHNDX 1755 && hdr2->sh_link == shindex) 1756 break; 1757 } 1758 if (i != shindex) 1759 ret = bfd_section_from_shdr (abfd, i); 1760 } 1761 goto success; 2100 2101 if (i != shindex) 2102 ret = bfd_section_from_shdr (abfd, i); 2103 /* else FIXME: we have failed to find the symbol table - should we issue an error ? */ 2104 goto success; 2105 } 1762 2106 1763 2107 case SHT_DYNSYM: /* A dynamic symbol table. */ … … 1780 2124 } 1781 2125 1782 BFD_ASSERT (elf_dynsymtab (abfd) == 0); 2126 /* PR 18854: A binary might contain more than one dynamic symbol table. 2127 Unusual, but possible. Warn, but continue. */ 2128 if (elf_dynsymtab (abfd) != 0) 2129 { 2130 (*_bfd_error_handler) 2131 (_("%B: warning: multiple dynamic symbol tables detected - ignoring the table in section %u"), 2132 abfd, shindex); 2133 goto success; 2134 } 1783 2135 elf_dynsymtab (abfd) = shindex; 1784 2136 elf_tdata (abfd)->dynsymtab_hdr = *hdr; … … 1792 2144 1793 2145 case SHT_SYMTAB_SHNDX: /* Symbol section indices when >64k sections. */ 1794 if (elf_symtab_shndx (abfd) == shindex) 2146 { 2147 elf_section_list * entry; 2148 2149 for (entry = elf_symtab_shndx_list (abfd); entry != NULL; entry = entry->next) 2150 if (entry->ndx == shindex) 2151 goto success; 2152 2153 entry = bfd_alloc (abfd, sizeof * entry); 2154 if (entry == NULL) 2155 goto fail; 2156 entry->ndx = shindex; 2157 entry->hdr = * hdr; 2158 entry->next = elf_symtab_shndx_list (abfd); 2159 elf_symtab_shndx_list (abfd) = entry; 2160 elf_elfsections (abfd)[shindex] = & entry->hdr; 1795 2161 goto success; 1796 1797 BFD_ASSERT (elf_symtab_shndx (abfd) == 0); 1798 elf_symtab_shndx (abfd) = shindex; 1799 elf_tdata (abfd)->symtab_shndx_hdr = *hdr; 1800 elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->symtab_shndx_hdr; 1801 goto success; 2162 } 1802 2163 1803 2164 case SHT_STRTAB: /* A string table. */ … … 1869 2230 unsigned int num_sec = elf_numsections (abfd); 1870 2231 struct bfd_elf_section_data *esdt; 1871 bfd_size_type amt;1872 2232 1873 2233 if (hdr->sh_entsize … … 1959 2319 p_hdr = &esdt->rel.hdr; 1960 2320 1961 BFD_ASSERT (*p_hdr == NULL); 1962 amt = sizeof (*hdr2); 1963 hdr2 = (Elf_Internal_Shdr *) bfd_alloc (abfd, amt); 2321 /* PR 17512: file: 0b4f81b7. */ 2322 if (*p_hdr != NULL) 2323 goto fail; 2324 hdr2 = (Elf_Internal_Shdr *) bfd_alloc (abfd, sizeof (*hdr2)); 1964 2325 if (hdr2 == NULL) 1965 2326 goto fail; … … 2016 2377 { 2017 2378 Elf_Internal_Group *idx = (Elf_Internal_Group *) hdr->contents; 2018 unsigned int n_elt = hdr->sh_size / GRP_ENTRY_SIZE;2379 unsigned int n_elt = hdr->sh_size / sizeof (* idx); 2019 2380 asection *s; 2020 2381 2382 if (n_elt == 0) 2383 goto fail; 2021 2384 if (idx->flags & GRP_COMDAT) 2022 2385 hdr->bfd_section->flags … … 2025 2388 /* We try to keep the same section order as it comes in. */ 2026 2389 idx += n_elt; 2390 2027 2391 while (--n_elt != 0) 2028 2392 { … … 2623 2987 } 2624 2988 2989 static bfd_boolean 2990 _bfd_elf_set_reloc_sh_name (bfd *abfd, 2991 Elf_Internal_Shdr *rel_hdr, 2992 const char *sec_name, 2993 bfd_boolean use_rela_p) 2994 { 2995 char *name = (char *) bfd_alloc (abfd, 2996 sizeof ".rela" + strlen (sec_name)); 2997 if (name == NULL) 2998 return FALSE; 2999 3000 sprintf (name, "%s%s", use_rela_p ? ".rela" : ".rel", sec_name); 3001 rel_hdr->sh_name = 3002 (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd), name, 3003 FALSE); 3004 if (rel_hdr->sh_name == (unsigned int) -1) 3005 return FALSE; 3006 3007 return TRUE; 3008 } 3009 2625 3010 /* Allocate and initialize a section-header for a new reloc section, 2626 3011 containing relocations against ASECT. It is stored in RELDATA. If … … 2631 3016 _bfd_elf_init_reloc_shdr (bfd *abfd, 2632 3017 struct bfd_elf_section_reloc_data *reldata, 2633 asection *asect, 2634 bfd_boolean use_rela_p) 3018 const char *sec_name, 3019 bfd_boolean use_rela_p, 3020 bfd_boolean delay_st_name_p) 2635 3021 { 2636 3022 Elf_Internal_Shdr *rel_hdr; 2637 char *name;2638 3023 const struct elf_backend_data *bed = get_elf_backend_data (abfd); 2639 bfd_size_type amt; 2640 2641 amt = sizeof (Elf_Internal_Shdr); 3024 2642 3025 BFD_ASSERT (reldata->hdr == NULL); 2643 rel_hdr = bfd_zalloc (abfd, amt);3026 rel_hdr = bfd_zalloc (abfd, sizeof (*rel_hdr)); 2644 3027 reldata->hdr = rel_hdr; 2645 3028 2646 amt = sizeof ".rela" + strlen (asect->name); 2647 name = (char *) bfd_alloc (abfd, amt); 2648 if (name == NULL) 2649 return FALSE; 2650 sprintf (name, "%s%s", use_rela_p ? ".rela" : ".rel", asect->name); 2651 rel_hdr->sh_name = 2652 (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd), name, 2653 FALSE); 2654 if (rel_hdr->sh_name == (unsigned int) -1) 3029 if (delay_st_name_p) 3030 rel_hdr->sh_name = (unsigned int) -1; 3031 else if (!_bfd_elf_set_reloc_sh_name (abfd, rel_hdr, sec_name, 3032 use_rela_p)) 2655 3033 return FALSE; 2656 3034 rel_hdr->sh_type = use_rela_p ? SHT_RELA : SHT_REL; … … 2694 3072 Elf_Internal_Shdr *this_hdr; 2695 3073 unsigned int sh_type; 3074 const char *name = asect->name; 3075 bfd_boolean delay_st_name_p = FALSE; 2696 3076 2697 3077 if (arg->failed) … … 2704 3084 this_hdr = &esd->this_hdr; 2705 3085 2706 this_hdr->sh_name = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd), 2707 asect->name, FALSE); 2708 if (this_hdr->sh_name == (unsigned int) -1) 2709 { 2710 arg->failed = TRUE; 2711 return; 3086 if (arg->link_info) 3087 { 3088 /* ld: compress DWARF debug sections with names: .debug_*. */ 3089 if ((arg->link_info->compress_debug & COMPRESS_DEBUG) 3090 && (asect->flags & SEC_DEBUGGING) 3091 && name[1] == 'd' 3092 && name[6] == '_') 3093 { 3094 /* Set SEC_ELF_COMPRESS to indicate this section should be 3095 compressed. */ 3096 asect->flags |= SEC_ELF_COMPRESS; 3097 3098 /* If this section will be compressed, delay adding setion 3099 name to section name section after it is compressed in 3100 _bfd_elf_assign_file_positions_for_non_load. */ 3101 delay_st_name_p = TRUE; 3102 } 3103 } 3104 else if ((asect->flags & SEC_ELF_RENAME)) 3105 { 3106 /* objcopy: rename output DWARF debug section. */ 3107 if ((abfd->flags & (BFD_DECOMPRESS | BFD_COMPRESS_GABI))) 3108 { 3109 /* When we decompress or compress with SHF_COMPRESSED, 3110 convert section name from .zdebug_* to .debug_* if 3111 needed. */ 3112 if (name[1] == 'z') 3113 { 3114 char *new_name = convert_zdebug_to_debug (abfd, name); 3115 if (new_name == NULL) 3116 { 3117 arg->failed = TRUE; 3118 return; 3119 } 3120 name = new_name; 3121 } 3122 } 3123 else if (asect->compress_status == COMPRESS_SECTION_DONE) 3124 { 3125 /* PR binutils/18087: Compression does not always make a 3126 section smaller. So only rename the section when 3127 compression has actually taken place. If input section 3128 name is .zdebug_*, we should never compress it again. */ 3129 char *new_name = convert_debug_to_zdebug (abfd, name); 3130 if (new_name == NULL) 3131 { 3132 arg->failed = TRUE; 3133 return; 3134 } 3135 BFD_ASSERT (name[1] != 'z'); 3136 name = new_name; 3137 } 3138 } 3139 3140 if (delay_st_name_p) 3141 this_hdr->sh_name = (unsigned int) -1; 3142 else 3143 { 3144 this_hdr->sh_name 3145 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd), 3146 name, FALSE); 3147 if (this_hdr->sh_name == (unsigned int) -1) 3148 { 3149 arg->failed = TRUE; 3150 return; 3151 } 2712 3152 } 2713 3153 … … 2723 3163 this_hdr->sh_size = asect->size; 2724 3164 this_hdr->sh_link = 0; 3165 /* PR 17512: file: 0eb809fe, 8b0535ee. */ 3166 if (asect->alignment_power >= (sizeof (bfd_vma) * 8) - 1) 3167 { 3168 (*_bfd_error_handler) 3169 (_("%B: error: Alignment power %d of section `%A' is too big"), 3170 abfd, asect, asect->alignment_power); 3171 arg->failed = TRUE; 3172 return; 3173 } 2725 3174 this_hdr->sh_addralign = (bfd_vma) 1 << asect->alignment_power; 2726 3175 /* The sh_entsize and sh_info fields may have been set already by … … 2758 3207 2759 3208 case SHT_STRTAB: 3209 case SHT_NOTE: 3210 case SHT_NOBITS: 3211 case SHT_PROGBITS: 3212 break; 3213 2760 3214 case SHT_INIT_ARRAY: 2761 3215 case SHT_FINI_ARRAY: 2762 3216 case SHT_PREINIT_ARRAY: 2763 case SHT_NOTE: 2764 case SHT_NOBITS: 2765 case SHT_PROGBITS: 3217 this_hdr->sh_entsize = bed->s->arch_size / 8; 2766 3218 break; 2767 3219 … … 2835 3287 this_hdr->sh_flags |= SHF_MERGE; 2836 3288 this_hdr->sh_entsize = asect->entsize; 2837 if ((asect->flags & SEC_STRINGS) != 0)2838 this_hdr->sh_flags |= SHF_STRINGS; 2839 }3289 } 3290 if ((asect->flags & SEC_STRINGS) != 0) 3291 this_hdr->sh_flags |= SHF_STRINGS; 2840 3292 if ((asect->flags & SEC_GROUP) == 0 && elf_group_name (asect) != NULL) 2841 3293 this_hdr->sh_flags |= SHF_GROUP; … … 2871 3323 /* Do the normal setup if we wouldn't create any sections here. */ 2872 3324 && esd->rel.count + esd->rela.count > 0 2873 && (arg->link_info->relocatable || arg->link_info->emitrelocations)) 3325 && (bfd_link_relocatable (arg->link_info) 3326 || arg->link_info->emitrelocations)) 2874 3327 { 2875 3328 if (esd->rel.count && esd->rel.hdr == NULL 2876 && !_bfd_elf_init_reloc_shdr (abfd, &esd->rel, asect, FALSE)) 3329 && !_bfd_elf_init_reloc_shdr (abfd, &esd->rel, name, FALSE, 3330 delay_st_name_p)) 2877 3331 { 2878 3332 arg->failed = TRUE; … … 2880 3334 } 2881 3335 if (esd->rela.count && esd->rela.hdr == NULL 2882 && !_bfd_elf_init_reloc_shdr (abfd, &esd->rela, asect, TRUE)) 3336 && !_bfd_elf_init_reloc_shdr (abfd, &esd->rela, name, TRUE, 3337 delay_st_name_p)) 2883 3338 { 2884 3339 arg->failed = TRUE; … … 2889 3344 (asect->use_rela_p 2890 3345 ? &esd->rela : &esd->rel), 2891 asect, 2892 asect->use_rela_p)) 3346 name, 3347 asect->use_rela_p, 3348 delay_st_name_p)) 2893 3349 arg->failed = TRUE; 2894 3350 } … … 3024 3480 } 3025 3481 3482 /* Return the section which RELOC_SEC applies to. */ 3483 3484 asection * 3485 _bfd_elf_get_reloc_section (asection *reloc_sec) 3486 { 3487 const char *name; 3488 unsigned int type; 3489 bfd *abfd; 3490 3491 if (reloc_sec == NULL) 3492 return NULL; 3493 3494 type = elf_section_data (reloc_sec)->this_hdr.sh_type; 3495 if (type != SHT_REL && type != SHT_RELA) 3496 return NULL; 3497 3498 /* We look up the section the relocs apply to by name. */ 3499 name = reloc_sec->name; 3500 if (type == SHT_REL) 3501 name += 4; 3502 else 3503 name += 5; 3504 3505 /* If a target needs .got.plt section, relocations in rela.plt/rel.plt 3506 section apply to .got.plt section. */ 3507 abfd = reloc_sec->owner; 3508 if (get_elf_backend_data (abfd)->want_got_plt 3509 && strcmp (name, ".plt") == 0) 3510 { 3511 /* .got.plt is a linker created input section. It may be mapped 3512 to some other output section. Try two likely sections. */ 3513 name = ".got.plt"; 3514 reloc_sec = bfd_get_section_by_name (abfd, name); 3515 if (reloc_sec != NULL) 3516 return reloc_sec; 3517 name = ".got"; 3518 } 3519 3520 reloc_sec = bfd_get_section_by_name (abfd, name); 3521 return reloc_sec; 3522 } 3523 3026 3524 /* Assign all ELF section numbers. The dummy first section is handled here 3027 3525 too. The link/info pointers for the standard section types are filled … … 3033 3531 struct elf_obj_tdata *t = elf_tdata (abfd); 3034 3532 asection *sec; 3035 unsigned int section_number , secn;3533 unsigned int section_number; 3036 3534 Elf_Internal_Shdr **i_shdrp; 3037 3535 struct bfd_elf_section_data *d; … … 3043 3541 3044 3542 /* SHT_GROUP sections are in relocatable files only. */ 3045 if (link_info == NULL || link_info->relocatable) 3046 { 3543 if (link_info == NULL || bfd_link_relocatable (link_info)) 3544 { 3545 size_t reloc_count = 0; 3546 3047 3547 /* Put SHT_GROUP sections first. */ 3048 3548 for (sec = abfd->sections; sec != NULL; sec = sec->next) … … 3061 3561 d->this_idx = section_number++; 3062 3562 } 3063 } 3563 3564 /* Count relocations. */ 3565 reloc_count += sec->reloc_count; 3566 } 3567 3568 /* Clear HAS_RELOC if there are no relocations. */ 3569 if (reloc_count == 0) 3570 abfd->flags &= ~HAS_RELOC; 3064 3571 } 3065 3572 … … 3070 3577 if (d->this_hdr.sh_type != SHT_GROUP) 3071 3578 d->this_idx = section_number++; 3072 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->this_hdr.sh_name); 3579 if (d->this_hdr.sh_name != (unsigned int) -1) 3580 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->this_hdr.sh_name); 3073 3581 if (d->rel.hdr) 3074 3582 { 3075 3583 d->rel.idx = section_number++; 3076 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rel.hdr->sh_name); 3584 if (d->rel.hdr->sh_name != (unsigned int) -1) 3585 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rel.hdr->sh_name); 3077 3586 } 3078 3587 else … … 3082 3591 { 3083 3592 d->rela.idx = section_number++; 3084 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rela.hdr->sh_name); 3593 if (d->rela.hdr->sh_name != (unsigned int) -1) 3594 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rela.hdr->sh_name); 3085 3595 } 3086 3596 else … … 3102 3612 if (section_number > ((SHN_LORESERVE - 2) & 0xFFFF)) 3103 3613 { 3104 elf_symtab_shndx (abfd) = section_number++; 3105 t->symtab_shndx_hdr.sh_name 3614 elf_section_list * entry; 3615 3616 BFD_ASSERT (elf_symtab_shndx_list (abfd) == NULL); 3617 3618 entry = bfd_zalloc (abfd, sizeof * entry); 3619 entry->ndx = section_number++; 3620 elf_symtab_shndx_list (abfd) = entry; 3621 entry->hdr.sh_name 3106 3622 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd), 3107 3623 ".symtab_shndx", FALSE); 3108 if ( t->symtab_shndx_hdr.sh_name == (unsigned int) -1)3624 if (entry->hdr.sh_name == (unsigned int) -1) 3109 3625 return FALSE; 3110 3626 } … … 3119 3635 return FALSE; 3120 3636 } 3121 3122 _bfd_elf_strtab_finalize (elf_shstrtab (abfd));3123 t->shstrtab_hdr.sh_size = _bfd_elf_strtab_size (elf_shstrtab (abfd));3124 3637 3125 3638 elf_numsections (abfd) = section_number; … … 3149 3662 if (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF)) 3150 3663 { 3151 i_shdrp[elf_symtab_shndx (abfd)] = &t->symtab_shndx_hdr; 3152 t->symtab_shndx_hdr.sh_link = elf_onesymtab (abfd); 3664 elf_section_list * entry = elf_symtab_shndx_list (abfd); 3665 BFD_ASSERT (entry != NULL); 3666 i_shdrp[entry->ndx] = & entry->hdr; 3667 entry->hdr.sh_link = elf_onesymtab (abfd); 3153 3668 } 3154 3669 i_shdrp[elf_strtab_sec (abfd)] = &t->strtab_hdr; … … 3159 3674 { 3160 3675 asection *s; 3161 const char *name;3162 3676 3163 3677 d = elf_section_data (sec); … … 3263 3777 d->this_hdr.sh_link = elf_section_data (s)->this_idx; 3264 3778 3265 /* We look up the section the relocs apply to by name. */ 3266 name = sec->name; 3267 if (d->this_hdr.sh_type == SHT_REL) 3268 name += 4; 3269 else 3270 name += 5; 3271 s = bfd_get_section_by_name (abfd, name); 3779 s = get_elf_backend_data (abfd)->get_reloc_section (sec); 3272 3780 if (s != NULL) 3273 3781 { … … 3345 3853 } 3346 3854 3347 for (secn = 1; secn < section_number; ++secn) 3348 if (i_shdrp[secn] == NULL) 3349 i_shdrp[secn] = i_shdrp[0]; 3350 else 3351 i_shdrp[secn]->sh_name = _bfd_elf_strtab_offset (elf_shstrtab (abfd), 3352 i_shdrp[secn]->sh_name); 3855 /* Delay setting sh_name to _bfd_elf_write_object_contents so that 3856 _bfd_elf_assign_file_positions_for_non_load can convert DWARF 3857 debug section name from .debug_* to .zdebug_* if needed. */ 3858 3353 3859 return TRUE; 3354 3860 } … … 3401 3907 unsigned int num_locals2 = 0; 3402 3908 unsigned int num_globals2 = 0; 3403 int max_index = 0;3909 unsigned int max_index = 0; 3404 3910 unsigned int idx; 3405 3911 asection *asect; … … 3550 4056 struct fake_section_arg fsargs; 3551 4057 bfd_boolean failed; 3552 struct bfd_strtab_hash *strtab = NULL;4058 struct elf_strtab_hash *strtab = NULL; 3553 4059 Elf_Internal_Shdr *shstrtab_hdr; 3554 4060 bfd_boolean need_symtab; … … 3601 4107 /* sh_name was set in prep_headers. */ 3602 4108 shstrtab_hdr->sh_type = SHT_STRTAB; 3603 shstrtab_hdr->sh_flags = 0;4109 shstrtab_hdr->sh_flags = bed->elf_strtab_flags; 3604 4110 shstrtab_hdr->sh_addr = 0; 3605 shstrtab_hdr->sh_size = _bfd_elf_strtab_size (elf_shstrtab (abfd));4111 /* sh_size is set in _bfd_elf_assign_file_positions_for_non_load. */ 3606 4112 shstrtab_hdr->sh_entsize = 0; 3607 4113 shstrtab_hdr->sh_link = 0; 3608 4114 shstrtab_hdr->sh_info = 0; 3609 /* sh_offset is set in assign_file_positions_except_relocs. */4115 /* sh_offset is set in _bfd_elf_assign_file_positions_for_non_load. */ 3610 4116 shstrtab_hdr->sh_addralign = 1; 3611 4117 … … 3620 4126 off = elf_next_file_pos (abfd); 3621 4127 3622 hdr = & elf_tdata (abfd)->symtab_hdr;4128 hdr = & elf_symtab_hdr (abfd); 3623 4129 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE); 3624 4130 3625 hdr = &elf_tdata (abfd)->symtab_shndx_hdr; 3626 if (hdr->sh_size != 0) 3627 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE); 4131 if (elf_symtab_shndx_list (abfd) != NULL) 4132 { 4133 hdr = & elf_symtab_shndx_list (abfd)->hdr; 4134 if (hdr->sh_size != 0) 4135 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE); 4136 /* FIXME: What about other symtab_shndx sections in the list ? */ 4137 } 3628 4138 3629 4139 hdr = &elf_tdata (abfd)->strtab_hdr; … … 3635 4145 out. */ 3636 4146 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0 3637 || ! _bfd_ stringtab_emit (abfd, strtab))4147 || ! _bfd_elf_strtab_emit (abfd, strtab)) 3638 4148 return FALSE; 3639 _bfd_ stringtab_free (strtab);4149 _bfd_elf_strtab_free (strtab); 3640 4150 } 3641 4151 … … 3976 4486 phdr_index = 0; 3977 4487 maxpagesize = bed->maxpagesize; 4488 /* PR 17512: file: c8455299. 4489 Avoid divide-by-zero errors later on. 4490 FIXME: Should we abort if the maxpagesize is zero ? */ 4491 if (maxpagesize == 0) 4492 maxpagesize = 1; 3978 4493 writable = FALSE; 3979 4494 dynsec = bfd_get_section_by_name (abfd, ".dynamic"); … … 4046 4561 } 4047 4562 else if ((last_hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0 4048 && (hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) != 0) 4563 && (hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) != 0 4564 && ((abfd->flags & D_PAGED) == 0 4565 || (((last_hdr->lma + last_size - 1) & -maxpagesize) 4566 != (hdr->lma & -maxpagesize)))) 4049 4567 { 4050 /* We don't want to put a loadable section after a 4051 nonloadable section in the same segment. 4052 Consider .tbss sections as loadable for this purpose. */ 4568 /* We don't want to put a loaded section after a 4569 nonloaded (ie. bss style) section in the same segment 4570 as that will force the non-loaded section to be loaded. 4571 Consider .tbss sections as loaded for this purpose. 4572 However, like the writable/non-writable case below, 4573 if they are on the same page then they must be put 4574 in the same segment. */ 4053 4575 new_segment = TRUE; 4054 4576 } … … 4321 4843 m->next = NULL; 4322 4844 m->p_type = PT_GNU_RELRO; 4323 m->p_flags = PF_R;4324 m->p_flags_valid = 1;4325 4326 4845 *pm = m; 4327 4846 pm = &m->next; … … 4963 5482 const struct elf_backend_data *bed = get_elf_backend_data (abfd); 4964 5483 Elf_Internal_Shdr **i_shdrpp; 4965 Elf_Internal_Shdr **hdrpp ;5484 Elf_Internal_Shdr **hdrpp, **end_hdrpp; 4966 5485 Elf_Internal_Phdr *phdrs; 4967 5486 Elf_Internal_Phdr *p; … … 4971 5490 bfd_vma phdrs_vaddr, phdrs_paddr; 4972 5491 file_ptr off; 4973 unsigned int num_sec;4974 unsigned int i;4975 5492 unsigned int count; 4976 5493 4977 5494 i_shdrpp = elf_elfsections (abfd); 4978 num_sec =elf_numsections (abfd);5495 end_hdrpp = i_shdrpp + elf_numsections (abfd); 4979 5496 off = elf_next_file_pos (abfd); 4980 for ( i = 1, hdrpp = i_shdrpp + 1; i < num_sec; i++,hdrpp++)5497 for (hdrpp = i_shdrpp + 1; hdrpp < end_hdrpp; hdrpp++) 4981 5498 { 4982 5499 Elf_Internal_Shdr *hdr; … … 5009 5526 else if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA) 5010 5527 && hdr->bfd_section == NULL) 5528 || (hdr->bfd_section != NULL 5529 && (hdr->bfd_section->flags & SEC_ELF_COMPRESS)) 5530 /* Compress DWARF debug sections. */ 5011 5531 || hdr == i_shdrpp[elf_onesymtab (abfd)] 5012 || hdr == i_shdrpp[elf_symtab_shndx (abfd)] 5013 || hdr == i_shdrpp[elf_strtab_sec (abfd)]) 5532 || (elf_symtab_shndx_list (abfd) != NULL 5533 && hdr == i_shdrpp[elf_symtab_shndx_list (abfd)->ndx]) 5534 || hdr == i_shdrpp[elf_strtab_sec (abfd)] 5535 || hdr == i_shdrpp[elf_shstrtab_sec (abfd)]) 5014 5536 hdr->sh_offset = -1; 5015 5537 else … … 5152 5674 p->p_align = 1; 5153 5675 if (!m->p_flags_valid) 5154 p->p_flags = (lp->p_flags & ~PF_W);5676 p->p_flags = PF_R; 5155 5677 } 5156 5678 else … … 5167 5689 else if (m->count != 0) 5168 5690 { 5691 unsigned int i; 5169 5692 if (p->p_type != PT_LOAD 5170 5693 && (p->p_type != PT_NOTE 5171 5694 || bfd_get_format (abfd) != bfd_core)) 5172 5695 { 5173 BFD_ASSERT (!m->includes_filehdr && !m->includes_phdrs); 5696 if (m->includes_filehdr || m->includes_phdrs) 5697 { 5698 /* PR 17512: file: 2195325e. */ 5699 (*_bfd_error_handler) 5700 (_("%B: warning: non-load segment includes file header and/or program header"), 5701 abfd); 5702 return FALSE; 5703 } 5174 5704 5175 5705 p->p_filesz = 0; … … 5207 5737 } 5208 5738 5739 static elf_section_list * 5740 find_section_in_list (unsigned int i, elf_section_list * list) 5741 { 5742 for (;list != NULL; list = list->next) 5743 if (list->ndx == i) 5744 break; 5745 return list; 5746 } 5747 5209 5748 /* Work out the file positions of all the sections. This is called by 5210 5749 _bfd_elf_compute_section_file_positions. All the section sizes and … … 5251 5790 if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA) 5252 5791 && hdr->bfd_section == NULL) 5792 || (hdr->bfd_section != NULL 5793 && (hdr->bfd_section->flags & SEC_ELF_COMPRESS)) 5794 /* Compress DWARF debug sections. */ 5253 5795 || i == elf_onesymtab (abfd) 5254 || i == elf_symtab_shndx (abfd) 5255 || i == elf_strtab_sec (abfd)) 5796 || (elf_symtab_shndx_list (abfd) != NULL 5797 && hdr == i_shdrpp[elf_symtab_shndx_list (abfd)->ndx]) 5798 || i == elf_strtab_sec (abfd) 5799 || i == elf_shstrtab_sec (abfd)) 5256 5800 { 5257 5801 hdr->sh_offset = -1; … … 5283 5827 5284 5828 /* Set e_type in ELF header to ET_EXEC for -pie -Ttext-segment=. */ 5285 if (link_info != NULL 5286 && link_info->executable 5287 && link_info->shared) 5829 if (link_info != NULL && bfd_link_pie (link_info)) 5288 5830 { 5289 5831 unsigned int num_segments = elf_elfheader (abfd)->e_phnum; … … 5404 5946 of the loadable file image, and the file position of section headers. */ 5405 5947 5406 static void5407 _bfd_elf_assign_file_positions_for_ relocs(bfd *abfd)5948 static bfd_boolean 5949 _bfd_elf_assign_file_positions_for_non_load (bfd *abfd) 5408 5950 { 5409 5951 file_ptr off; 5410 unsigned int i, num_sec;5411 Elf_Internal_Shdr * *shdrpp;5952 Elf_Internal_Shdr **shdrpp, **end_shdrpp; 5953 Elf_Internal_Shdr *shdrp; 5412 5954 Elf_Internal_Ehdr *i_ehdrp; 5413 5955 const struct elf_backend_data *bed; … … 5415 5957 off = elf_next_file_pos (abfd); 5416 5958 5417 num_sec = elf_numsections (abfd); 5418 for (i = 1, shdrpp = elf_elfsections (abfd) + 1; i < num_sec; i++, shdrpp++) 5419 { 5420 Elf_Internal_Shdr *shdrp; 5421 5959 shdrpp = elf_elfsections (abfd); 5960 end_shdrpp = shdrpp + elf_numsections (abfd); 5961 for (shdrpp++; shdrpp < end_shdrpp; shdrpp++) 5962 { 5422 5963 shdrp = *shdrpp; 5423 if ((shdrp->sh_type == SHT_REL || shdrp->sh_type == SHT_RELA) 5424 && shdrp->sh_offset == -1) 5425 off = _bfd_elf_assign_file_position_for_section (shdrp, off, TRUE); 5426 } 5427 5428 /* Place the section headers. */ 5964 if (shdrp->sh_offset == -1) 5965 { 5966 asection *sec = shdrp->bfd_section; 5967 bfd_boolean is_rel = (shdrp->sh_type == SHT_REL 5968 || shdrp->sh_type == SHT_RELA); 5969 if (is_rel 5970 || (sec != NULL && (sec->flags & SEC_ELF_COMPRESS))) 5971 { 5972 if (!is_rel) 5973 { 5974 const char *name = sec->name; 5975 struct bfd_elf_section_data *d; 5976 5977 /* Compress DWARF debug sections. */ 5978 if (!bfd_compress_section (abfd, sec, 5979 shdrp->contents)) 5980 return FALSE; 5981 5982 if (sec->compress_status == COMPRESS_SECTION_DONE 5983 && (abfd->flags & BFD_COMPRESS_GABI) == 0) 5984 { 5985 /* If section is compressed with zlib-gnu, convert 5986 section name from .debug_* to .zdebug_*. */ 5987 char *new_name 5988 = convert_debug_to_zdebug (abfd, name); 5989 if (new_name == NULL) 5990 return FALSE; 5991 name = new_name; 5992 } 5993 /* Add setion name to section name section. */ 5994 if (shdrp->sh_name != (unsigned int) -1) 5995 abort (); 5996 shdrp->sh_name 5997 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd), 5998 name, FALSE); 5999 d = elf_section_data (sec); 6000 6001 /* Add reloc setion name to section name section. */ 6002 if (d->rel.hdr 6003 && !_bfd_elf_set_reloc_sh_name (abfd, 6004 d->rel.hdr, 6005 name, FALSE)) 6006 return FALSE; 6007 if (d->rela.hdr 6008 && !_bfd_elf_set_reloc_sh_name (abfd, 6009 d->rela.hdr, 6010 name, TRUE)) 6011 return FALSE; 6012 6013 /* Update section size and contents. */ 6014 shdrp->sh_size = sec->size; 6015 shdrp->contents = sec->contents; 6016 shdrp->bfd_section->contents = NULL; 6017 } 6018 off = _bfd_elf_assign_file_position_for_section (shdrp, 6019 off, 6020 TRUE); 6021 } 6022 } 6023 } 6024 6025 /* Place section name section after DWARF debug sections have been 6026 compressed. */ 6027 _bfd_elf_strtab_finalize (elf_shstrtab (abfd)); 6028 shdrp = &elf_tdata (abfd)->shstrtab_hdr; 6029 shdrp->sh_size = _bfd_elf_strtab_size (elf_shstrtab (abfd)); 6030 off = _bfd_elf_assign_file_position_for_section (shdrp, off, TRUE); 6031 6032 /* Place the section headers. */ 5429 6033 i_ehdrp = elf_elfheader (abfd); 5430 6034 bed = get_elf_backend_data (abfd); … … 5433 6037 off += i_ehdrp->e_shnum * i_ehdrp->e_shentsize; 5434 6038 elf_next_file_pos (abfd) = off; 6039 6040 return TRUE; 5435 6041 } 5436 6042 … … 5455 6061 return FALSE; 5456 6062 5457 _bfd_elf_assign_file_positions_for_relocs (abfd); 6063 if (!_bfd_elf_assign_file_positions_for_non_load (abfd)) 6064 return FALSE; 5458 6065 5459 6066 /* After writing the headers, we need to write the sections too... */ … … 5461 6068 for (count = 1; count < num_sec; count++) 5462 6069 { 6070 i_shdrp[count]->sh_name 6071 = _bfd_elf_strtab_offset (elf_shstrtab (abfd), 6072 i_shdrp[count]->sh_name); 5463 6073 if (bed->elf_backend_section_processing) 5464 6074 (*bed->elf_backend_section_processing) (abfd, i_shdrp[count]); … … 5912 6522 a warning is produced. */ 5913 6523 if (segment->p_type == PT_LOAD) 5914 (*_bfd_error_handler) (_(" %B: warning: Empty loadable segment"5915 " detected, is this intentional ?\n"),6524 (*_bfd_error_handler) (_("\ 6525 %B: warning: Empty loadable segment detected, is this intentional ?"), 5916 6526 ibfd); 5917 6527 … … 5967 6577 first_suggested_lma = TRUE; 5968 6578 5969 for (section = ibfd->sections;6579 for (section = first_section, j = 0; 5970 6580 section != NULL; 5971 6581 section = section->next) 5972 if (section == first_section)5973 break;5974 5975 for (j = 0; section != NULL; section = section->next)5976 6582 { 5977 6583 if (INCLUDE_SECTION_IN_SEGMENT (section, segment, bed)) … … 6526 7132 if (segment->p_type == PT_LOAD 6527 7133 && maxpagesize < segment->p_align) 6528 maxpagesize = segment->p_align; 7134 { 7135 /* PR 17512: file: f17299af. */ 7136 if (segment->p_align > (bfd_vma) 1 << ((sizeof (bfd_vma) * 8) - 2)) 7137 (*_bfd_error_handler) (_("\ 7138 %B: warning: segment alignment of 0x%llx is too large"), 7139 ibfd, (long long) segment->p_align); 7140 else 7141 maxpagesize = segment->p_align; 7142 } 6529 7143 6530 7144 if (maxpagesize != get_elf_backend_data (obfd)->maxpagesize) … … 6546 7160 { 6547 7161 Elf_Internal_Shdr *ihdr, *ohdr; 6548 bfd_boolean final_link = link_info != NULL && !link_info->relocatable; 7162 bfd_boolean final_link = (link_info != NULL 7163 && !bfd_link_relocatable (link_info)); 6549 7164 6550 7165 if (ibfd->xvec->flavour != bfd_target_elf_flavour … … 6583 7198 elf_section_data (osec)->group = elf_section_data (isec)->group; 6584 7199 } 7200 7201 /* If not decompress, preserve SHF_COMPRESSED. */ 7202 if ((ibfd->flags & BFD_DECOMPRESS) == 0) 7203 elf_section_flags (osec) |= (elf_section_flags (isec) 7204 & SHF_COMPRESSED); 6585 7205 } 6586 7206 … … 6761 7381 else if (shndx == elf_shstrtab_sec (ibfd)) 6762 7382 shndx = MAP_SHSTRTAB; 6763 else if ( shndx == elf_symtab_shndx (ibfd))7383 else if (find_section_in_list (shndx, elf_symtab_shndx_list (ibfd))) 6764 7384 shndx = MAP_SYM_SHNDX; 6765 7385 osym->internal_elf_sym.st_shndx = shndx; … … 6773 7393 static bfd_boolean 6774 7394 swap_out_syms (bfd *abfd, 6775 struct bfd_strtab_hash **sttp,7395 struct elf_strtab_hash **sttp, 6776 7396 int relocatable_p) 6777 7397 { … … 6779 7399 int symcount; 6780 7400 asymbol **syms; 6781 struct bfd_strtab_hash *stt;7401 struct elf_strtab_hash *stt; 6782 7402 Elf_Internal_Shdr *symtab_hdr; 6783 7403 Elf_Internal_Shdr *symtab_shndx_hdr; 6784 7404 Elf_Internal_Shdr *symstrtab_hdr; 7405 struct elf_sym_strtab *symstrtab; 6785 7406 bfd_byte *outbound_syms; 6786 7407 bfd_byte *outbound_shndx; 7408 unsigned long outbound_syms_index; 7409 unsigned long outbound_shndx_index; 6787 7410 int idx; 6788 7411 unsigned int num_locals; … … 6794 7417 6795 7418 /* Dump out the symtabs. */ 6796 stt = _bfd_elf_str ingtab_init ();7419 stt = _bfd_elf_strtab_init (); 6797 7420 if (stt == NULL) 6798 7421 return FALSE; … … 6810 7433 symstrtab_hdr->sh_type = SHT_STRTAB; 6811 7434 7435 /* Allocate buffer to swap out the .strtab section. */ 7436 symstrtab = (struct elf_sym_strtab *) bfd_malloc ((symcount + 1) 7437 * sizeof (*symstrtab)); 7438 if (symstrtab == NULL) 7439 { 7440 _bfd_elf_strtab_free (stt); 7441 return FALSE; 7442 } 7443 6812 7444 outbound_syms = (bfd_byte *) bfd_alloc2 (abfd, 1 + symcount, 6813 7445 bed->s->sizeof_sym); 6814 7446 if (outbound_syms == NULL) 6815 7447 { 6816 _bfd_stringtab_free (stt); 7448 error_return: 7449 _bfd_elf_strtab_free (stt); 7450 free (symstrtab); 6817 7451 return FALSE; 6818 7452 } 6819 7453 symtab_hdr->contents = outbound_syms; 7454 outbound_syms_index = 0; 6820 7455 6821 7456 outbound_shndx = NULL; 6822 symtab_shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr; 6823 if (symtab_shndx_hdr->sh_name != 0) 6824 { 6825 amt = (bfd_size_type) (1 + symcount) * sizeof (Elf_External_Sym_Shndx); 6826 outbound_shndx = (bfd_byte *) 6827 bfd_zalloc2 (abfd, 1 + symcount, sizeof (Elf_External_Sym_Shndx)); 6828 if (outbound_shndx == NULL) 6829 { 6830 _bfd_stringtab_free (stt); 6831 return FALSE; 6832 } 6833 6834 symtab_shndx_hdr->contents = outbound_shndx; 6835 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX; 6836 symtab_shndx_hdr->sh_size = amt; 6837 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx); 6838 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx); 7457 outbound_shndx_index = 0; 7458 7459 if (elf_symtab_shndx_list (abfd)) 7460 { 7461 symtab_shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr; 7462 if (symtab_shndx_hdr->sh_name != 0) 7463 { 7464 amt = (bfd_size_type) (1 + symcount) * sizeof (Elf_External_Sym_Shndx); 7465 outbound_shndx = (bfd_byte *) 7466 bfd_zalloc2 (abfd, 1 + symcount, sizeof (Elf_External_Sym_Shndx)); 7467 if (outbound_shndx == NULL) 7468 goto error_return; 7469 7470 symtab_shndx_hdr->contents = outbound_shndx; 7471 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX; 7472 symtab_shndx_hdr->sh_size = amt; 7473 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx); 7474 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx); 7475 } 7476 /* FIXME: What about any other headers in the list ? */ 6839 7477 } 6840 7478 … … 6850 7488 sym.st_shndx = SHN_UNDEF; 6851 7489 sym.st_target_internal = 0; 6852 bed->s->swap_symbol_out (abfd, &sym, outbound_syms, outbound_shndx); 6853 outbound_syms += bed->s->sizeof_sym; 7490 symstrtab[0].sym = sym; 7491 symstrtab[0].dest_index = outbound_syms_index; 7492 symstrtab[0].destshndx_index = outbound_shndx_index; 7493 outbound_syms_index++; 6854 7494 if (outbound_shndx != NULL) 6855 outbound_shndx += sizeof (Elf_External_Sym_Shndx);7495 outbound_shndx_index++; 6856 7496 } 6857 7497 … … 6861 7501 6862 7502 syms = bfd_get_outsymbols (abfd); 6863 for (idx = 0; idx < symcount; idx++)7503 for (idx = 0; idx < symcount;) 6864 7504 { 6865 7505 Elf_Internal_Sym sym; … … 6873 7513 { 6874 7514 /* Local section symbols have no name. */ 6875 sym.st_name = 0;7515 sym.st_name = (unsigned long) -1; 6876 7516 } 6877 7517 else 6878 7518 { 6879 sym.st_name = (unsigned long) _bfd_stringtab_add (stt, 6880 syms[idx]->name, 6881 TRUE, FALSE); 7519 /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize 7520 to get the final offset for st_name. */ 7521 sym.st_name 7522 = (unsigned long) _bfd_elf_strtab_add (stt, syms[idx]->name, 7523 FALSE); 6882 7524 if (sym.st_name == (unsigned long) -1) 6883 { 6884 _bfd_stringtab_free (stt); 6885 return FALSE; 6886 } 7525 goto error_return; 6887 7526 } 6888 7527 … … 6944 7583 break; 6945 7584 case MAP_SYM_SHNDX: 6946 shndx = elf_symtab_shndx (abfd); 7585 if (elf_symtab_shndx_list (abfd)) 7586 shndx = elf_symtab_shndx_list (abfd)->ndx; 6947 7587 break; 6948 7588 default: … … 6974 7614 sec->name); 6975 7615 bfd_set_error (bfd_error_invalid_operation); 6976 _bfd_stringtab_free (stt); 6977 return FALSE; 7616 goto error_return; 6978 7617 } 6979 7618 … … 7019 7658 else if (bfd_is_com_section (syms[idx]->section)) 7020 7659 { 7021 #ifdef USE_STT_COMMON 7022 if (type == STT_OBJECT) 7023 sym.st_info = ELF_ST_INFO (STB_GLOBAL, STT_COMMON); 7024 else 7025 #endif 7026 sym.st_info = ELF_ST_INFO (STB_GLOBAL, type); 7660 if (type != STT_TLS) 7661 { 7662 if ((abfd->flags & BFD_CONVERT_ELF_COMMON)) 7663 type = ((abfd->flags & BFD_USE_ELF_STT_COMMON) 7664 ? STT_COMMON : STT_OBJECT); 7665 else 7666 type = ((flags & BSF_ELF_COMMON) != 0 7667 ? STT_COMMON : STT_OBJECT); 7668 } 7669 sym.st_info = ELF_ST_INFO (STB_GLOBAL, type); 7027 7670 } 7028 7671 else if (bfd_is_und_section (syms[idx]->section)) … … 7061 7704 } 7062 7705 7063 bed->s->swap_symbol_out (abfd, &sym, outbound_syms, outbound_shndx); 7064 outbound_syms += bed->s->sizeof_sym; 7706 idx++; 7707 symstrtab[idx].sym = sym; 7708 symstrtab[idx].dest_index = outbound_syms_index; 7709 symstrtab[idx].destshndx_index = outbound_shndx_index; 7710 7711 outbound_syms_index++; 7065 7712 if (outbound_shndx != NULL) 7066 outbound_shndx += sizeof (Elf_External_Sym_Shndx); 7067 } 7713 outbound_shndx_index++; 7714 } 7715 7716 /* Finalize the .strtab section. */ 7717 _bfd_elf_strtab_finalize (stt); 7718 7719 /* Swap out the .strtab section. */ 7720 for (idx = 0; idx <= symcount; idx++) 7721 { 7722 struct elf_sym_strtab *elfsym = &symstrtab[idx]; 7723 if (elfsym->sym.st_name == (unsigned long) -1) 7724 elfsym->sym.st_name = 0; 7725 else 7726 elfsym->sym.st_name = _bfd_elf_strtab_offset (stt, 7727 elfsym->sym.st_name); 7728 bed->s->swap_symbol_out (abfd, &elfsym->sym, 7729 (outbound_syms 7730 + (elfsym->dest_index 7731 * bed->s->sizeof_sym)), 7732 (outbound_shndx 7733 + (elfsym->destshndx_index 7734 * sizeof (Elf_External_Sym_Shndx)))); 7735 } 7736 free (symstrtab); 7068 7737 7069 7738 *sttp = stt; 7070 symstrtab_hdr->sh_size = _bfd_ stringtab_size (stt);7739 symstrtab_hdr->sh_size = _bfd_elf_strtab_size (stt); 7071 7740 symstrtab_hdr->sh_type = SHT_STRTAB; 7072 7073 symstrtab_hdr->sh_flags = 0; 7741 symstrtab_hdr->sh_flags = bed->elf_strtab_flags; 7074 7742 symstrtab_hdr->sh_addr = 0; 7075 7743 symstrtab_hdr->sh_entsize = 0; … … 7273 7941 hdr = &elf_tdata (abfd)->dynverref_hdr; 7274 7942 7275 if (hdr->sh_info) 7276 elf_tdata (abfd)->verref = (Elf_Internal_Verneed *) 7277 bfd_zalloc2 (abfd, hdr->sh_info, sizeof (Elf_Internal_Verneed)); 7278 else 7279 elf_tdata (abfd)->verref = NULL; 7280 7281 if (elf_tdata (abfd)->verref == NULL) 7282 goto error_return; 7283 7284 elf_tdata (abfd)->cverrefs = hdr->sh_info; 7285 7286 contents = (bfd_byte *) bfd_malloc (hdr->sh_size); 7287 if (contents == NULL) 7288 { 7943 if (hdr->sh_info == 0 || hdr->sh_size < sizeof (Elf_External_Verneed)) 7944 { 7945 error_return_bad_verref: 7946 (*_bfd_error_handler) 7947 (_("%B: .gnu.version_r invalid entry"), abfd); 7948 bfd_set_error (bfd_error_bad_value); 7289 7949 error_return_verref: 7290 7950 elf_tdata (abfd)->verref = NULL; … … 7292 7952 goto error_return; 7293 7953 } 7954 7955 contents = (bfd_byte *) bfd_malloc (hdr->sh_size); 7956 if (contents == NULL) 7957 goto error_return_verref; 7958 7294 7959 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0 7295 7960 || bfd_bread (contents, hdr->sh_size, abfd) != hdr->sh_size) 7296 7961 goto error_return_verref; 7297 7962 7298 if (hdr->sh_info && hdr->sh_size < sizeof (Elf_External_Verneed)) 7963 elf_tdata (abfd)->verref = (Elf_Internal_Verneed *) 7964 bfd_zalloc2 (abfd, hdr->sh_info, sizeof (Elf_Internal_Verneed)); 7965 7966 if (elf_tdata (abfd)->verref == NULL) 7299 7967 goto error_return_verref; 7300 7968 … … 7318 7986 iverneed->vn_file); 7319 7987 if (iverneed->vn_filename == NULL) 7320 goto error_return_ verref;7988 goto error_return_bad_verref; 7321 7989 7322 7990 if (iverneed->vn_cnt == 0) … … 7333 8001 if (iverneed->vn_aux 7334 8002 > (size_t) (contents_end - (bfd_byte *) everneed)) 7335 goto error_return_ verref;8003 goto error_return_bad_verref; 7336 8004 7337 8005 evernaux = ((Elf_External_Vernaux *) … … 7346 8014 ivernaux->vna_name); 7347 8015 if (ivernaux->vna_nodename == NULL) 7348 goto error_return_verref; 7349 8016 goto error_return_bad_verref; 8017 8018 if (ivernaux->vna_other > freeidx) 8019 freeidx = ivernaux->vna_other; 8020 8021 ivernaux->vna_nextptr = NULL; 8022 if (ivernaux->vna_next == 0) 8023 { 8024 iverneed->vn_cnt = j + 1; 8025 break; 8026 } 7350 8027 if (j + 1 < iverneed->vn_cnt) 7351 8028 ivernaux->vna_nextptr = ivernaux + 1; 7352 else7353 ivernaux->vna_nextptr = NULL;7354 8029 7355 8030 if (ivernaux->vna_next 7356 8031 > (size_t) (contents_end - (bfd_byte *) evernaux)) 7357 goto error_return_ verref;8032 goto error_return_bad_verref; 7358 8033 7359 8034 evernaux = ((Elf_External_Vernaux *) 7360 8035 ((bfd_byte *) evernaux + ivernaux->vna_next)); 7361 7362 if (ivernaux->vna_other > freeidx)7363 freeidx = ivernaux->vna_other;7364 8036 } 7365 8037 8038 iverneed->vn_nextref = NULL; 8039 if (iverneed->vn_next == 0) 8040 break; 7366 8041 if (i + 1 < hdr->sh_info) 7367 8042 iverneed->vn_nextref = iverneed + 1; 7368 else7369 iverneed->vn_nextref = NULL;7370 8043 7371 8044 if (iverneed->vn_next 7372 8045 > (size_t) (contents_end - (bfd_byte *) everneed)) 7373 goto error_return_ verref;8046 goto error_return_bad_verref; 7374 8047 7375 8048 everneed = ((Elf_External_Verneed *) 7376 8049 ((bfd_byte *) everneed + iverneed->vn_next)); 7377 8050 } 8051 elf_tdata (abfd)->cverrefs = i; 7378 8052 7379 8053 free (contents); … … 7394 8068 hdr = &elf_tdata (abfd)->dynverdef_hdr; 7395 8069 8070 if (hdr->sh_info == 0 || hdr->sh_size < sizeof (Elf_External_Verdef)) 8071 { 8072 error_return_bad_verdef: 8073 (*_bfd_error_handler) 8074 (_("%B: .gnu.version_d invalid entry"), abfd); 8075 bfd_set_error (bfd_error_bad_value); 8076 error_return_verdef: 8077 elf_tdata (abfd)->verdef = NULL; 8078 elf_tdata (abfd)->cverdefs = 0; 8079 goto error_return; 8080 } 8081 7396 8082 contents = (bfd_byte *) bfd_malloc (hdr->sh_size); 7397 8083 if (contents == NULL) 7398 goto error_return ;8084 goto error_return_verdef; 7399 8085 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0 7400 8086 || bfd_bread (contents, hdr->sh_size, abfd) != hdr->sh_size) 7401 goto error_return; 7402 7403 if (hdr->sh_info && hdr->sh_size < sizeof (Elf_External_Verdef)) 7404 goto error_return; 8087 goto error_return_verdef; 7405 8088 7406 8089 BFD_ASSERT (sizeof (Elf_External_Verdef) … … 7420 8103 _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem); 7421 8104 8105 if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) == 0) 8106 goto error_return_bad_verdef; 7422 8107 if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) > maxidx) 7423 8108 maxidx = iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION); 7424 8109 8110 if (iverdefmem.vd_next == 0) 8111 break; 8112 7425 8113 if (iverdefmem.vd_next 7426 8114 > (size_t) (contents_end_def - (bfd_byte *) everdef)) 7427 goto error_return ;8115 goto error_return_bad_verdef; 7428 8116 7429 8117 everdef = ((Elf_External_Verdef *) … … 7438 8126 freeidx = ++maxidx; 7439 8127 } 7440 if (maxidx) 7441 elf_tdata (abfd)->verdef = (Elf_Internal_Verdef *) 7442 bfd_zalloc2 (abfd, maxidx, sizeof (Elf_Internal_Verdef)); 7443 else 7444 elf_tdata (abfd)->verdef = NULL; 7445 8128 8129 elf_tdata (abfd)->verdef = (Elf_Internal_Verdef *) 8130 bfd_zalloc2 (abfd, maxidx, sizeof (Elf_Internal_Verdef)); 7446 8131 if (elf_tdata (abfd)->verdef == NULL) 7447 goto error_return ;8132 goto error_return_verdef; 7448 8133 7449 8134 elf_tdata (abfd)->cverdefs = maxidx; … … 7460 8145 7461 8146 if ((iverdefmem.vd_ndx & VERSYM_VERSION) == 0) 7462 { 7463 error_return_verdef: 7464 elf_tdata (abfd)->verdef = NULL; 7465 elf_tdata (abfd)->cverdefs = 0; 7466 goto error_return; 7467 } 8147 goto error_return_bad_verdef; 7468 8148 7469 8149 iverdef = &iverdefarr[(iverdefmem.vd_ndx & VERSYM_VERSION) - 1]; 7470 memcpy (iverdef, &iverdefmem, sizeof (Elf_Internal_Verdef));8150 memcpy (iverdef, &iverdefmem, offsetof (Elf_Internal_Verdef, vd_bfd)); 7471 8151 7472 8152 iverdef->vd_bfd = abfd; … … 7485 8165 if (iverdef->vd_aux 7486 8166 > (size_t) (contents_end_aux - (bfd_byte *) everdef)) 7487 goto error_return_ verdef;8167 goto error_return_bad_verdef; 7488 8168 7489 8169 everdaux = ((Elf_External_Verdaux *) … … 7498 8178 iverdaux->vda_name); 7499 8179 if (iverdaux->vda_nodename == NULL) 7500 goto error_return_verdef; 7501 8180 goto error_return_bad_verdef; 8181 8182 iverdaux->vda_nextptr = NULL; 8183 if (iverdaux->vda_next == 0) 8184 { 8185 iverdef->vd_cnt = j + 1; 8186 break; 8187 } 7502 8188 if (j + 1 < iverdef->vd_cnt) 7503 8189 iverdaux->vda_nextptr = iverdaux + 1; 7504 else7505 iverdaux->vda_nextptr = NULL;7506 8190 7507 8191 if (iverdaux->vda_next 7508 8192 > (size_t) (contents_end_aux - (bfd_byte *) everdaux)) 7509 goto error_return_ verdef;8193 goto error_return_bad_verdef; 7510 8194 7511 8195 everdaux = ((Elf_External_Verdaux *) … … 7513 8197 } 7514 8198 8199 iverdef->vd_nodename = NULL; 7515 8200 if (iverdef->vd_cnt) 7516 8201 iverdef->vd_nodename = iverdef->vd_auxptr->vda_nodename; 7517 8202 8203 iverdef->vd_nextdef = NULL; 8204 if (iverdef->vd_next == 0) 8205 break; 7518 8206 if ((size_t) (iverdef - iverdefarr) + 1 < maxidx) 7519 8207 iverdef->vd_nextdef = iverdef + 1; 7520 else7521 iverdef->vd_nextdef = NULL;7522 8208 7523 8209 everdef = ((Elf_External_Verdef *) … … 7562 8248 goto error_return_verdef; 7563 8249 iverdef->vd_nextdef = NULL; 7564 iverdef->vd_auxptr = ( struct elf_internal_verdaux *)7565 bfd_alloc (abfd, sizeof (Elf_Internal_Verdaux));8250 iverdef->vd_auxptr = ((struct elf_internal_verdaux *) 8251 bfd_zalloc (abfd, sizeof (Elf_Internal_Verdaux))); 7566 8252 if (iverdef->vd_auxptr == NULL) 7567 8253 goto error_return_verdef; … … 7569 8255 iverdaux = iverdef->vd_auxptr; 7570 8256 iverdaux->vda_nodename = iverdef->vd_nodename; 7571 iverdaux->vda_nextptr = NULL;7572 8257 } 7573 8258 … … 7627 8312 return TRUE; 7628 8313 8314 /* Treat assembler generated fake symbols, dollar local labels and 8315 forward-backward labels (aka local labels) as locals. 8316 These labels have the form: 8317 8318 L0^A.* (fake symbols) 8319 8320 [.]?L[0123456789]+{^A|^B}[0123456789]* (local labels) 8321 8322 Versions which start with .L will have already been matched above, 8323 so we only need to match the rest. */ 8324 if (name[0] == 'L' && ISDIGIT (name[1])) 8325 { 8326 bfd_boolean ret = FALSE; 8327 const char * p; 8328 char c; 8329 8330 for (p = name + 2; (c = *p); p++) 8331 { 8332 if (c == 1 || c == 2) 8333 { 8334 if (c == 1 && p == name + 2) 8335 /* A fake symbol. */ 8336 return TRUE; 8337 8338 /* FIXME: We are being paranoid here and treating symbols like 8339 L0^Bfoo as if there were non-local, on the grounds that the 8340 assembler will never generate them. But can any symbol 8341 containing an ASCII value in the range 1-31 ever be anything 8342 other than some kind of local ? */ 8343 ret = TRUE; 8344 } 8345 8346 if (! ISDIGIT (c)) 8347 { 8348 ret = FALSE; 8349 break; 8350 } 8351 } 8352 return ret; 8353 } 8354 7629 8355 return FALSE; 7630 8356 } … … 7651 8377 7652 8378 return bfd_default_set_arch_mach (abfd, arch, machine); 7653 }7654 7655 /* Find the function to a particular section and offset,7656 for error reporting. */7657 7658 static bfd_boolean7659 elf_find_function (bfd *abfd,7660 asymbol **symbols,7661 asection *section,7662 bfd_vma offset,7663 const char **filename_ptr,7664 const char **functionname_ptr)7665 {7666 struct elf_find_function_cache7667 {7668 asection *last_section;7669 asymbol *func;7670 const char *filename;7671 bfd_size_type func_size;7672 } *cache;7673 7674 if (symbols == NULL)7675 return FALSE;7676 7677 cache = elf_tdata (abfd)->elf_find_function_cache;7678 if (cache == NULL)7679 {7680 cache = bfd_zalloc (abfd, sizeof (*cache));7681 elf_tdata (abfd)->elf_find_function_cache = cache;7682 if (cache == NULL)7683 return FALSE;7684 }7685 if (cache->last_section != section7686 || cache->func == NULL7687 || offset < cache->func->value7688 || offset >= cache->func->value + cache->func_size)7689 {7690 asymbol *file;7691 bfd_vma low_func;7692 asymbol **p;7693 /* ??? Given multiple file symbols, it is impossible to reliably7694 choose the right file name for global symbols. File symbols are7695 local symbols, and thus all file symbols must sort before any7696 global symbols. The ELF spec may be interpreted to say that a7697 file symbol must sort before other local symbols, but currently7698 ld -r doesn't do this. So, for ld -r output, it is possible to7699 make a better choice of file name for local symbols by ignoring7700 file symbols appearing after a given local symbol. */7701 enum { nothing_seen, symbol_seen, file_after_symbol_seen } state;7702 const struct elf_backend_data *bed = get_elf_backend_data (abfd);7703 7704 file = NULL;7705 low_func = 0;7706 state = nothing_seen;7707 cache->filename = NULL;7708 cache->func = NULL;7709 cache->func_size = 0;7710 cache->last_section = section;7711 7712 for (p = symbols; *p != NULL; p++)7713 {7714 asymbol *sym = *p;7715 bfd_vma code_off;7716 bfd_size_type size;7717 7718 if ((sym->flags & BSF_FILE) != 0)7719 {7720 file = sym;7721 if (state == symbol_seen)7722 state = file_after_symbol_seen;7723 continue;7724 }7725 7726 size = bed->maybe_function_sym (sym, section, &code_off);7727 if (size != 07728 && code_off <= offset7729 && (code_off > low_func7730 || (code_off == low_func7731 && size > cache->func_size)))7732 {7733 cache->func = sym;7734 cache->func_size = size;7735 cache->filename = NULL;7736 low_func = code_off;7737 if (file != NULL7738 && ((sym->flags & BSF_LOCAL) != 07739 || state != file_after_symbol_seen))7740 cache->filename = bfd_asymbol_name (file);7741 }7742 if (state == nothing_seen)7743 state = symbol_seen;7744 }7745 }7746 7747 if (cache->func == NULL)7748 return FALSE;7749 7750 if (filename_ptr)7751 *filename_ptr = cache->filename;7752 if (functionname_ptr)7753 *functionname_ptr = bfd_asymbol_name (cache->func);7754 7755 return TRUE;7756 8379 } 7757 8380 … … 7775 8398 line_ptr, discriminator_ptr, 7776 8399 dwarf_debug_sections, 0, 7777 &elf_tdata (abfd)->dwarf2_find_line_info)) 8400 &elf_tdata (abfd)->dwarf2_find_line_info) 8401 || _bfd_dwarf1_find_nearest_line (abfd, symbols, section, offset, 8402 filename_ptr, functionname_ptr, 8403 line_ptr)) 7778 8404 { 7779 8405 if (!*functionname_ptr) 7780 elf_find_function (abfd, symbols, section, offset, 7781 *filename_ptr ? NULL : filename_ptr, 7782 functionname_ptr); 7783 7784 return TRUE; 7785 } 7786 7787 if (_bfd_dwarf1_find_nearest_line (abfd, symbols, section, offset, 7788 filename_ptr, functionname_ptr, line_ptr)) 7789 { 7790 if (!*functionname_ptr) 7791 elf_find_function (abfd, symbols, section, offset, 7792 *filename_ptr ? NULL : filename_ptr, 7793 functionname_ptr); 7794 8406 _bfd_elf_find_function (abfd, symbols, section, offset, 8407 *filename_ptr ? NULL : filename_ptr, 8408 functionname_ptr); 7795 8409 return TRUE; 7796 8410 } … … 7807 8421 return FALSE; 7808 8422 7809 if (! elf_find_function (abfd, symbols, section, offset,7810 8423 if (! _bfd_elf_find_function (abfd, symbols, section, offset, 8424 filename_ptr, functionname_ptr)) 7811 8425 return FALSE; 7812 8426 … … 7852 8466 int ret = bed->s->sizeof_ehdr; 7853 8467 7854 if (! info->relocatable)8468 if (!bfd_link_relocatable (info)) 7855 8469 { 7856 8470 bfd_size_type phdr_size = elf_program_header_size (abfd); … … 7889 8503 return FALSE; 7890 8504 8505 if (!count) 8506 return TRUE; 8507 7891 8508 hdr = &elf_section_data (section)->this_hdr; 8509 if (hdr->sh_offset == (file_ptr) -1) 8510 { 8511 /* We must compress this section. Write output to the buffer. */ 8512 unsigned char *contents = hdr->contents; 8513 if ((offset + count) > hdr->sh_size 8514 || (section->flags & SEC_ELF_COMPRESS) == 0 8515 || contents == NULL) 8516 abort (); 8517 memcpy (contents + offset, location, count); 8518 return TRUE; 8519 } 7892 8520 pos = hdr->sh_offset + offset; 7893 8521 if (bfd_seek (abfd, pos, SEEK_SET) != 0 … … 8310 8938 8311 8939 static bfd_boolean 8940 elfcore_grok_s390_vxrs_low (bfd *abfd, Elf_Internal_Note *note) 8941 { 8942 return elfcore_make_note_pseudosection (abfd, ".reg-s390-vxrs-low", note); 8943 } 8944 8945 static bfd_boolean 8946 elfcore_grok_s390_vxrs_high (bfd *abfd, Elf_Internal_Note *note) 8947 { 8948 return elfcore_make_note_pseudosection (abfd, ".reg-s390-vxrs-high", note); 8949 } 8950 8951 static bfd_boolean 8312 8952 elfcore_grok_arm_vfp (bfd *abfd, Elf_Internal_Note *note) 8313 8953 { … … 8772 9412 else 8773 9413 return TRUE; 9414 9415 case NT_S390_VXRS_LOW: 9416 if (note->namesz == 6 9417 && strcmp (note->namedata, "LINUX") == 0) 9418 return elfcore_grok_s390_vxrs_low (abfd, note); 9419 else 9420 return TRUE; 9421 9422 case NT_S390_VXRS_HIGH: 9423 if (note->namesz == 6 9424 && strcmp (note->namedata, "LINUX") == 0) 9425 return elfcore_grok_s390_vxrs_high (abfd, note); 9426 else 9427 return TRUE; 8774 9428 8775 9429 case NT_ARM_VFP: … … 8833 9487 return elfcore_make_note_pseudosection (abfd, ".note.linuxcore.siginfo", 8834 9488 note); 9489 8835 9490 } 8836 9491 } … … 8839 9494 elfobj_grok_gnu_build_id (bfd *abfd, Elf_Internal_Note *note) 8840 9495 { 8841 struct elf_obj_tdata *t;9496 struct bfd_build_id* build_id; 8842 9497 8843 9498 if (note->descsz == 0) 8844 9499 return FALSE; 8845 9500 8846 t = elf_tdata (abfd); 8847 t->build_id = bfd_alloc (abfd, sizeof (*t->build_id) - 1 + note->descsz); 8848 if (t->build_id == NULL) 9501 build_id = bfd_alloc (abfd, sizeof (struct bfd_build_id) - 1 + note->descsz); 9502 if (build_id == NULL) 8849 9503 return FALSE; 8850 9504 8851 t->build_id->size = note->descsz; 8852 memcpy (t->build_id->data, note->descdata, note->descsz); 9505 build_id->size = note->descsz; 9506 memcpy (build_id->data, note->descdata, note->descsz); 9507 abfd->build_id = build_id; 8853 9508 8854 9509 return TRUE; … … 8891 9546 case NT_STAPSDT: 8892 9547 return elfobj_grok_stapsdt_note_1 (abfd, note); 9548 9549 default: 9550 return TRUE; 9551 } 9552 } 9553 9554 static bfd_boolean 9555 elfcore_grok_freebsd_psinfo (bfd *abfd, Elf_Internal_Note *note) 9556 { 9557 size_t offset; 9558 9559 /* Check for version 1 in pr_version. */ 9560 if (bfd_h_get_32 (abfd, (bfd_byte *) note->descdata) != 1) 9561 return FALSE; 9562 offset = 4; 9563 9564 /* Skip over pr_psinfosz. */ 9565 switch (abfd->arch_info->bits_per_word) 9566 { 9567 case 32: 9568 offset += 4; 9569 break; 9570 9571 case 64: 9572 offset += 4; /* Padding before pr_psinfosz. */ 9573 offset += 8; 9574 break; 9575 9576 default: 9577 return FALSE; 9578 } 9579 9580 /* pr_fname is PRFNAMESZ (16) + 1 bytes in size. */ 9581 elf_tdata (abfd)->core->program 9582 = _bfd_elfcore_strndup (abfd, note->descdata + offset, 17); 9583 offset += 17; 9584 9585 /* pr_psargs is PRARGSZ (80) + 1 bytes in size. */ 9586 elf_tdata (abfd)->core->command 9587 = _bfd_elfcore_strndup (abfd, note->descdata + offset, 81); 9588 9589 return TRUE; 9590 } 9591 9592 static bfd_boolean 9593 elfcore_grok_freebsd_prstatus (bfd *abfd, Elf_Internal_Note *note) 9594 { 9595 size_t offset; 9596 size_t size; 9597 9598 /* Check for version 1 in pr_version. */ 9599 if (bfd_h_get_32 (abfd, (bfd_byte *) note->descdata) != 1) 9600 return FALSE; 9601 offset = 4; 9602 9603 /* Skip over pr_statussz. */ 9604 switch (abfd->arch_info->bits_per_word) 9605 { 9606 case 32: 9607 offset += 4; 9608 break; 9609 9610 case 64: 9611 offset += 4; /* Padding before pr_statussz. */ 9612 offset += 8; 9613 break; 9614 9615 default: 9616 return FALSE; 9617 } 9618 9619 /* Extract size of pr_reg from pr_gregsetsz. */ 9620 if (abfd->arch_info->bits_per_word == 32) 9621 size = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset); 9622 else 9623 size = bfd_h_get_64 (abfd, (bfd_byte *) note->descdata + offset); 9624 9625 /* Skip over pr_gregsetsz and pr_fpregsetsz. */ 9626 offset += (abfd->arch_info->bits_per_word / 8) * 2; 9627 9628 /* Skip over pr_osreldate. */ 9629 offset += 4; 9630 9631 /* Read signal from pr_cursig. */ 9632 if (elf_tdata (abfd)->core->signal == 0) 9633 elf_tdata (abfd)->core->signal 9634 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset); 9635 offset += 4; 9636 9637 /* Read TID from pr_pid. */ 9638 elf_tdata (abfd)->core->lwpid 9639 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset); 9640 offset += 4; 9641 9642 /* Padding before pr_reg. */ 9643 if (abfd->arch_info->bits_per_word == 64) 9644 offset += 4; 9645 9646 /* Make a ".reg/999" section and a ".reg" section. */ 9647 return _bfd_elfcore_make_pseudosection (abfd, ".reg", 9648 size, note->descpos + offset); 9649 } 9650 9651 static bfd_boolean 9652 elfcore_grok_freebsd_note (bfd *abfd, Elf_Internal_Note *note) 9653 { 9654 switch (note->type) 9655 { 9656 case NT_PRSTATUS: 9657 return elfcore_grok_freebsd_prstatus (abfd, note); 9658 9659 case NT_FPREGSET: 9660 return elfcore_grok_prfpreg (abfd, note); 9661 9662 case NT_PRPSINFO: 9663 return elfcore_grok_freebsd_psinfo (abfd, note); 9664 9665 case NT_FREEBSD_THRMISC: 9666 if (note->namesz == 8) 9667 return elfcore_make_note_pseudosection (abfd, ".thrmisc", note); 9668 else 9669 return TRUE; 9670 9671 case NT_FREEBSD_PROCSTAT_AUXV: 9672 { 9673 asection *sect = bfd_make_section_anyway_with_flags (abfd, ".auxv", 9674 SEC_HAS_CONTENTS); 9675 9676 if (sect == NULL) 9677 return FALSE; 9678 sect->size = note->descsz - 4; 9679 sect->filepos = note->descpos + 4; 9680 sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32; 9681 9682 return TRUE; 9683 } 9684 9685 case NT_X86_XSTATE: 9686 if (note->namesz == 8) 9687 return elfcore_grok_xstatereg (abfd, note); 9688 else 9689 return TRUE; 8893 9690 8894 9691 default: … … 9330 10127 struct elf_external_linux_prpsinfo32 data; 9331 10128 9332 memset (&data, 0, sizeof (data)); 9333 LINUX_PRPSINFO32_SWAP_FIELDS (abfd, prpsinfo, data); 9334 10129 swap_linux_prpsinfo32_out (abfd, prpsinfo, &data); 9335 10130 return elfcore_write_note (abfd, buf, bufsiz, "CORE", NT_PRPSINFO, 9336 10131 &data, sizeof (data)); … … 9344 10139 struct elf_external_linux_prpsinfo64 data; 9345 10140 9346 memset (&data, 0, sizeof (data)); 9347 LINUX_PRPSINFO64_SWAP_FIELDS (abfd, prpsinfo, data); 9348 10141 swap_linux_prpsinfo64_out (abfd, prpsinfo, &data); 9349 10142 return elfcore_write_note (abfd, buf, bufsiz, 9350 10143 "CORE", NT_PRPSINFO, &data, sizeof (data)); … … 9498 10291 const void *xfpregs, int size) 9499 10292 { 9500 char *note_name = "LINUX"; 10293 char *note_name; 10294 if (get_elf_backend_data (abfd)->elf_osabi == ELFOSABI_FREEBSD) 10295 note_name = "FreeBSD"; 10296 else 10297 note_name = "LINUX"; 9501 10298 return elfcore_write_note (abfd, buf, bufsiz, 9502 10299 note_name, NT_X86_XSTATE, xfpregs, size); … … 9636 10433 return elfcore_write_note (abfd, buf, bufsiz, 9637 10434 note_name, NT_S390_TDB, s390_tdb, size); 10435 } 10436 10437 char * 10438 elfcore_write_s390_vxrs_low (bfd *abfd, 10439 char *buf, 10440 int *bufsiz, 10441 const void *s390_vxrs_low, 10442 int size) 10443 { 10444 char *note_name = "LINUX"; 10445 return elfcore_write_note (abfd, buf, bufsiz, 10446 note_name, NT_S390_VXRS_LOW, s390_vxrs_low, size); 10447 } 10448 10449 char * 10450 elfcore_write_s390_vxrs_high (bfd *abfd, 10451 char *buf, 10452 int *bufsiz, 10453 const void *s390_vxrs_high, 10454 int size) 10455 { 10456 char *note_name = "LINUX"; 10457 return elfcore_write_note (abfd, buf, bufsiz, 10458 note_name, NT_S390_VXRS_HIGH, 10459 s390_vxrs_high, size); 9638 10460 } 9639 10461 … … 9722 10544 if (strcmp (section, ".reg-s390-tdb") == 0) 9723 10545 return elfcore_write_s390_tdb (abfd, buf, bufsiz, data, size); 10546 if (strcmp (section, ".reg-s390-vxrs-low") == 0) 10547 return elfcore_write_s390_vxrs_low (abfd, buf, bufsiz, data, size); 10548 if (strcmp (section, ".reg-s390-vxrs-high") == 0) 10549 return elfcore_write_s390_vxrs_high (abfd, buf, bufsiz, data, size); 9724 10550 if (strcmp (section, ".reg-arm-vfp") == 0) 9725 10551 return elfcore_write_arm_vfp (abfd, buf, bufsiz, data, size); … … 9769 10595 9770 10596 case bfd_core: 9771 if (CONST_STRNEQ (in.namedata, "NetBSD-CORE")) 10597 { 10598 #define GROKER_ELEMENT(S,F) {S, sizeof (S) - 1, F} 10599 struct 9772 10600 { 9773 if (! elfcore_grok_netbsd_note (abfd, &in)) 9774 return FALSE; 10601 const char * string; 10602 size_t len; 10603 bfd_boolean (* func)(bfd *, Elf_Internal_Note *); 9775 10604 } 9776 else if (CONST_STRNEQ (in.namedata, "OpenBSD"))10605 grokers[] = 9777 10606 { 9778 if (! elfcore_grok_openbsd_note (abfd, &in)) 9779 return FALSE; 9780 } 9781 else if (CONST_STRNEQ (in.namedata, "QNX")) 9782 { 9783 if (! elfcore_grok_nto_note (abfd, &in)) 9784 return FALSE; 9785 } 9786 else if (CONST_STRNEQ (in.namedata, "SPU/")) 9787 { 9788 if (! elfcore_grok_spu_note (abfd, &in)) 9789 return FALSE; 9790 } 9791 else 9792 { 9793 if (! elfcore_grok_note (abfd, &in)) 9794 return FALSE; 9795 } 9796 break; 10607 GROKER_ELEMENT ("", elfcore_grok_note), 10608 GROKER_ELEMENT ("FreeBSD", elfcore_grok_freebsd_note), 10609 GROKER_ELEMENT ("NetBSD-CORE", elfcore_grok_netbsd_note), 10610 GROKER_ELEMENT ( "OpenBSD", elfcore_grok_openbsd_note), 10611 GROKER_ELEMENT ("QNX", elfcore_grok_nto_note), 10612 GROKER_ELEMENT ("SPU/", elfcore_grok_spu_note) 10613 }; 10614 #undef GROKER_ELEMENT 10615 int i; 10616 10617 for (i = ARRAY_SIZE (grokers); i--;) 10618 { 10619 if (in.namesz >= grokers[i].len 10620 && strncmp (in.namedata, grokers[i].string, 10621 grokers[i].len) == 0) 10622 { 10623 if (! grokers[i].func (abfd, & in)) 10624 return FALSE; 10625 break; 10626 } 10627 } 10628 break; 10629 } 9797 10630 9798 10631 case bfd_object: … … 9828 10661 return FALSE; 9829 10662 9830 buf = (char *) bfd_malloc (size );10663 buf = (char *) bfd_malloc (size + 1); 9831 10664 if (buf == NULL) 9832 10665 return FALSE; 10666 10667 /* PR 17512: file: ec08f814 10668 0-termintate the buffer so that string searches will not overflow. */ 10669 buf[size] = 0; 9833 10670 9834 10671 if (bfd_bread (buf, size, abfd) != size … … 9952 10789 } 9953 10790 10791 /* Adjust an address within a section. Given OFFSET within SEC, return 10792 the new offset within the section, based upon changes made to the 10793 section. Returns -1 if the offset is now invalid. 10794 The offset (in abnd out) is in target sized bytes, however big a 10795 byte may be. */ 10796 9954 10797 bfd_vma 9955 10798 _bfd_elf_section_offset (bfd *abfd, … … 9965 10808 case SEC_INFO_TYPE_EH_FRAME: 9966 10809 return _bfd_elf_eh_frame_section_offset (abfd, info, sec, offset); 10810 9967 10811 default: 9968 10812 if ((sec->flags & SEC_ELF_REVERSE_COPY) != 0) 9969 10813 { 10814 /* Reverse the offset. */ 9970 10815 const struct elf_backend_data *bed = get_elf_backend_data (abfd); 9971 10816 bfd_size_type address_size = bed->s->arch_size / 8; 9972 offset = sec->size - offset - address_size; 10817 10818 /* address_size and sec->size are in octets. Convert 10819 to bytes before subtracting the original offset. */ 10820 offset = (sec->size - address_size) / bfd_octets_per_byte (abfd) - offset; 9973 10821 } 9974 10822 return offset;
Note:
See TracChangeset
for help on using the changeset viewer.