Loading...
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
/*
 * Copyright (c) 2008 Apple Inc. All rights reserved.
 *
 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
 *
 * This file contains Original Code and/or Modifications of Original Code
 * as defined in and that are subject to the Apple Public Source License
 * Version 2.0 (the 'License'). You may not use this file except in
 * compliance with the License. The rights granted to you under the License
 * may not be used to create, or enable the creation or redistribution of,
 * unlawful or unlicensed copies of an Apple operating system, or to
 * circumvent, violate, or enable the circumvention or violation of, any
 * terms of an Apple operating system software license agreement.
 *
 * Please obtain a copy of the License at
 * http://www.opensource.apple.com/apsl/ and read it before using this file.
 *
 * The Original Code and all software distributed under the License are
 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
 * Please see the License for the specific language governing rights and
 * limitations under the License.
 *
 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
 */
#include <string.h>
#include <mach-o/loader.h>
#include <mach-o/reloc.h>
#include <sys/types.h>

#define DEBUG_ASSERT_COMPONENT_NAME_STRING "kxld"
#include <AssertMacros.h>

#include "kxld_reloc.h"
#include "kxld_sect.h"
#include "kxld_seg.h"
#include "kxld_symtab.h"
#include "kxld_util.h"

static kern_return_t export_macho(const KXLDSect *sect, u_char *buf, u_long offset,
    u_long bufsize);
#if KXLD_USER_OR_ILP32
static kern_return_t sect_export_macho_header_32(const KXLDSect *sect, u_char *buf,
    u_long *header_offset, u_long header_size, u_long data_offset);
#endif
#if KXLD_USER_OR_LP64
static kern_return_t sect_export_macho_header_64(const KXLDSect *sect, u_char *buf,
    u_long *header_offset, u_long header_size, u_long data_offset);
#endif
extern boolean_t isSplitKext;

#if KXLD_USER_OR_ILP32
/*******************************************************************************
*******************************************************************************/
kern_return_t
kxld_sect_init_from_macho_32(KXLDSect *sect, u_char *macho, u_long *sect_offset,
    u_int sectnum, const KXLDRelocator *relocator)
{
	kern_return_t rval = KERN_FAILURE;
	struct section *src = (struct section *) ((void *) (macho + *sect_offset));
	struct relocation_info *relocs = NULL;

	check(sect);
	check(macho);
	check(src);

	strlcpy(sect->segname, src->segname, sizeof(sect->segname));
	strlcpy(sect->sectname, src->sectname, sizeof(sect->sectname));
	sect->base_addr = src->addr;
	sect->link_addr = src->addr;
	sect->size = src->size;
	sect->sectnum = sectnum;
	sect->flags = src->flags;
	sect->align = src->align;
	sect->reserved1 = src->reserved1;
	sect->reserved2 = src->reserved2;

	if (src->offset) {
		sect->data = macho + src->offset;
	} else {
		sect->data = NULL;
	}

	relocs = (struct relocation_info *) ((void *) (macho + src->reloff));

	rval = kxld_reloc_create_macho(&sect->relocs, relocator,
	    relocs, src->nreloc);
	require_noerr(rval, finish);

	*sect_offset += sizeof(*src);
	rval = KERN_SUCCESS;

finish:
	if (rval) {
		kxld_sect_deinit(sect);
	}

	return rval;
}
#endif /* KXLD_USER_OR_ILP32 */

