1/* 2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved. 3 * 4 * @APPLE_LICENSE_HEADER_START@ 5 * 6 * This file contains Original Code and/or Modifications of Original Code 7 * as defined in and that are subject to the Apple Public Source License 8 * Version 2.0 (the 'License'). You may not use this file except in 9 * compliance with the License. Please obtain a copy of the License at 10 * http://www.opensource.apple.com/apsl/ and read it before using this 11 * file. 12 * 13 * The Original Code and all software distributed under the License are 14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER 15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, 16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, 17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. 18 * Please see the License for the specific language governing rights and 19 * limitations under the License. 20 * 21 * @APPLE_LICENSE_HEADER_END@ 22 */ 23#ifdef SHLIB 24#include "shlib.h" 25#endif /* SHLIB */ 26/* 27 * This file contains the routines that deal with literal pointers sections. 28 * A literal pointer must point to something in another literal section. And 29 * since it is a pointer and is being relocated it must have exactly one 30 * relocation entry for each pointer. Also that relocation entry must have an 31 * r_length of 2 (long, since each pointer is the size of a long) and must have 32 * r_pcrel set to 0 (FALSE, since the pointer is not going to have the pc added 33 * to it). As with all literals, literals in this section must beable to me 34 * moved freely with respect to other literals. This means relocation to this 35 * literal must not reach outside the size of the literal. The size of this 36 * this type of section must be a multiple of 4 bytes (size of a pointer) in 37 * all input files. 38 */ 39#include <stdlib.h> 40#if !(defined(KLD) && defined(__STATIC__)) 41#include <stdio.h> 42#include <mach/mach.h> 43#else /* defined(KLD) && defined(__STATIC__) */ 44#include <mach/mach.h> 45#include <mach/kern_return.h> 46#endif /* !(defined(KLD) && defined(__STATIC__)) */ 47#include <stdarg.h> 48#include <string.h> 49#include "stuff/openstep_mach.h" 50#include <mach-o/loader.h> 51#include <mach-o/nlist.h> 52#include <mach-o/reloc.h> 53#include "stuff/bool.h" 54#include "stuff/bytesex.h" 55 56#include "ld.h" 57#include "live_refs.h" 58#include "objects.h" 59#include "sections.h" 60#include "pass2.h" 61#include "generic_reloc.h" 62#include "pass1.h" 63#include "symbols.h" 64#include "layout.h" 65#include "literal_pointers.h" 66#include "cstring_literals.h" 67#include "4byte_literals.h" 68#include "8byte_literals.h" 69#include "dylibs.h" 70 71static unsigned long lookup_literal_pointer( 72 struct merged_symbol *merged_symbol, 73 struct merged_section *literal_ms, 74 unsigned long merged_section_offset, 75 unsigned long offset, 76 struct literal_pointer_data *data, 77 struct merged_section *ms, 78 enum bool *new); 79 80#ifndef RLD 81static unsigned long literal_pointer_order_line( 82 unsigned long *line_start, 83 unsigned long line_number, 84 struct literal_pointer_data *data, 85 struct merged_section *ms, 86 char *buffer); 87#endif /* !defined(RLD) */ 88 89static void count_reloc( 90 struct merged_section *ms, 91 enum bool new, 92 unsigned long r_extern, 93 enum bool defined); 94 95/* 96 * literal_pointer_merge() merges literal pointers from the specified section 97 * current object file (cur_obj). When redo_live is FALSE it allocates a fine 98 * relocation map and sets the fine_relocs field in the section_map to it (as 99 * well as the count). When redo_live is TRUE it re-merges only the live 100 * literal pointers based on the live bit in the previouly allocated 101 * fine_relocs. 102 */ 103__private_extern__ 104void 105literal_pointer_merge( 106struct literal_pointer_data *data, 107struct merged_section *ms, 108struct section *s, 109struct section_map *section_map, 110enum bool redo_live) 111{ 112 long i; 113 unsigned long nliterals, j; 114 char *literals; 115 struct fine_reloc *fine_relocs; 116 struct relocation_info *relocs; 117 struct relocation_info *reloc; 118 struct scattered_relocation_info *sreloc; 119 unsigned long r_address, r_symbolnum, r_pcrel, r_length, r_extern, 120 r_scattered, r_value; 121 struct undefined_map *undefined_map; 122 struct nlist *nlists; 123 char *strings; 124 enum bool defined, new; 125 126 struct merged_symbol *merged_symbol; 127 struct section_map *literal_map; 128 struct merged_section *literal_ms; 129 struct section *literal_s; 130 unsigned long section_value, input_section_offset, merged_section_offset, 131 offset; 132 133 if(s->size == 0){ 134 if(redo_live == FALSE){ 135 section_map->fine_relocs = NULL; 136 section_map->nfine_relocs = 0; 137 } 138 return; 139 } 140 if(s->size % 4 != 0){ 141 error_with_cur_obj("literal pointer section (%.16s,%.16s) size is " 142 "not a multiple of 4 bytes",s->segname, s->sectname); 143 return; 144 } 145 nliterals = s->size / 4; 146 if(s->nreloc != nliterals){ 147 error_with_cur_obj("literal pointer section (%.16s,%.16s) does not " 148 "have is exactly one relocation entry for each " 149 "pointer\n", s->segname, s->sectname); 150 return; 151 } 152#ifdef DEBUG 153 if(redo_live == FALSE){ 154 data->nfiles++; 155 data->nliterals += nliterals; 156 } 157#endif /* DEBUG */ 158 /* 159 * The size is not zero an it has as many relocation entries as literals so 160 * this section is being relocated (as long as -dead_strip is not 161 * specified). 162 */ 163 if(dead_strip == FALSE) 164 ms->relocated = TRUE; 165 166 /* 167 * If redo_live == FALSE this is the first time we are called so set the 168 * output_offset to -1 here so that when going through the relocation 169 * entries to merge the literals the error of having more than one 170 * relocation entry for each literal can be caught. 171 */ 172 if(redo_live == FALSE){ 173 fine_relocs = allocate(nliterals * sizeof(struct fine_reloc)); 174 memset(fine_relocs, '\0', nliterals * sizeof(struct fine_reloc)); 175 for(j = 0; j < nliterals; j++){ 176 fine_relocs[j].output_offset = -1; 177 } 178 section_map->fine_relocs = fine_relocs; 179 section_map->nfine_relocs = nliterals; 180 } 181 else{ 182 /* 183 * redo_live is TRUE so this is the second time we are called to 184 * re-merge just the live fine_relocs. 185 */ 186 fine_relocs = section_map->fine_relocs; 187 nliterals = section_map->nfine_relocs; 188 } 189 190 /* 191 * Because at this point it is known that their are exactly as many literals 192 * in the section as relocation entries either could be used to merge the 193 * the literals themselves. The loop is driven off the relocation entries 194 * for two reasons: first if it looped throught the literals themselfs a 195 * costly search would result in trying to find the relocation entry for it 196 * and by doing it the way it is done here the r_address (really an offset) 197 * can be used directly to get the literal, secondly by looping through the 198 * relocation entries checking for the error case of more than one 199 * relocation entry refering to the same literal can be caught easily. So 200 * if everything goes without error then their must have been exactly one 201 * relocation entry for each literal pointer in this section. The reason 202 * that this loop runs backwards through the relocation entries is to get 203 * this implementation of merging literal pointers to match the previous 204 * one so a binary compare can be done (the previous implemention went 205 * through the literals and the current assembler puts out the relocation 206 * entries in reverse order so these two implementions just happen to get 207 * the exact same result). 208 */ 209 relocs = (struct relocation_info *)(cur_obj->obj_addr + s->reloff); 210 literals = (char *)(cur_obj->obj_addr + s->offset); 211 if(cur_obj->swapped && section_map->input_relocs_already_swapped == FALSE){ 212 swap_relocation_info(relocs, s->nreloc, host_byte_sex); 213 section_map->input_relocs_already_swapped = TRUE; 214 } 215 merged_symbol = NULL; 216 for(i = s->nreloc - 1; i >= 0 ; i--){ 217 /* 218 * Break out the fields of the relocation entry. 219 */ 220 if((relocs[i].r_address & R_SCATTERED) != 0){ 221 sreloc = (struct scattered_relocation_info *)(relocs + i); 222 reloc = NULL; 223 r_scattered = 1; 224 r_address = sreloc->r_address; 225 r_pcrel = sreloc->r_pcrel; 226 r_length = sreloc->r_length; 227 r_value = sreloc->r_value; 228 r_extern = 0; 229 /* calculate the r_symbolnum (n_sect) from the r_value */ 230 r_symbolnum = 0; 231 for(j = 0; j < cur_obj->nsection_maps; j++){ 232 if(r_value >= cur_obj->section_maps[j].s->addr && 233 r_value < cur_obj->section_maps[j].s->addr + 234 cur_obj->section_maps[j].s->size){ 235 r_symbolnum = j + 1; 236 break; 237 } 238 } 239 if(r_symbolnum == 0){ 240 /* 241 * The edge case where the last address past then end of 242 * of the last section is referenced. 243 */ 244 for(j = 0; j < cur_obj->nsection_maps; j++){ 245 if(r_value == cur_obj->section_maps[j].s->addr + 246 cur_obj->section_maps[j].s->size){ 247 r_symbolnum = j + 1; 248 break; 249 } 250 } 251 if(r_symbolnum == 0){ 252 error_with_cur_obj("r_value (0x%x) field of relocation " 253 "entry %lu in section (%.16s,%.16s) out of range", 254 (unsigned int)r_value, i, s->segname, s->sectname); 255 continue; 256 } 257 } 258 } 259 else{ 260 reloc = relocs + i; 261 sreloc = NULL; 262 r_scattered = 0; 263 r_address = reloc->r_address; 264 r_pcrel = reloc->r_pcrel; 265 r_length = reloc->r_length; 266 r_extern = reloc->r_extern; 267 r_symbolnum = reloc->r_symbolnum; 268 r_value = 0; 269 } 270 /* 271 * The r_address field is really an offset into the contents of the 272 * section and must reference something inside the section. 273 */ 274 if(r_address >= s->size){ 275 error_with_cur_obj("r_address (0x%x) field of relocation entry " 276 "%ld in section (%.16s,%.16s) out of range", 277 (unsigned int)r_address, i, s->segname, s->sectname); 278 continue; 279 } 280 /* 281 * For a literal pointer section all relocation entries must be for one 282 * of the pointers and therefore the offset must be a multiple of 4, 283 * have an r_length field of 2 (long) and a r_pcrel field of 0 (FALSE). 284 */ 285 if(r_address % 4 != 0){ 286 error_with_cur_obj("r_address (0x%x) field of relocation entry " 287 "%ld in literal pointer section (%.16s,%.16s) is not a " 288 "multiple of 4", (unsigned int)r_address, i, s->segname, 289 s->sectname); 290 continue; 291 } 292 if(r_length != 2){ 293 error_with_cur_obj("r_length (0x%x) field of relocation entry " 294 "%ld in literal pointer section (%.16s,%.16s) is not 2 (long)", 295 (unsigned int)r_length, i, s->segname, s->sectname); 296 continue; 297 } 298 if(r_pcrel != 0){ 299 error_with_cur_obj("r_pcrel (0x%x) field of relocation entry " 300 "%ld in literal pointer section (%.16s,%.16s) is not 0 " 301 "(FALSE)", (unsigned int)r_pcrel, i, s->segname, s->sectname); 302 continue; 303 } 304 defined = TRUE; 305 /* 306 * If r_extern is set this relocation entry is an external entry 307 * else it is a local entry (or scattered entry). 308 */ 309 if(r_extern){ 310 /* 311 * This is an external relocation entry. So the value to be 312 * added to the item to be relocated is the value of the symbol. 313 * r_symbolnum is an index into the input file's symbol table 314 * of the symbol being refered to. The symbol must be an 315 * undefined symbol to be used in an external relocation entry. 316 */ 317 if(r_symbolnum >= cur_obj->symtab->nsyms){ 318 error_with_cur_obj("r_symbolnum (%lu) field of external " 319 "relocation entry %ld in section (%.16s,%.16s) out of " 320 "range", r_symbolnum, i, s->segname, s->sectname); 321 continue; 322 } 323 undefined_map = bsearch(&r_symbolnum, cur_obj->undefined_maps, 324 cur_obj->nundefineds, sizeof(struct undefined_map), 325 (int (*)(const void *, const void *))undef_bsearch); 326 if(undefined_map != NULL){ 327 merged_symbol = undefined_map->merged_symbol; 328 } 329 else{ 330 nlists = (struct nlist *)(cur_obj->obj_addr + 331 cur_obj->symtab->symoff); 332 strings = cur_obj->obj_addr + cur_obj->symtab->stroff; 333 if((nlists[r_symbolnum].n_type & N_EXT) != N_EXT){ 334 error_with_cur_obj("r_symbolnum (%lu) field of external " 335 "relocation entry %lu in section (%.16s,%.16s) refers " 336 "to a non-external symbol", r_symbolnum, i, s->segname, 337 s->sectname); 338 continue; 339 } 340 /* 341 * We must correctly catch the errors of a literal pointer 342 * refering defined global coalesced symbols with external 343 * relocation entries. 344 */ 345 if((nlists[r_symbolnum].n_type & N_TYPE) == N_SECT && 346 (cur_obj->section_maps[nlists[r_symbolnum]. 347 n_sect-1].s->flags & SECTION_TYPE) == S_COALESCED){ 348 merged_symbol = lookup_symbol(strings + 349 nlists[r_symbolnum].n_un.n_strx); 350 if(merged_symbol->name_len == 0){ 351 fatal("internal error, in literal_pointer_merge() " 352 "failed to lookup coalesced symbol %s", 353 strings + nlists[r_symbolnum].n_un.n_strx); 354 } 355 error_with_cur_obj("exteral symbol (%s) for external " 356 "relocation entry %ld in section (%.16s,%.16s) refers " 357 "a symbol not defined in a literal section", 358 merged_symbol->nlist.n_un.n_name, i, s->segname, 359 s->sectname); 360 continue; 361 } 362 else{ 363 if(nlists[r_symbolnum].n_type != (N_EXT | N_UNDF)){ 364 error_with_cur_obj("r_symbolnum (%lu) field of " 365 "external relocation entry %lu in section " 366 "(%.16s,%.16s) refers to a non-undefined symbol", 367 r_symbolnum, i, s->segname, s->sectname); 368 continue; 369 } 370 print_obj_name(cur_obj); 371 fatal("internal error, in literal_pointer_merge() symbol " 372 "index %lu in above file not in undefined map", 373 r_symbolnum); 374 } 375 } 376 /* 377 * If this is an indirect symbol resolve indirection (all chains 378 * of indirect symbols have been resolved so that they point at 379 * a symbol that is not an indirect symbol). 380 */ 381 if((merged_symbol->nlist.n_type & N_TYPE) == N_INDR) 382 merged_symbol = (struct merged_symbol *) 383 merged_symbol->nlist.n_value; 384 /* 385 * If the symbol is a common symbol it is an error 386 * because it not a pointer to a literal. 387 */ 388 if(merged_symbol->nlist.n_type == (N_EXT | N_UNDF) && 389 merged_symbol->nlist.n_value != 0){ 390 error_with_cur_obj("r_symbolnum (%lu) field of external " 391 "relocation entry %ld in section (%.16s,%.16s) refers to " 392 "a common symbol (%s) and is not in a literal section", 393 r_symbolnum, i, s->segname, s->sectname, 394 merged_symbol->nlist.n_un.n_name); 395 continue; 396 } 397 /* 398 * If the symbol is an absolute symbol it is treated as an error 399 * because it is not known to be a pointer to a literal. 400 */ 401 if((merged_symbol->nlist.n_type & N_TYPE) == N_ABS){ 402 error_with_cur_obj("r_symbolnum (%lu) field of external " 403 "relocation entry %ld in section (%.16s,%.16s) refers to " 404 "an absolue symbol (%s) and is not known to be in a " 405 "literal section", r_symbolnum, i, s->segname, s->sectname, 406 merged_symbol->nlist.n_un.n_name); 407 continue; 408 } 409 /* 410 * For multi module dynamic shared library format files the 411 * merged sections that could have had external relocation 412 * entries must be resolved to private extern symbols. This is 413 * because for multi module MH_DYLIB files all modules share the 414 * merged sections and the entire section gets relocated when 415 * the library is mapped in. So the above restriction assures 416 * the merged section will get relocated properly and can be 417 * shared amoung library modules. 418 */ 419 if(filetype == MH_DYLIB && multi_module_dylib == TRUE){ 420 /* 421 * If the symbol is undefined or not a private extern it is an 422 * error for in this section for a MH_DYLIB file. 423 */ 424 if(merged_symbol->nlist.n_type == (N_EXT | N_UNDF)){ 425 if(merged_symbol->error_flagged_for_dylib == 0){ 426 error_with_cur_obj("illegal undefined reference for " 427 "multi module MH_DYLIB output file to symbol: %s " 428 "from a literal pointer section (section (%.16s," 429 "%.16s) relocation entry: %lu)", 430 merged_symbol->nlist.n_un.n_name, s->segname, 431 s->sectname, i); 432 merged_symbol->error_flagged_for_dylib = 1; 433 } 434 } 435 else if((merged_symbol->nlist.n_type & N_PEXT) != N_PEXT){ 436 if(merged_symbol->error_flagged_for_dylib == 0){ 437 error_with_cur_obj("illegal external reference for " 438 "multi module MH_DYLIB output file to symbol: %s " 439 "(not a private extern symbol) from a literal " 440 "pointer section (section (%.16s,%.16s) relocation " 441 "entry: %lu)", merged_symbol->nlist.n_un.n_name, 442 s->segname, s->sectname, i); 443 merged_symbol->error_flagged_for_dylib = 1; 444 } 445 } 446 } 447 /* 448 * If the symbol is an undefined symbol then the literal is an 449 * offset to be added to the value of the symbol. 450 */ 451 if(merged_symbol->nlist.n_type == (N_EXT | N_UNDF)){ 452 literal_ms = NULL; 453 merged_section_offset = 0; 454 offset = get_long((long *)(literals + r_address)); 455 defined = FALSE; 456 } 457 else { 458 /* 459 * All other types of symbol of symbol have been handled so this 460 * symbol must be defined in a section. The section that the 461 * symbol is defined in must be a literal section or else it 462 * is an error. Since this is an external relocation entry and 463 * the symbol is defined this means it is defined in some other 464 * object than this one. 465 */ 466 if((merged_symbol->nlist.n_type & N_TYPE) != N_SECT || 467 (merged_symbol->nlist.n_type & N_EXT) != N_EXT){ 468 fatal("internal error, in merge_literal_pointers() merged " 469 "symbol %s does not have a type of N_EXT|N_SECT", 470 merged_symbol->nlist.n_un.n_name); 471 } 472 literal_map = &(merged_symbol->definition_object-> 473 section_maps[merged_symbol->nlist.n_sect - 1]); 474 literal_ms = literal_map->output_section; 475 if((literal_ms->s.flags & SECTION_TYPE) != S_CSTRING_LITERALS && 476 (literal_ms->s.flags & SECTION_TYPE) != S_4BYTE_LITERALS && 477 (literal_ms->s.flags & SECTION_TYPE) != S_8BYTE_LITERALS){ 478 error_with_cur_obj("exteral symbol (%s) for external " 479 "relocation entry %ld in section (%.16s,%.16s) refers " 480 "a symbol not defined in a literal section", 481 merged_symbol->nlist.n_un.n_name, i, s->segname, 482 s->sectname); 483 continue; 484 } 485 section_value = merged_symbol->nlist.n_value; 486 literal_s = literal_map->s; 487 if(section_value < literal_s->addr || 488 section_value > literal_s->addr + literal_s->size){ 489 error_with_cur_obj("exteral symbol's (%s) address not in " 490 "the section (%.16s,%.16s) it is defined in", 491 merged_symbol->nlist.n_un.n_name, literal_s->segname, 492 literal_s->sectname); 493 continue; 494 } 495 input_section_offset = section_value - literal_s->addr; 496 /* 497 * At this point it is known that the merged section the 498 * literal is defined in is a literal section. The checking 499 * for an internal error if the section does not have fine_reloc 500 * entry is left to fine_reloc_output_offset(); 501 */ 502 merged_section_offset = fine_reloc_output_offset(literal_map, 503 input_section_offset); 504 /* 505 * since this was an external relocation entry the value of the 506 * literal pointer is symbol+offset and the relocation is done 507 * based on only the symbol's value without the offset added. 508 * That's why offset is NOT added to input_section_offset above. 509 * Also if the offset is not zero that is need to be known so 510 * that a scattered relocation entry can be created on output. 511 */ 512 offset = get_long((long *)(literals + r_address)); 513 /* 514 * merged_symbol is set to NULL for the call to 515 * lookup_literal_pointer because this symbol is not undefined. 516 */ 517 merged_symbol = NULL; 518 519 /* mark the section this symbol is in as referenced */ 520 literal_map->output_section->referenced = TRUE; 521 } 522 } 523 else{ 524 /* 525 * This is a local relocation entry (the value to which the item 526 * to be relocated is refering to is defined in section number 527 * r_symbolnum in this object file). Check that r_symbolnum is not 528 * R_ABS so it can be used to directly index the section map. 529 * For scattered relocation entries r_value was previously checked 530 * to be in the section refered to by r_symbolnum. 531 */ 532 if(r_symbolnum == R_ABS){ 533 error_with_cur_obj("r_symbolnum (0x%x) field of relocation " 534 "entry %ld in literal pointer section (%.16s,%.16s) is " 535 "R_ABS (not correct for a literal pointer section)", 536 (unsigned int)r_symbolnum, i, s->segname, s->sectname); 537 continue; 538 } 539 merged_symbol = NULL; 540 literal_map = &(cur_obj->section_maps[r_symbolnum - 1]); 541 literal_s = literal_map->s; 542 literal_ms = literal_map->output_section; 543 if(r_scattered == 0){ 544 offset = 0; 545 section_value = get_long((long *)(literals + r_address)); 546 if(section_value < literal_s->addr || 547 section_value > literal_s->addr + literal_s->size){ 548 error_with_cur_obj("literal pointer (0x%x) in section " 549 "(%.16s,%.16s) at address 0x%x does not point into " 550 "its (%.16s,%.16s) section as refered to by its " 551 "r_symbolnum (0x%x) field in relocation entry %ld as " 552 "it should", (unsigned int)section_value, s->segname, 553 s->sectname, (unsigned int)(s->addr + r_address), 554 literal_s->segname, literal_s->sectname, 555 (unsigned int)r_symbolnum, i); 556 continue; 557 } 558 if((literal_ms->s.flags & SECTION_TYPE) != S_CSTRING_LITERALS && 559 (literal_ms->s.flags & SECTION_TYPE) != S_4BYTE_LITERALS && 560 (literal_ms->s.flags & SECTION_TYPE) != S_8BYTE_LITERALS){ 561 error_with_cur_obj("r_symbolnum field (0x%x) in relocation " 562 "entry %ld in literal pointer section (%.16s,%.16s) " 563 "refers to section (%.16s,%.16s) which is not a " 564 "literal section", (unsigned int)r_symbolnum, i, 565 s->segname, s->sectname, literal_ms->s.segname, 566 literal_ms->s.sectname); 567 continue; 568 } 569 } 570 else{ 571 offset = get_long((long *)(literals + r_address)) - r_value; 572 section_value = r_value; 573 if((literal_ms->s.flags & SECTION_TYPE) != S_CSTRING_LITERALS && 574 (literal_ms->s.flags & SECTION_TYPE) != S_4BYTE_LITERALS && 575 (literal_ms->s.flags & SECTION_TYPE) != S_8BYTE_LITERALS){ 576 error_with_cur_obj("r_value field (0x%x) in relocation " 577 "entry %ld in literal pointer section (%.16s,%.16s) " 578 "refers to section (%.16s,%.16s) which is not a " 579 "literal section", (unsigned int)r_value, i, s->segname, 580 s->sectname, literal_ms->s.segname, 581 literal_ms->s.sectname); 582 continue; 583 } 584 } 585 input_section_offset = section_value - literal_s->addr; 586 /* 587 * At this point it is known that the merged section the 588 * literal is defined in is a literal section. The checking 589 * for an internal error if the section does not have fine_reloc 590 * entry is left to fine_reloc_output_offset(); 591 */ 592 merged_section_offset = fine_reloc_output_offset(literal_map, 593 input_section_offset); 594 /* mark the section this literal is in as referenced */ 595 literal_map->output_section->referenced = TRUE; 596 } 597 598 /* 599 * Since all the output_offset field of all the fine reloc entries were 600 * set to -1 before merging the literals and there must be only one 601 * relocation entry for each literal pointer if the relocation entry 602 * for this literal does not have an output_offset of -1 it is an error 603 * because we have seen it before. 604 */ 605 if(redo_live == FALSE && 606 (int)(fine_relocs[r_address/4].output_offset) != -1){ 607 error_with_cur_obj("more than one relocation entry for literal " 608 "pointer at address 0x%x (r_address 0x%x) in section " 609 "(%.16s,%.16s)", (unsigned int)(s->addr + r_address), 610 (unsigned int)r_address, s->segname, s->sectname); 611 continue; 612 } 613 614 /* 615 * If redo_live == FALSE this is the first time we are called and now 616 * at long last the literal pointer can be merged and the fine 617 * relocation entry for it can be built. 618 */ 619 if(redo_live == FALSE){ 620 fine_relocs[r_address/4].input_offset = r_address; 621 fine_relocs[r_address/4].output_offset = 622 lookup_literal_pointer(merged_symbol, literal_ms, 623 merged_section_offset, offset, data, 624 ms, &new); 625 count_reloc(ms, new, r_extern, defined); 626 } 627 else{ 628 /* 629 * redo_live == TRUE so if this fine_reloc is live re-merge it. 630 */ 631 if(fine_relocs[r_address/4].live == TRUE){ 632 /* 633 * Since we now know that there will be a live pointer in this 634 * section and since it has a relocation entry mark the merged 635 * section as relocated. 636 */ 637 ms->relocated = TRUE; 638 fine_relocs[r_address/4].output_offset = 639 lookup_literal_pointer(merged_symbol, literal_ms, 640 merged_section_offset, offset, data, 641 ms, &new); 642 count_reloc(ms, new, r_extern, defined); 643 } 644 else{ 645 fine_relocs[r_address/4].output_offset = 0; 646 } 647 } 648 } 649} 650 651/* 652 * count reloc is used after a call to lookup_literal_pointer() to count the 653 * relocation entry for the literal if it will be in the output file. 654 */ 655static void 656count_reloc( 657struct merged_section *ms, 658enum bool new, 659unsigned long r_extern, 660enum bool defined) 661{ 662 /* 663 * If saving relocation entries count it as one of the output 664 * relocation entries. 665 */ 666 if(output_for_dyld && new == TRUE){ 667 /* 668 * The number of relocation entries in the output file is based 669 * on one of three different cases: 670 * The output file is a multi module dynamic shared library 671 * The output file has a dynamic linker load command 672 * The output does not have a dynamic linker load command 673 */ 674 if(filetype == MH_DYLIB && multi_module_dylib == TRUE){ 675 /* 676 * For a multi module dynamic shared library there are no 677 * external relocation entries that will be left as external as 678 * checked above. Only non-sectdiff local relocation entries 679 * are kept. Modules of multi module dylibs are not linked 680 * together and can only be slid keeping all sections 681 * relative to each other the same. 682 */ 683 ms->nlocrel++; 684 } 685 else if(has_dynamic_linker_command){ 686 /* 687 * For an file with a dynamic linker load command only 688 * external relocation entries for undefined symbols are 689 * kept. This output file is a fixed address and can't be 690 * moved. 691 */ 692 if(r_extern) 693 if(defined == FALSE) 694 ms->nextrel++; 695 } 696 else{ 697 /* 698 * For an file without a dynamic linker load command 699 * external relocation entries for undefined symbols are 700 * kept and locals that are non-sectdiff are kept. This 701 * file can only be slid keeping all sections relative to 702 * each other the same. 703 */ 704 if(r_extern){ 705 if(defined == FALSE) 706 ms->nextrel++; 707 else 708 ms->nlocrel++; 709 } 710 else 711 ms->nlocrel++; 712 } 713 } 714 else if(save_reloc && new == TRUE){ 715 ms->s.nreloc++; 716 nreloc++; 717 } 718} 719 720/* 721 * literal_pointer_order() enters literal pointers from the order_file from the 722 * merged section structure. Since this is called before any call to 723 * literal_pointer_merge and it enters the literals in the order of the file it 724 * causes the section to be ordered. 725 */ 726__private_extern__ 727void 728literal_pointer_order( 729struct literal_pointer_data *data, 730struct merged_section *ms) 731{ 732#ifndef RLD 733 unsigned long i, line_number, line_length, max_line_length, output_offset; 734 char *buffer; 735 kern_return_t r; 736 struct literal_pointer_order_line *order_lines; 737 738 /* 739 * Parse the load order file by changing '\n' to '\0'. Also check for 740 * '\0 in the file and flag them as errors. Also determine the maximum 741 * line length of the file for the needed buffer to allocate for 742 * character translation. 743 */ 744 line_number = 1; 745 line_length = 1; 746 max_line_length = 1; 747 for(i = 0; i < ms->order_size; i++){ 748 if(ms->order_addr[i] == '\0'){ 749 fatal("format error in -sectorder file: %s line %lu character " 750 "possition %lu for section (%.16s,%.16s) (illegal null " 751 "character \'\\0\' found)", ms->order_filename, 752 line_number, line_length, ms->s.segname, ms->s.sectname); 753 } 754 if(ms->order_addr[i] == '\n'){ 755 ms->order_addr[i] = '\0'; 756 if(line_length > max_line_length) 757 max_line_length = line_length; 758 line_length = 1; 759 line_number++; 760 } 761 else 762 line_length++; 763 } 764 765 /* 766 * Allocate the buffer to translate the order file lines' escape 767 * characters into real characters. 768 */ 769 buffer = allocate(max_line_length + 1); 770 771 /* 772 * If -dead_strip is specified allocate the needed structures so that 773 * the order of the live literal pointers can be recreated later by 774 * literal_pointer_reset_live(). 775 */ 776 order_lines = NULL; 777 if(dead_strip == TRUE){ 778 data->literal_pointer_load_order_data = 779 allocate(sizeof(struct literal_pointer_load_order_data)); 780 order_lines = allocate(sizeof(struct literal_pointer_order_line) * 781 (line_number - 1)); 782 data->literal_pointer_load_order_data->order_line_buffer = 783 buffer; 784 data->literal_pointer_load_order_data->literal_pointer_order_lines = 785 order_lines; 786 data->literal_pointer_load_order_data->nliteral_pointer_order_lines 787 = (line_number - 1); 788 } 789 790 /* 791 * Process each line in the order file. 792 */ 793 line_number = 1; 794 for(i = 0; i < ms->order_size; i++){ 795 796 if(dead_strip == TRUE){ 797 order_lines[line_number - 1].character_index = i; 798 order_lines[line_number - 1].line_number = line_number; 799 } 800 801 output_offset = literal_pointer_order_line(&i, line_number, data, 802 ms, buffer); 803 if(dead_strip == TRUE) 804 order_lines[line_number - 1].output_offset = output_offset; 805 806 /* skip any trailing characters on the line */ 807 while(i < ms->order_size && ms->order_addr[i] != '\0') 808 i++; 809 810 line_number++; 811 } 812 813 /* 814 * If -dead_strip is not specified free up the memory for the line 815 * buffer and the load order file. If -dead_strip is specified these 816 * will be free'ed up in literal_pointer_reset_live(). 817 */ 818 if(dead_strip == FALSE){ 819 /* deallocate the buffer */ 820 free(buffer); 821 822 /* 823 * Deallocate the memory for the load order file now that it is 824 * nolonger needed (since the memory has been written on it is 825 * allways deallocated so it won't get written to the swap file 826 * unnecessarily). 827 */ 828 if((r = vm_deallocate(mach_task_self(), (vm_address_t) 829 ms->order_addr, ms->order_size)) != KERN_SUCCESS) 830 mach_fatal(r, "can't vm_deallocate() memory for -sectorder " 831 "file: %s for section (%.16s,%.16s)", 832 ms->order_filename, ms->s.segname, 833 ms->s.sectname); 834 ms->order_addr = NULL; 835 } 836#endif /* !defined(RLD) */ 837} 838 839#ifndef RLD 840/* 841 * literal_pointer_order_line() parses out and enters the literal pointer and 842 * literal from the order line specified by the parameters. The parameter 843 * buffer is a buffer used to parse any C string on the line and must be as 844 * large as the longest line in the order file. It returns the output_offset 845 * in the merged section for the literal pointer and indirectly returns the 846 * resulting line_start after the characters for this line. 847 */ 848static 849unsigned long 850literal_pointer_order_line( 851unsigned long *line_start, 852unsigned long line_number, 853struct literal_pointer_data *data, 854struct merged_section *ms, 855char *buffer) 856{ 857 unsigned long i, j, char_pos, output_offset, merged_section_offset; 858 char segname[17], sectname[17]; 859 struct merged_section *literal_ms; 860 struct literal8 literal8; 861 struct literal4 literal4; 862 enum bool new; 863 864 /* 865 * An order line for a literal pointer is three parts: 866 * segment_name:section_name:literal 867 * The segment_name and section_name are strings separated by a colon 868 * character ':' which also separates the literal. The literal is just 869 * as it would be for a cstring, 4-byte or 8-byte literal. The literals 870 * are looked up using the function for each literal then the literal 871 * pointer is looked up. 872 */ 873 output_offset = 0; 874 i = *line_start; 875 char_pos = 1; 876 /* copy segment name into segname */ 877 j = 0; 878 while(i < ms->order_size && 879 ms->order_addr[i] != ':' && 880 ms->order_addr[i] != '\0'){ 881 if(j <= 16) 882 segname[j++] = ms->order_addr[i++]; 883 else 884 i++; 885 char_pos++; 886 } 887 if(i >= ms->order_size || ms->order_addr[i] == '\0'){ 888 error("format error in -sectorder file: %s line %lu for section" 889 " (%.16s,%.16s) (missing ':' after segment name)", 890 ms->order_filename, line_number, ms->s.segname, 891 ms->s.sectname); 892 *line_start = i; 893 return(output_offset); 894 } 895 segname[j] = '\0'; 896 i++; 897 char_pos++; 898 899 /* copy section name into sectname */ 900 j = 0; 901 while(i < ms->order_size && 902 ms->order_addr[i] != ':' && 903 ms->order_addr[i] != '\0'){ 904 if(j <= 16) 905 sectname[j++] = ms->order_addr[i++]; 906 else 907 i++; 908 char_pos++; 909 } 910 if(i >= ms->order_size || ms->order_addr[i] == '\0'){ 911 error("format error in -sectorder file: %s line %lu for section" 912 " (%.16s,%.16s) (missing ':' after section name)", 913 ms->order_filename, line_number, ms->s.segname, 914 ms->s.sectname); 915 *line_start = i; 916 return(output_offset); 917 } 918 sectname[j] = '\0'; 919 i++; 920 char_pos++; 921 922 literal_ms = lookup_merged_section(segname, sectname); 923 if(literal_ms == NULL){ 924 error("error in -sectorder file: %s line %lu for section " 925 "(%.16s,%.16s) (specified section (%s,%s) is not " 926 "loaded objects)", ms->order_filename, line_number, 927 ms->s.segname, ms->s.sectname, segname, sectname); 928 } 929 else{ 930 switch(literal_ms->s.flags & SECTION_TYPE){ 931 case S_CSTRING_LITERALS: 932 get_cstring_from_sectorder(ms, &i, buffer, line_number, 933 char_pos); 934 merged_section_offset = lookup_cstring(buffer, 935 literal_ms->literal_data, literal_ms); 936 output_offset = lookup_literal_pointer(NULL, literal_ms, 937 merged_section_offset, 0, data, ms, &new); 938 count_reloc(ms, new, 0, FALSE); 939 break; 940 case S_4BYTE_LITERALS: 941 if(get_hex_from_sectorder(ms, &i, &(literal4.long0), 942 line_number) == TRUE){ 943 merged_section_offset = lookup_literal4(literal4, 944 literal_ms->literal_data, literal_ms); 945 output_offset = lookup_literal_pointer(NULL, literal_ms, 946 merged_section_offset, 0, data, ms, &new); 947 count_reloc(ms, new, 0, FALSE); 948 } 949 else{ 950 error("error in -sectorder file: %s line %lu for " 951 "section (%.16s,%.16s) (missing hex number for " 952 "specified 4 byte literal section (%s,%s))", 953 ms->order_filename, line_number, 954 ms->s.segname, ms->s.sectname, segname, sectname); 955 } 956 break; 957 case S_8BYTE_LITERALS: 958 if(get_hex_from_sectorder(ms, &i, &(literal8.long0), 959 line_number) == TRUE){ 960 if(get_hex_from_sectorder(ms, &i, &(literal8.long1), 961 line_number) == TRUE){ 962 merged_section_offset = lookup_literal8(literal8, 963 literal_ms->literal_data, 964 literal_ms); 965 output_offset = lookup_literal_pointer(NULL, literal_ms, 966 merged_section_offset, 0, data, ms, &new); 967 count_reloc(ms, new, 0, FALSE); 968 } 969 else{ 970 error("error in -sectorder file: %s line %lu for " 971 "section (%.16s,%.16s) (missing second hex " 972 "number for specified 8 byte literal section " 973 "(%s,%s))", ms->order_filename, line_number, 974 ms->s.segname, ms->s.sectname, segname, 975 sectname); 976 } 977 } 978 else{ 979 error("error in -sectorder file: %s line %lu for " 980 "section (%.16s,%.16s) (missing first hex number " 981 "for specified 8 byte literal section (%s,%s))", 982 ms->order_filename, line_number, 983 ms->s.segname, ms->s.sectname, segname, sectname); 984 } 985 break; 986 default: 987 error("error in -sectorder file: %s line %lu for section " 988 "(%.16s,%.16s) (specified section (%s,%s) is not a " 989 "literal section)", ms->order_filename, line_number, 990 ms->s.segname, ms->s.sectname, segname, sectname); 991 break; 992 } 993 } 994 *line_start = i; 995 return(output_offset); 996} 997#endif /* !defined(RLD) */ 998 999/* 1000 * literal_pointer_reset_live() is called when -dead_strip is specified after 1001 * all the literal pointers from the input objects are merged. It clears out 1002 * the literal_pointer_data so the live literal pointers can be re-merged (by 1003 * later calling literal_pointer_merge() with redo_live == TRUE. In here we 1004 * first merge in the live literal pointers from the order file if any. 1005 */ 1006__private_extern__ 1007void 1008literal_pointer_reset_live( 1009struct literal_pointer_data *data, 1010struct merged_section *ms) 1011{ 1012#ifndef RLD 1013 unsigned long i, norder_lines, line_number, character_index, output_offset; 1014 char *buffer; 1015 struct literal_pointer_order_line *order_lines; 1016 enum bool live; 1017 kern_return_t r; 1018 1019 /* reset the merge section size back to zero */ 1020 ms->s.size = 0; 1021 1022 /* reset the count of relocation entries for this merged section */ 1023 if(output_for_dyld){ 1024 ms->nlocrel = 0; 1025 ms->nextrel = 0; 1026 } 1027 else if(save_reloc){ 1028 nreloc -= ms->s.nreloc; 1029 ms->s.nreloc = 0; 1030 } 1031 1032 /* clear out the previously merged data */ 1033 literal_pointer_free(data); 1034 1035 /* 1036 * If this merged section has an order file we need to re-merged only 1037 * the live literals from that order file. 1038 */ 1039 if(ms->order_filename != NULL){ 1040 buffer = data->literal_pointer_load_order_data->order_line_buffer; 1041 order_lines = data->literal_pointer_load_order_data-> 1042 literal_pointer_order_lines; 1043 norder_lines = data->literal_pointer_load_order_data-> 1044 nliteral_pointer_order_lines; 1045 for(i = 0; i < norder_lines; i++){ 1046 /* 1047 * Figure out if this literal pointer order line's output_index 1048 * is live and if so re-merge the literal pointer. 1049 */ 1050 live = is_literal_output_offset_live( 1051 ms, order_lines[i].output_offset); 1052 line_number = order_lines[i].line_number; 1053 if(live){ 1054 character_index = order_lines[i].character_index; 1055 output_offset = literal_pointer_order_line( 1056 &character_index, line_number, data, ms, buffer); 1057 } 1058 else{ 1059 if(sectorder_detail == TRUE) 1060 warning("specification of literal pointer in " 1061 "-sectorder file: %s on line %lu for section " 1062 "(%.16s,%.16s) not used (dead stripped)", 1063 ms->order_filename, line_number, ms->s.segname, 1064 ms->s.sectname); 1065 } 1066 } 1067 1068 /* deallocate the various data structures no longer needed */ 1069 free(data->literal_pointer_load_order_data->order_line_buffer); 1070 free(data->literal_pointer_load_order_data-> 1071 literal_pointer_order_lines); 1072 free(data->literal_pointer_load_order_data); 1073 data->literal_pointer_load_order_data = NULL; 1074 1075 /* 1076 * Deallocate the memory for the load order file now that it is 1077 * nolonger needed (since the memory has been written on it is 1078 * allways deallocated so it won't get written to the swap file 1079 * unnecessarily). 1080 */ 1081 if((r = vm_deallocate(mach_task_self(), (vm_address_t) 1082 ms->order_addr, ms->order_size)) != KERN_SUCCESS) 1083 mach_fatal(r, "can't vm_deallocate() memory for -sectorder " 1084 "file: %s for section (%.16s,%.16s)", 1085 ms->order_filename, ms->s.segname, 1086 ms->s.sectname); 1087 ms->order_addr = NULL; 1088 } 1089#endif /* !defined(RLD) */ 1090} 1091 1092/* 1093 * lookup_literal_pointer() is passed a quad that defined a literal pointer 1094 * (merged_symbol, literal_ms, merged_section_offset, offset). If merged_symbol 1095 * is not NULL then this pointer is the undefined merged_symbol plus the offset 1096 * else the pointer is into the merged literal section litersal_ms with an 1097 * offset into that section of merged_section_offset plus offset. In either 1098 * case the literal pointer must match exactly (that means merged_section_offset 1099 * can't be added to offset and the sum be used to determine a match). 1100 */ 1101static 1102unsigned long 1103lookup_literal_pointer( 1104struct merged_symbol *merged_symbol, 1105struct merged_section *literal_ms, 1106unsigned long merged_section_offset, 1107unsigned long offset, 1108struct literal_pointer_data *data, 1109struct merged_section *ms, 1110enum bool *new) 1111{ 1112 unsigned long hashval, output_offset; 1113 struct literal_pointer_block **p, *literal_pointer_block; 1114 struct literal_pointer *literal_pointer; 1115 struct literal_pointer_bucket *bp; 1116 1117 *new = FALSE; 1118 if(data->hashtable == NULL){ 1119 data->hashtable = allocate(sizeof(struct literal_pointer_bucket *) * 1120 LITERAL_POINTER_HASHSIZE); 1121 memset(data->hashtable, '\0', 1122 sizeof(struct literal_pointer_bucket *) * 1123 LITERAL_POINTER_HASHSIZE); 1124 } 1125#if defined(DEBUG) && defined(PROBE_COUNT) 1126 data->nprobes++; 1127#endif 1128 hashval = ((long)merged_symbol + (long)literal_ms + 1129 merged_section_offset + offset) % LITERAL_POINTER_HASHSIZE; 1130 for(bp = data->hashtable[hashval]; bp; bp = bp->next){ 1131#if defined(DEBUG) && defined(PROBE_COUNT) 1132 data->nprobes++; 1133#endif 1134 if(bp->literal_pointer->merged_symbol == merged_symbol && 1135 bp->literal_pointer->literal_ms == literal_ms && 1136 bp->literal_pointer->merged_section_offset == 1137 merged_section_offset && 1138 bp->literal_pointer->offset == offset) 1139 return(bp->output_offset); 1140 } 1141 1142 bp = allocate(sizeof(struct literal_pointer_bucket)); 1143 output_offset = 0; 1144 for(p = &(data->literal_pointer_blocks); 1145 *p ; 1146 p = &(literal_pointer_block->next)){ 1147 1148 literal_pointer_block = *p; 1149 if(literal_pointer_block->used != LITERAL_POINTER_BLOCK_SIZE){ 1150 literal_pointer = literal_pointer_block->literal_pointers + 1151 literal_pointer_block->used; 1152 literal_pointer->merged_symbol = merged_symbol; 1153 literal_pointer->literal_ms = literal_ms; 1154 literal_pointer->merged_section_offset = merged_section_offset; 1155 literal_pointer->offset = offset; 1156 1157 bp->literal_pointer = literal_pointer; 1158 bp->output_offset = output_offset + 1159 literal_pointer_block->used * 4; 1160 bp->next = data->hashtable[hashval]; 1161 data->hashtable[hashval] = bp; 1162 1163 literal_pointer_block->used++; 1164 ms->s.size += 4; 1165 *new = TRUE; 1166 return(bp->output_offset); 1167 } 1168 output_offset += literal_pointer_block->used * 4; 1169 } 1170 *p = allocate(sizeof(struct literal_pointer_block)); 1171 literal_pointer_block = *p; 1172 literal_pointer = literal_pointer_block->literal_pointers; 1173 literal_pointer->merged_symbol = merged_symbol; 1174 literal_pointer->literal_ms = literal_ms; 1175 literal_pointer->merged_section_offset = merged_section_offset; 1176 literal_pointer->offset = offset; 1177 literal_pointer_block->used = 1; 1178 literal_pointer_block->next = NULL; 1179 1180 bp->literal_pointer = literal_pointer; 1181 bp->output_offset = output_offset; 1182 bp->next = data->hashtable[hashval]; 1183 data->hashtable[hashval] = bp; 1184 1185 ms->s.size += 4; 1186 *new = TRUE; 1187 return(bp->output_offset); 1188} 1189 1190/* 1191 * literal_pointer_output puts the literal pointers into the output file. 1192 * It also puts the relocation entries for the literal pointers in the output 1193 * file if relocation entries are being saved. 1194 */ 1195__private_extern__ 1196void 1197literal_pointer_output( 1198struct literal_pointer_data *data, 1199struct merged_section *ms) 1200{ 1201 unsigned long i; 1202 long *output_pointer; 1203 struct literal_pointer_block **p, *literal_pointer_block; 1204 struct literal_pointer *literal_pointers; 1205 1206#ifndef RLD 1207 struct relocation_info *reloc, *extreloc, *r; 1208 struct scattered_relocation_info *sreloc; 1209 unsigned long r_address; 1210#endif /* !defined(RLD) */ 1211 1212 /* 1213 * Put the literal pointers into the output file. 1214 */ 1215 output_pointer = (long *)(output_addr + ms->s.offset); 1216 for(p = &(data->literal_pointer_blocks); 1217 *p ; 1218 p = &(literal_pointer_block->next)){ 1219 1220 literal_pointer_block = *p; 1221 literal_pointers = literal_pointer_block->literal_pointers; 1222 for(i = 0; i < literal_pointer_block->used; i++){ 1223 if(literal_pointers[i].merged_symbol != NULL){ 1224 *output_pointer = literal_pointers[i].offset; 1225 } 1226 else{ 1227 *output_pointer = 1228 literal_pointers[i].literal_ms->s.addr + 1229 literal_pointers[i].merged_section_offset + 1230 literal_pointers[i].offset; 1231 } 1232 if(host_byte_sex != target_byte_sex) 1233 *output_pointer = SWAP_LONG(*output_pointer); 1234 output_pointer++; 1235 } 1236 } 1237#ifndef RLD 1238 output_flush(ms->s.offset, 1239 (char *)output_pointer - (char *)(output_addr + ms->s.offset)); 1240 1241 /* 1242 * If saving relocation entries the create the proper relocation entry 1243 * for the literal pointer and put it in the file. 1244 */ 1245 if(save_reloc || output_for_dyld){ 1246 if(output_for_dyld){ 1247 extreloc = (struct relocation_info *)(output_addr + 1248 output_dysymtab_info.dysymtab_command.extreloff + 1249 ms->iextrel * sizeof(struct relocation_info)); 1250 reloc = (struct relocation_info *)(output_addr + 1251 output_dysymtab_info.dysymtab_command.locreloff + 1252 ms->ilocrel * sizeof(struct relocation_info)); 1253 sreloc = (struct scattered_relocation_info *)reloc; 1254 1255 /* 1256 * For MH_SPLIT_SEGS images the r_address is relative to the 1257 * first read-write segment and there are no relocation entries 1258 * allowed in the read-only segments. This is needed because 1259 * the r_address field in a scattered relocation entry is 24 1260 * bits which means that the normal split of 265meg wouldn't 1261 * allow the use of 24 bits from the address of the first 1262 * segment which is what is normally used for outputs for dyld. 1263 */ 1264 if(segs_read_only_addr_specified == TRUE) 1265 r_address = ms->s.addr - segs_read_write_addr; 1266 else 1267 r_address = ms->s.addr - merged_segments->sg.vmaddr; 1268 } 1269 else{ 1270 extreloc = NULL; 1271 reloc = (struct relocation_info *)(output_addr + ms->s.reloff); 1272 sreloc = (struct scattered_relocation_info *)reloc; 1273 r_address = 0; 1274 } 1275 1276 for(p = &(data->literal_pointer_blocks); 1277 *p ; 1278 p = &(literal_pointer_block->next)){ 1279 1280 literal_pointer_block = *p; 1281 literal_pointers = literal_pointer_block->literal_pointers; 1282 for(i = 0; i < literal_pointer_block->used; i++){ 1283 /* 1284 * If the pointer is made up from an undefined merged 1285 * symbol and external relocation entry is created. 1286 */ 1287 if(literal_pointers[i].merged_symbol != NULL){ 1288 if(output_for_dyld) 1289 r = extreloc; 1290 else 1291 r = reloc; 1292 r->r_address = r_address; 1293 r->r_symbolnum = 1294 merged_symbol_output_index( 1295 literal_pointers[i].merged_symbol); 1296 r->r_pcrel = 0; 1297 r->r_length = 2; 1298 r->r_extern = 1; 1299 r->r_type = 0; 1300 if(output_for_dyld) 1301 extreloc++; 1302 else 1303 reloc++; 1304 } 1305 /* 1306 * For an file with a dynamic linker load command only 1307 * external relocation entries for undefined symbols are 1308 * kept. Which are handled above. So if this file has 1309 * a dynamic linker load command the remaining relocation 1310 * entries are local and not kept in the output. 1311 */ 1312 else if(has_dynamic_linker_command){ 1313 continue; 1314 } 1315 /* 1316 * If the offset added to the item to be relocated is 1317 * zero then local relocation entry is created. 1318 */ 1319 else if(literal_pointers[i].offset == 0){ 1320 reloc->r_address = r_address; 1321 reloc->r_symbolnum = 1322 literal_pointers[i].literal_ms->output_sectnum; 1323 reloc->r_pcrel = 0; 1324 reloc->r_length = 2; 1325 reloc->r_extern = 0; 1326 reloc->r_type = 0; 1327 reloc++; 1328 sreloc++; 1329 } 1330 /* 1331 * The offset added to the item to be relocated is NOT 1332 * zero so a scattered relocation entry is created. 1333 */ 1334 else{ 1335 sreloc->r_scattered = 1; 1336 sreloc->r_pcrel = 0; 1337 sreloc->r_length = 2; 1338 sreloc->r_type = 0; 1339 sreloc->r_address = r_address; 1340 sreloc->r_value = 1341 literal_pointers[i].literal_ms->s.addr + 1342 literal_pointers[i].merged_section_offset; 1343 reloc++; 1344 sreloc++; 1345 } 1346 r_address += 4; 1347 } 1348 } 1349 if(output_for_dyld){ 1350 if(host_byte_sex != target_byte_sex){ 1351 swap_relocation_info((struct relocation_info *) 1352 (output_addr + 1353 output_dysymtab_info.dysymtab_command.extreloff + 1354 ms->iextrel * sizeof(struct relocation_info)), 1355 ms->nextrel, target_byte_sex); 1356 swap_relocation_info((struct relocation_info *) 1357 (output_addr + 1358 output_dysymtab_info.dysymtab_command.locreloff + 1359 ms->ilocrel * sizeof(struct relocation_info)), 1360 ms->nlocrel, target_byte_sex); 1361 } 1362 output_flush(output_dysymtab_info.dysymtab_command.extreloff + 1363 ms->iextrel * sizeof(struct relocation_info), 1364 ms->nextrel * sizeof(struct relocation_info)); 1365 output_flush(output_dysymtab_info.dysymtab_command.locreloff + 1366 ms->ilocrel * sizeof(struct relocation_info), 1367 ms->nlocrel * sizeof(struct relocation_info)); 1368 } 1369 else{ 1370 if(host_byte_sex != target_byte_sex) 1371 swap_relocation_info((struct relocation_info *) 1372 (output_addr + ms->s.reloff), ms->s.nreloc, 1373 target_byte_sex); 1374 output_flush(ms->s.reloff, 1375 ms->s.nreloc * sizeof(struct relocation_info)); 1376 } 1377 } 1378#endif /* !defined(RLD) */ 1379 1380 literal_pointer_free(data); 1381} 1382 1383/* 1384 * literal_pointer_free() free()'s up all space used by the data block except 1385 * the data block itself. 1386 */ 1387__private_extern__ 1388void 1389literal_pointer_free( 1390struct literal_pointer_data *data) 1391{ 1392 unsigned long i; 1393 struct literal_pointer_bucket *bp, *next_bp; 1394 struct literal_pointer_block *literal_pointer_block, 1395 *next_literal_pointer_block; 1396 1397 /* 1398 * Free all data for this block. 1399 */ 1400 if(data->hashtable != NULL){ 1401 for(i = 0; i < LITERAL_POINTER_HASHSIZE; i++){ 1402 for(bp = data->hashtable[i]; bp; ){ 1403 next_bp = bp->next; 1404 free(bp); 1405 bp = next_bp; 1406 } 1407 } 1408 free(data->hashtable); 1409 data->hashtable = NULL; 1410 } 1411 for(literal_pointer_block = data->literal_pointer_blocks; 1412 literal_pointer_block; 1413 literal_pointer_block = next_literal_pointer_block){ 1414 1415 next_literal_pointer_block = literal_pointer_block->next; 1416 free(literal_pointer_block); 1417 } 1418 data->literal_pointer_blocks = NULL; 1419} 1420 1421#ifdef DEBUG 1422/* 1423 * print_literal_pointer_data() prints a literal_pointer_data. Used for 1424 * debugging. 1425 */ 1426__private_extern__ 1427void 1428print_literal_pointer_data( 1429struct literal_pointer_data *data, 1430char *indent) 1431{ 1432 unsigned long i; 1433 struct literal_pointer_block **p, *literal_pointer_block; 1434 struct literal_pointer *literal_pointers; 1435 1436 print("%sliteral pointer data at 0x%x\n", indent, (unsigned int)data); 1437 if(data == NULL) 1438 return; 1439 for(p = &(data->literal_pointer_blocks); 1440 *p ; 1441 p = &(literal_pointer_block->next)){ 1442 1443 literal_pointer_block = *p; 1444 literal_pointers = literal_pointer_block->literal_pointers; 1445 print("%sused %lu\n", indent, literal_pointer_block->used); 1446 for(i = 0; i < literal_pointer_block->used; i++){ 1447 if(literal_pointers[i].merged_symbol != NULL){ 1448 print("%s symbol %s offset %lu\n", indent, 1449 literal_pointers[i].merged_symbol->nlist.n_un.n_name, 1450 literal_pointers[i].offset); 1451 } 1452 else{ 1453 print("%s section (%.16s,%.16s) section_offset %lu " 1454 "offset %lu\n", indent, 1455 literal_pointers[i].literal_ms->s.segname, 1456 literal_pointers[i].literal_ms->s.sectname, 1457 literal_pointers[i].merged_section_offset, 1458 literal_pointers[i].offset); 1459 } 1460 } 1461 } 1462} 1463 1464/* 1465 * literal_pointer_data_stats() prints the literal_pointer_data stats. Used for 1466 * tuning. 1467 */ 1468__private_extern__ 1469void 1470literal_pointer_data_stats( 1471struct literal_pointer_data *data, 1472struct merged_section *ms) 1473{ 1474 if(data == NULL) 1475 return; 1476 print("literal pointer section (%.16s,%.16s) contains:\n", 1477 ms->s.segname, ms->s.sectname); 1478 print(" %u merged literal pointers\n", ms->s.size / 4); 1479 print(" from %lu files and %lu total literal pointers from those " 1480 "files\n", data->nfiles, data->nliterals); 1481 print(" average number of literals per file %g\n", 1482 (double)((double)data->nliterals / (double)(data->nfiles))); 1483 if(data->nprobes != 0){ 1484 print(" average number of hash probes %g\n", 1485 (double)((double)data->nprobes / (double)(data->nliterals))); 1486 } 1487} 1488#endif /* DEBUG */ 1489