#if KXLD_USER_OR_LP64
/*******************************************************************************
*******************************************************************************/
kern_return_t
kxld_sect_init_from_macho_64(KXLDSect *sect, u_char *macho, u_long *sect_offset,
    u_int sectnum, const KXLDRelocator *relocator)
{
	kern_return_t rval = KERN_FAILURE;
	struct section_64 *src = (struct section_64 *) ((void *) (macho + *sect_offset));
	struct relocation_info *relocs = NULL;

	check(sect);
	check(macho);
	check(src);

	strlcpy(sect->segname, src->segname, sizeof(sect->segname));
	strlcpy(sect->sectname, src->sectname, sizeof(sect->sectname));
	sect->base_addr = src->addr;
	sect->link_addr = src->addr;
	sect->size = src->size;
	sect->sectnum = sectnum;
	sect->flags = src->flags;
	sect->align = src->align;
	sect->reserved1 = src->reserved1;
	sect->reserved2 = src->reserved2;

	if (src->offset) {
		sect->data = macho + src->offset;
	} else {
		sect->data = NULL;
	}

	relocs = (struct relocation_info *) ((void *) (macho + src->reloff));

	rval = kxld_reloc_create_macho(&sect->relocs, relocator,
	    relocs, src->nreloc);
	require_noerr(rval, finish);

	*sect_offset += sizeof(*src);
	rval = KERN_SUCCESS;

finish:
	if (rval) {
		kxld_sect_deinit(sect);
	}

	return rval;
}
#endif /* KXLD_USER_OR_LP64 */

#if KXLD_USER_OR_GOT
/*******************************************************************************
* Assumes GOT is comprised of kxld_addr_t entries
*******************************************************************************/
kern_return_t
kxld_sect_init_got(KXLDSect *sect, u_int ngots)
{
	kern_return_t rval = KERN_FAILURE;

	check(sect);

	strlcpy(sect->segname, KXLD_SEG_GOT, sizeof(sect->segname));
	strlcpy(sect->sectname, KXLD_SECT_GOT, sizeof(sect->sectname));
	sect->base_addr = 0;
	sect->link_addr = 0;
	sect->flags = 0;
	sect->align = 4;
	sect->reserved1 = 0;
	sect->reserved2 = 0;

	sect->size = ngots * sizeof(kxld_addr_t);
	sect->data = kxld_alloc((u_long) sect->size);
	require_action(sect->data, finish, rval = KERN_RESOURCE_SHORTAGE);

	sect->allocated = TRUE;

	rval = KERN_SUCCESS;

finish:
	return rval;
}
#endif /* KXLD_USER_OR_GOT */

#if KXLD_USER_OR_COMMON
/*******************************************************************************
*******************************************************************************/
void
kxld_sect_init_zerofill(KXLDSect *sect, const char *segname,
    const char *sectname, kxld_size_t size, u_int align)
{
	check(sect);
	check(segname);
	check(sectname);

	strlcpy(sect->segname, segname, sizeof(sect->segname));
	strlcpy(sect->sectname, sectname, sizeof(sect->sectname));
	sect->size = size;
	sect->align = align;
	sect->base_addr = 0;
	sect->link_addr = 0;
	sect->flags = S_ZEROFILL;
}
#endif /* KXLD_USER_OR_COMMON */

/*******************************************************************************
*******************************************************************************/
void
kxld_sect_clear(KXLDSect *sect)
{
	check(sect);

	if (sect->allocated) {
		kxld_free(sect->data, (u_long) sect->size);
		sect->allocated = FALSE;
	}

	bzero(sect->sectname, sizeof(sect->sectname));
	bzero(sect->segname, sizeof(sect->segname));
	sect->data = NULL;
	sect->base_addr = 0;
	sect->link_addr = 0;
	sect->size = 0;
	sect->flags = 0;
	sect->align = 0;
	sect->reserved1 = 0;
	sect->reserved2 = 0;
	kxld_array_clear(&sect->relocs);
}

/*******************************************************************************
*******************************************************************************/
void
kxld_sect_deinit(KXLDSect *sect)
{
	check(sect);

	if (streq_safe(sect->sectname, KXLD_SECT_GOT, sizeof(KXLD_SECT_GOT))) {
		kxld_free(sect->data, (u_long) sect->size);
	}

	kxld_array_deinit(&sect->relocs);
	bzero(sect, sizeof(*sect));
}

/*******************************************************************************
*******************************************************************************/
u_int
kxld_sect_get_num_relocs(const KXLDSect *sect)
{
	check(sect);

	return sect->relocs.nitems;
}

/*******************************************************************************
*******************************************************************************/
u_long
kxld_sect_get_macho_header_size(boolean_t is_32_bit)
{
	if (is_32_bit) {
		return sizeof(struct section);
	} else {
		return sizeof(struct section_64);
	}
}

/*******************************************************************************
*******************************************************************************/
u_long
kxld_sect_get_macho_data_size(const KXLDSect *sect)
{
	u_long size = 0;

	check(sect);

	if (sect->data) {
		size = (u_long) sect->size;
	}

	return size;
}

#if KXLD_USER_OR_GOT
/*******************************************************************************
*******************************************************************************/
u_int
kxld_sect_get_ngots(const KXLDSect *sect, const KXLDRelocator *relocator,
    const KXLDSymtab *symtab)
{
	const KXLDReloc *reloc = NULL;
	KXLDSym *sym = NULL;
	u_int ngots = 0;
	u_int i = 0;

	for (i = 0; i < sect->relocs.nitems; ++i) {
		reloc = kxld_array_get_item(&sect->relocs, i);

		if (relocator->reloc_has_got(reloc->reloc_type)) {
			/* @TODO This assumes 64-bit symbols (which is valid at the
			 * moment since only x86_64 has a GOT)
			 */
			sym = kxld_reloc_get_symbol(relocator, reloc, sect->data, symtab);
			if (!kxld_sym_is_got(sym)) {
				kxld_sym_set_got(sym);
				++ngots;
			}
		}
	}

	return ngots;
}
#endif /* KXLD_USER_OR_GOT */

/*******************************************************************************
* Each section must be aligned at a certain power of two.  To figure out that
* alignment, we mask for the low bits that may need to be adjusted.  If they are
* non zero, we then subtract them from the target alignment to find the offset,
* and then add that offset to the link address.
*******************************************************************************/
kxld_addr_t
kxld_sect_align_address(const KXLDSect *sect, kxld_addr_t address)
{
	return kxld_align_address(address, sect->align);
}

/*******************************************************************************
*******************************************************************************/
kern_return_t
kxld_sect_export_macho_to_file_buffer(const KXLDSect *sect, u_char *buf,
    u_long *header_offset, u_long header_size, u_long *data_offset,
    u_long data_size, boolean_t is_32_bit __unused)
{
	kern_return_t rval = KERN_FAILURE;

	check(sect);
	check(buf);
	check(header_offset);
	check(data_offset);

	/* If there is no data to export, we only need to write the header.  We
	 * make it a separate call so that we don't modify data_offset.
	 */
	if (!sect->data) {
		KXLD_3264_FUNC(is_32_bit, rval,
		    sect_export_macho_header_32, sect_export_macho_header_64,
		    sect, buf, header_offset, header_size, /* data_offset */ 0);
		require_noerr(rval, finish);
	} else {
		*data_offset = (u_long) kxld_sect_align_address(sect, *data_offset);

		KXLD_3264_FUNC(is_32_bit, rval,
		    sect_export_macho_header_32, sect_export_macho_header_64,
		    sect, buf, header_offset, header_size, *data_offset);
		require_noerr(rval, finish);

		rval = export_macho(sect, buf, *data_offset, data_size);
		require_noerr(rval, finish);

		*data_offset += (u_long) sect->size;
	}
	rval = KERN_SUCCESS;

finish:
	return rval;
}

/*******************************************************************************
*******************************************************************************/
kern_return_t
kxld_sect_export_macho_to_vm(const KXLDSect *sect,
    u_char *buf,
    u_long *header_offset,
    u_long header_size,
    kxld_addr_t link_addr,
    u_long data_size,
    boolean_t is_32_bit __unused)
{
	kern_return_t rval = KERN_FAILURE;
	u_long data_offset;

	check(sect);
	check(buf);
	check(header_offset);

	data_offset = (u_long) (sect->link_addr - link_addr);

	KXLD_3264_FUNC(is_32_bit, rval,
	    sect_export_macho_header_32, sect_export_macho_header_64,
	    sect, buf, header_offset, header_size, data_offset);
	require_noerr(rval, finish);

	rval = export_macho(sect, buf, data_offset, data_size);
	require_noerr(rval, finish);

	rval = KERN_SUCCESS;

finish:
	return rval;
}

/*******************************************************************************
*******************************************************************************/
static kern_return_t
export_macho(const KXLDSect *sect, u_char *buf, u_long offset, u_long bufsize)
{
	kern_return_t rval = KERN_FAILURE;

	check(sect);
	check(buf);

	if (!sect->data) {
		rval = KERN_SUCCESS;
		goto finish;
	}

	if (!isSplitKext) {
		/* Verify that the section is properly aligned */
		if (kxld_sect_align_address(sect, offset) != offset) {
			kxld_log(kKxldLogLinking, kKxldLogErr, kKxldLogMalformedMachO
			    "Alignment error: %llu != %lu for %s %s <%s>",
			    kxld_sect_align_address(sect, offset), offset,
			    sect->segname, sect->sectname, __func__);
			goto finish;
		}
	}

	/* Verify that we have enough space to copy */
	if (buf + offset + sect->size > buf + bufsize) {
		kxld_log(kKxldLogLinking, kKxldLogErr, kKxldLogMalformedMachO
		    "Overflow: offset %lu + sect->size %llu > bufsize %lu for %s %s",
		    offset, sect->size, bufsize,
		    sect->segname, sect->sectname);
		goto finish;
	}

	/* Copy section data */
	switch (sect->flags & SECTION_TYPE) {
	case S_NON_LAZY_SYMBOL_POINTERS:
	case S_MOD_INIT_FUNC_POINTERS:
	case S_MOD_TERM_FUNC_POINTERS:
	case S_REGULAR:
	case S_CSTRING_LITERALS:
	case S_4BYTE_LITERALS:
	case S_8BYTE_LITERALS:
	case S_LITERAL_POINTERS:
	case S_COALESCED:
	case S_16BYTE_LITERALS:
	case S_SYMBOL_STUBS:
#if SPLIT_KEXTS_DEBUG
		kxld_log(kKxldLogLinking, kKxldLogErr,
		    " sectname %s copy from %p (sect->data) for %llu bytes (sect->size) to %p (buf %p + offset %lu <%s>",
		    sect->sectname[0] ? sect->sectname : "none",
		    (void *) sect->data,
		    sect->size,
		    (void *) (buf + offset),
		    (void *) buf,
		    offset,
		    __func__);

		kxld_log(kKxldLogLinking, kKxldLogErr,
		    " %p >>> Start of %s section data (sect->size %llu) <%s>",
		    (void *) (buf + offset),
		    sect->sectname[0] ? sect->sectname : "none",
		    sect->size,
		    __func__);
		kxld_log(kKxldLogLinking, kKxldLogErr,
		    " %p <<< End of %s section data <%s>",
		    (void *) (buf + offset + sect->size),
		    sect->sectname[0] ? sect->sectname : "none",
		    __func__);
#endif
		memcpy(buf + offset, sect->data, (size_t)sect->size);
		break;
	case S_ZEROFILL: /* sect->data should be NULL, so we'll never get here */
	case S_LAZY_SYMBOL_POINTERS:
	case S_GB_ZEROFILL:
	case S_INTERPOSING:
	case S_DTRACE_DOF:
	default:
		rval = KERN_FAILURE;
		kxld_log(kKxldLogLinking, kKxldLogErr, kKxldLogMalformedMachO
		    "Invalid section type: %u.", sect->flags & SECTION_TYPE);
		goto finish;
	}

	rval = KERN_SUCCESS;

finish:
	return rval;
}

#if KXLD_USER_OR_ILP32
/*******************************************************************************
*******************************************************************************/
static kern_return_t
sect_export_macho_header_32(const KXLDSect *sect, u_char *buf,
    u_long *header_offset, u_long header_size, u_long data_offset)
{
	kern_return_t rval = KERN_FAILURE;
	struct section *secthdr = NULL;

	check(sect);
	check(buf);
	check(header_offset);

	require_action(sizeof(*secthdr) <= header_size - *header_offset, finish,
	    rval = KERN_FAILURE);
	secthdr = (struct section *) ((void *) (buf + *header_offset));
	*header_offset += sizeof(*secthdr);

	/* Initalize header */

	strlcpy(secthdr->sectname, sect->sectname, sizeof(secthdr->sectname));
	strlcpy(secthdr->segname, sect->segname, sizeof(secthdr->segname));
	secthdr->addr = (uint32_t) sect->link_addr;
	secthdr->size = (uint32_t) sect->size;
	secthdr->offset = (uint32_t) ((sect->data) ? data_offset : 0);
	secthdr->align = sect->align;
	secthdr->reloff = 0;
	secthdr->nreloc = 0;
	secthdr->flags = sect->flags;
	secthdr->reserved1 = sect->reserved1;
	secthdr->reserved2 = sect->reserved2;

#if SPLIT_KEXTS_DEBUG
	{
		kxld_log(kKxldLogLinking, kKxldLogErr,
		    "sectname %s secthdr: %p addr %p size %02X %u offset %02X %u <%s>",
		    sect->sectname[0] ? sect->sectname : "none",
		    (void *) secthdr,
		    (void *) ((uint64_t)secthdr->addr),
		    secthdr->size,
		    secthdr->size,
		    secthdr->offset,
		    secthdr->offset,
		    __func__);
	}
#endif

	rval = KERN_SUCCESS;

finish:
	return rval;
}
#endif /* KXLD_USER_OR_ILP32 */

#if KXLD_USER_OR_LP64
/*******************************************************************************
*******************************************************************************/
static kern_return_t
sect_export_macho_header_64(const KXLDSect *sect, u_char *buf,
    u_long *header_offset, u_long header_size, u_long data_offset)
{
	kern_return_t rval = KERN_FAILURE;
	struct section_64 *secthdr = NULL;

	check(sect);
	check(buf);
	check(header_offset);


	require_action(sizeof(*secthdr) <= header_size - *header_offset, finish,
	    rval = KERN_FAILURE);
	secthdr = (struct section_64 *) ((void *) (buf + *header_offset));
	*header_offset += sizeof(*secthdr);

	/* Initalize header */

	strlcpy(secthdr->sectname, sect->sectname, sizeof(secthdr->sectname));
	strlcpy(secthdr->segname, sect->segname, sizeof(secthdr->segname));
	secthdr->addr = (uint64_t) sect->link_addr;
	secthdr->size = (uint64_t) sect->size;
	secthdr->offset = (uint32_t) ((sect->data) ? data_offset : 0);
	secthdr->align = sect->align;
	secthdr->reloff = 0;
	secthdr->nreloc = 0;
	secthdr->flags = sect->flags;
	secthdr->reserved1 = sect->reserved1;
	secthdr->reserved2 = sect->reserved2;

#if SPLIT_KEXTS_DEBUG
	kxld_log(kKxldLogLinking, kKxldLogErr,
	    " %p >>> Start of %s secthdr (size %lu) <%s>",
	    (void *) secthdr,
	    sect->sectname[0] ? sect->sectname : "none",
	    sizeof(*secthdr),
	    __func__);
	kxld_log(kKxldLogLinking, kKxldLogErr,
	    " %p <<< End of %s secthdr <%s>",
	    (void *) ((u_char *)secthdr + sizeof(*secthdr)),
	    sect->sectname[0] ? sect->sectname : "none",
	    __func__);
	kxld_log(kKxldLogLinking, kKxldLogErr,
	    " secthdr: addr %p size %llu offset %u sectname %s <%s>",
	    (void *) secthdr->addr,
	    secthdr->size,
	    secthdr->offset,
	    sect->sectname[0] ? sect->sectname : "none",
	    __func__);
#endif

	rval = KERN_SUCCESS;

finish:
	return rval;
}
#endif /* KXLD_USER_OR_LP64 */

#if KXLD_USER_OR_COMMON
/*******************************************************************************
*******************************************************************************/
kxld_size_t
kxld_sect_grow(KXLDSect *sect, kxld_size_t nbytes, u_int align)
{
	kxld_size_t size = kxld_align_address(sect->size, align);

	if (align > sect->align) {
		sect->align = align;
	}
	sect->size = size + nbytes;

	return size;
}
#endif /* KXLD_USER_OR_COMMON */

/*******************************************************************************
*******************************************************************************/
void
kxld_sect_relocate(KXLDSect *sect, kxld_addr_t link_addr)
{
#if SPLIT_KEXTS_DEBUG
	{
		kxld_log(kKxldLogLinking, kKxldLogErr,
		    "%p >>> Start of %s section (sect->size %llu) <%s>",
		    (void *) (kxld_sect_align_address(sect, sect->link_addr + link_addr)),
		    sect->sectname[0] ? sect->sectname : "none",
		    sect->size,
		    __func__);
		kxld_log(kKxldLogLinking, kKxldLogErr,
		    "%p <<< End of %s section <%s>",
		    (void *) (kxld_sect_align_address(sect, sect->link_addr + link_addr) + sect->size),
		    sect->sectname[0] ? sect->sectname : "none",
		    __func__);
	}
#endif

	sect->link_addr = kxld_sect_align_address(sect,
	    sect->link_addr + link_addr);
}

#if KXLD_USER_OR_GOT
/*******************************************************************************
*******************************************************************************/
kern_return_t
kxld_sect_populate_got(KXLDSect *sect, KXLDSymtab *symtab,
    boolean_t swap __unused)
{
	kern_return_t rval = KERN_FAILURE;
	KXLDSymtabIterator iter;
	KXLDSym *sym = NULL;
	kxld_addr_t *entry = NULL;
	kxld_addr_t entry_addr = 0;

	check(sect);
	check(symtab);
	require(streq_safe(sect->segname, KXLD_SEG_GOT, sizeof(KXLD_SEG_GOT)),
	    finish);
	require(streq_safe(sect->sectname, KXLD_SECT_GOT, sizeof(KXLD_SECT_GOT)),
	    finish);

	kxld_symtab_iterator_init(&iter, symtab, kxld_sym_is_got, FALSE);

	entry = (kxld_addr_t *) sect->data;
	entry_addr = sect->link_addr;
	while ((sym = kxld_symtab_iterator_get_next(&iter))) {
		*entry = sym->link_addr;
		sym->got_addr = entry_addr;

#if !KERNEL
		if (swap) {
			*entry = OSSwapInt64(*entry);
		}
#endif /* !KERNEL */

		++entry;
		entry_addr += sizeof(*entry);
	}

	rval = KERN_SUCCESS;

finish:
	return rval;
}
#endif /* KXLD_USER_OR_GOT */

/*******************************************************************************
*******************************************************************************/
kern_return_t
kxld_sect_process_relocs(KXLDSect *sect, KXLDRelocator *relocator)
{
	kern_return_t rval = KERN_FAILURE;
	KXLDReloc *reloc = NULL;
	u_int i = 0;

	for (i = 0; i < sect->relocs.nitems; ++i) {
		reloc = kxld_array_get_item(&sect->relocs, i);
		rval = kxld_relocator_process_sect_reloc(relocator, reloc, sect);
		require_noerr(rval, finish);
	}

	rval = KERN_SUCCESS;
finish:
	return rval;
}