-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathdyld_bind_info.c
3952 lines (3713 loc) · 151 KB
/
dyld_bind_info.c
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
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <mach-o/loader.h>
#include <stuff/bool.h>
#include <stuff/allocate.h>
#include <stuff/bytesex.h>
#include "dyld_bind_info.h"
#include "fixup-chains.h"
#include "stuff/guess_short_name.h"
/*****************************************************************************
*
* LC_DYLD_INFO, LC_DYLD_INFO_ONLY
*
*/
/* The entries of the ordinalTable for ThreadedRebaseBind. */
struct ThreadedBindData {
const char* symbolName;
int64_t addend;
int libraryOrdinal;
uint8_t flags;
uint8_t type;
};
const char *
bindTypeName(
uint8_t type)
{
switch(type){
case BIND_TYPE_POINTER:
return("pointer");
case BIND_TYPE_TEXT_ABSOLUTE32:
return("text abs32");
case BIND_TYPE_TEXT_PCREL32:
return("text rel32");
}
return("!!Unknown!!");
}
/*
static
const char *
ordinalName(
int libraryOrdinal,
const char **dylibs,
uint32_t ndylibs,
enum bool *libraryOrdinalSet)
{
*libraryOrdinalSet = TRUE;
switch(libraryOrdinal){
case BIND_SPECIAL_DYLIB_SELF:
return("this-image");
case BIND_SPECIAL_DYLIB_MAIN_EXECUTABLE:
return("main-executable");
case BIND_SPECIAL_DYLIB_FLAT_LOOKUP:
return("flat-namespace");
case BIND_SPECIAL_DYLIB_WEAK_LOOKUP:
return("weak");
}
if(libraryOrdinal < BIND_SPECIAL_DYLIB_WEAK_LOOKUP){
*libraryOrdinalSet = FALSE;
return("Unknown special ordinal");
}
if(libraryOrdinal > ndylibs){
*libraryOrdinalSet = FALSE;
return("LibraryOrdinal out of range");
}
return(dylibs[libraryOrdinal-1]);
}
*/
/*
* validateOrdinal validates a dylibOrdinal value as read from the LC_DYLD_INFO
* opcodes. If dylibOrdinal is valid, validateOrdinal will return TRUE and set
* dylibName to either the name of a dylib or to a human-readable special name.
* If dylibOrdinal is not valid, it will return FALSE and set error to a
* human-readable error string. error may be NULL.
*/
static
enum bool
validateOrdinal(
int dylibOrdinal,
const char **dylibs,
uint32_t ndylibs,
const char** dylibName,
const char** error)
{
*dylibName = NULL;
if (error)
*error = NULL;
if (dylibOrdinal < BIND_SPECIAL_DYLIB_WEAK_LOOKUP){
if (error)
*error = "Unknown special ordinal";
return FALSE;
}
if (dylibOrdinal > 0 && dylibOrdinal > ndylibs){
if (error)
*error = "dylibOrdinal out of range";
return FALSE;
}
switch(dylibOrdinal){
case BIND_SPECIAL_DYLIB_SELF:
*dylibName = "this-image";
break;
case BIND_SPECIAL_DYLIB_MAIN_EXECUTABLE:
*dylibName = "main-executable";
break;
case BIND_SPECIAL_DYLIB_FLAT_LOOKUP:
*dylibName = "flat-namespace";
break;
case BIND_SPECIAL_DYLIB_WEAK_LOOKUP:
*dylibName = "weak";
break;
default:
*dylibName = dylibs[dylibOrdinal-1];
}
return TRUE;
}
/*
* validateSegIndex validates a segIndex value as read from the LC_DYLD_INFO
* opcodes. If segIndex is in range of the segs64 or segs arrays,
* validateSegIndex will return TRUE. Otherwise, it will return FALSE and set
* error to a human-readable error string.
*/
static
enum bool
validateSegIndex(
uint8_t segIndex,
struct segment_command **segs,
uint32_t nsegs,
struct segment_command_64 **segs64,
uint32_t nsegs64,
const char** error)
{
*error = NULL;
if(segs64 != NULL && segIndex >= nsegs64) {
*error = "segment index out of range";
return FALSE;
}
if(segs != NULL && segIndex >= nsegs) {
*error = "segment index out of range";
return FALSE;
}
return TRUE;
}
uint64_t
segStartAddress(
uint8_t segIndex,
struct segment_command **segs,
uint32_t nsegs,
struct segment_command_64 **segs64,
uint32_t nsegs64)
{
if(segs != NULL){
if(segIndex >= nsegs)
return(0); /* throw "segment index out of range"; */
return(segs[segIndex]->vmaddr);
}
else if(segs64 != NULL){
if(segIndex >= nsegs64)
return(0); /* throw "segment index out of range"; */
return(segs64[segIndex]->vmaddr);
}
return(0);
}
const char *
segmentName(
uint8_t segIndex,
struct segment_command **segs,
uint32_t nsegs,
struct segment_command_64 **segs64,
uint32_t nsegs64)
{
if(segs != NULL){
if(segIndex >= nsegs)
return("??"); /* throw "segment index out of range"; */
return(segs[segIndex]->segname);
}
else if(segs64 != NULL){
if(segIndex >= nsegs64)
return("??"); /* throw "segment index out of range"; */
return(segs64[segIndex]->segname);
}
return("??");
}
const char *
sectionName(
uint8_t segIndex,
uint64_t address,
struct segment_command **segs,
uint32_t nsegs,
struct segment_command_64 **segs64,
uint32_t nsegs64)
{
struct section *s;
struct section_64 *s64;
uint32_t i;
if(segs != NULL){
if(segIndex >= nsegs)
return("??"); /* throw "segment index out of range"; */
s = (struct section *)((char *)segs[segIndex] +
sizeof(struct segment_command));
for(i = 0; i < segs[segIndex]->nsects; i++){
if(s->addr <= address && address < s->addr + s->size)
return(s->sectname);
s++;
}
}
else if(segs64 != NULL){
if(segIndex >= nsegs64)
return("??"); /* throw "segment index out of range"; */
s64 = (struct section_64 *)((char *)segs64[segIndex] +
sizeof(struct segment_command_64));
for(i = 0; i < segs64[segIndex]->nsects; i++){
if(s64->addr <= address && address < s64->addr + s64->size)
return(s64->sectname);
s64++;
}
}
return("??");
}
/*
* segmentIndexFromAddr returns the index of a segment that contains the
* specified input address. The address is a vmaddr, not a fileoff. Canonical
* Mach-O files will include either 32-bit or 64-bit segments. If for some
* reason both 32- and 64-bit segments are passed in here, 64-bit segments will
* be consulted first; but really, you'll have other issues ...
*
* Earlier Mach-O structures encoded segment indicies directly. dyld3's
* chained fixups format saves space by ommitting this information, knowing
* that with a quick O(N) algorithm one can simply compute this information
* from a vmaddr.
*/
static uint32_t
segmentIndexFromAddr(uint64_t addr,
struct segment_command **segs,
uint32_t nsegs,
struct segment_command_64 **segs64,
uint32_t nsegs64)
{
for (uint32_t i = 0; i < nsegs64; ++i) {
if (segs64[i]->vmaddr <= addr &&
segs64[i]->vmaddr + segs64[i]->vmsize > addr)
return i;
}
for (uint32_t i = 0; i < nsegs; ++i) {
if (segs[i]->vmaddr <= addr &&
segs[i]->vmaddr + segs[i]->vmsize > addr)
return i;
}
return ((uint32_t)-1);
}
static uint64_t segmentFileOffset(
uint8_t segIndex,
struct segment_command **segs,
uint32_t nsegs,
struct segment_command_64 **segs64,
uint32_t nsegs64)
{
if (segIndex < nsegs64)
return segs64[segIndex]->fileoff;
if (segIndex < nsegs)
return segs[segIndex]->fileoff;
return -1;
}
const char *
checkSegAndOffset(
int segIndex,
uint64_t segOffset,
struct segment_command **segs,
uint32_t nsegs,
struct segment_command_64 **segs64,
uint32_t nsegs64,
enum bool endInvalid)
{
uint64_t address;
if(segIndex == -1)
return("missing preceding BIND_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB");
if(segs != NULL){
if(segIndex >= nsegs)
return("bad segIndex (too large)");
address = segs[segIndex]->vmaddr + segOffset;
if(address > segs[segIndex]->vmaddr + segs[segIndex]->vmsize)
return("bad segOffset, too large");
if(endInvalid == TRUE &&
address == segs[segIndex]->vmaddr + segs[segIndex]->vmsize)
return("bad segOffset, too large");
}
else if(segs64 != NULL){
if(segIndex >= nsegs64)
return("bad segIndex (too large)");
address = segs64[segIndex]->vmaddr + segOffset;
if(address > segs64[segIndex]->vmaddr + segs64[segIndex]->vmsize)
return("bad segOffset, too large");
if(endInvalid == TRUE &&
address == segs64[segIndex]->vmaddr + segs64[segIndex]->vmsize)
return("bad segOffset, too large");
}
return(NULL);
}
const char *
checkCountAndSkip(
uint32_t *count,
uint64_t skip,
int segIndex,
uint64_t segOffset,
struct segment_command **segs,
uint32_t nsegs,
struct segment_command_64 **segs64,
uint32_t nsegs64)
{
uint64_t address, i;
i = 0;
if(segs != NULL){
if(segIndex >= nsegs){
*count = 1;
return("bad segIndex (too large)");
}
if(*count > 1)
i = (skip + 4) * (*count - 1);
address = segs[segIndex]->vmaddr + segOffset;
if(address >= segs[segIndex]->vmaddr + segs[segIndex]->vmsize){
*count = 1;
return("bad segOffset, too large");
}
if(address + i >= segs[segIndex]->vmaddr + segs[segIndex]->vmsize){
*count = 1;
return("bad count and skip, too large");
}
}
else if(segs64 != NULL){
if(segIndex >= nsegs64){
*count = 1;
return("bad segIndex (too large)");
}
if(*count > 1)
i = (skip + 8) * (*count - 1);
address = segs64[segIndex]->vmaddr + segOffset;
if(address >= segs64[segIndex]->vmaddr + segs64[segIndex]->vmsize){
*count = 1;
return("bad segOffset, too large");
}
if(address + i >=
segs64[segIndex]->vmaddr + segs64[segIndex]->vmsize){
*count = 1;
return("bad count and skip, too large");
}
}
return(NULL);
}
static
uint64_t
read_uleb128(
const uint8_t **pp,
const uint8_t* end,
const char **error)
{
const uint8_t *p = *pp;
uint64_t result = 0;
int bit = 0;
*error = NULL;
do{
if(p >= end){
*pp = p;
*error = "malformed uleb128, extends past opcode bytes";
return(0);
}
uint64_t slice = *p & 0x7f;
if(bit >= 64 || slice << bit >> bit != slice){
*pp = p;
*error = "uleb128 too big for uint64";
return(0);
}
else {
result |= (slice << bit);
bit += 7;
}
}while(*p++ & 0x80);
*pp = p;
return(result);
}
static
int64_t
read_sleb128(
const uint8_t **pp,
const uint8_t* end,
const char **error)
{
const uint8_t *p = *pp;
int64_t result = 0;
int bit = 0;
uint8_t byte;
*error = NULL;
do{
if(p == end){
*pp = p;
*error = "malformed sleb128, extends past opcode bytes";
return(0);
}
byte = *p++;
result |= (((int64_t)(byte & 0x7f)) << bit);
bit += 7;
}while (byte & 0x80);
// sign extend negative numbers
if((byte & 0x40) != 0)
result |= (-1LL) << bit;
*pp = p;
return(result);
}
static inline
void
printErrorBind(
int pass,
enum bool verbose,
const char* opName,
unsigned long location,
const char* error,
uint32_t *errorCount)
{
if (1 == pass && verbose && error) {
printf("bad bind info (for %s opcode at 0x%lx) %s\n",
opName, location, error);
}
if (errorCount)
(*errorCount)++;
}
static inline
void
printErrorOrdinal(
int pass,
enum bool verbose,
const char* opName,
unsigned long location,
int32_t ordinal,
uint32_t ndylibs,
const char* error,
uint32_t *errorCount)
{
if (1 == pass && verbose && error) {
printf("bad bind info (for %s opcode at 0x%lx) bad library ordinal "
"%u (max %u): %s\n",
opName, location, ordinal, ndylibs, error);
}
if (errorCount)
(*errorCount)++;
}
static inline
void
printErrorOrdAddr(
int pass,
enum bool verbose,
const char* opName,
unsigned long location,
int32_t ordinal,
uint64_t ordAddr,
uint32_t *errorCount)
{
if (1 == pass && verbose) {
printf("bad bind info (for %s opcode at 0x%lx) bad ordinal: %u in "
"pointer at address 0x%llx\n",
opName, location, ordinal, ordAddr);
}
if (errorCount)
(*errorCount)++;
}
static inline
void
printErrorBindType(
int pass,
enum bool verbose,
const char* opName,
unsigned long location,
uint8_t bindType,
uint32_t *errorCount)
{
if (1 == pass && verbose) {
printf("bad bind info (for %s opcode at 0x%lx) "
"bad bind type: %u\n",
opName, location, bindType);
}
if (errorCount)
(*errorCount)++;
}
static inline
void
printErrorOffset(
int pass,
enum bool verbose,
const char* opName,
unsigned long location,
uint64_t address,
int reason,
uint32_t *errorCount)
{
if (1 == pass && verbose) {
const char* error = "invalid";
if (0 == reason)
error = "past end of file";
else if (1 == reason)
error = "past end of the same page";
printf("bad bind info (for %s opcode at: 0x%lx) offset to next pointer "
"in the chain after one at address 0x%llx is %s\n",
opName, location, address, error);
}
if (errorCount)
(*errorCount)++;
}
static inline
void
printErrorOpcode(
int pass,
enum bool verbose,
const char* opName,
unsigned long location,
uint8_t opcode,
uint32_t *errorCount)
{
if (1 == pass && verbose) {
if (opName)
printf("bad bind info (for %s opcode at 0x%lx) bad sub-opcode "
"value 0x%x\n",
opName, location, opcode);
else
printf("bad bind info (for opcode at 0x%lx) bad opcode "
"value 0x%x\n",
location, opcode);
}
if (errorCount)
(*errorCount)++;
}
static inline
void
printErrorFixupBindOrdinal(
uint64_t location,
uint32_t ordinal,
uint32_t count,
uint32_t *errorCount)
{
printf("bad chained fixups: bind 0x%llx target ordinal %d "
"exceeds target table size %d\n", location, ordinal, count);
if (errorCount)
(*errorCount)++;
}
/*
* get_dyld_bind_info() unpacks the dyld bind info from the data from an
* LC_BIND_INFO load command pointed to by start through end into the internal
* dyld_bind_info structs returned through dbi, and its count ndbi. The array
* of dylib names and their count are passed in in dylibs and ndylibs. The
* array of segments (either 32-bit with segs & nsegs or 64-bit segs64 & nsegs64)
* are used to determine which sections the pointers are in.
*
* A word about libraryOrdinal. The libraryOrdinal is a combination of array
* ordinals and special magic values:
*
* -3 BIND_SPECIAL_DYLIB_WEAK_LOOKUP
* -2 BIND_SPECIAL_DYLIB_FLAT_LOOKUP
* -1 BIND_SPECIAL_DYLIB_MAIN_EXECUTABLE
* 0 BIND_SPECIAL_DYLIB_SELF
* [1..n] library ordinal
*
* The libraryOrdinal is commonly used to index into a C-array (that is, in
* the range [0, n-1]) of dylibs. Ordinarily, once libraryOrdinal is assigned
* one should call ordinalName to validate the ordinal. ordinalName will either
* set libraryOrdinalSet to TRUE and return a human-readable library or special
* name, or it will set libraryOrdinalSet to FALSE and return a human-readable
* error message. While all this sounds perfectly reasonable, there is a
* potential for mischief here ...
*/
void
get_dyld_bind_info(
const uint8_t* start, /* inputs */
const uint8_t* end,
const char **dylibs,
uint32_t ndylibs,
struct segment_command **segs,
uint32_t nsegs,
struct segment_command_64 **segs64,
uint32_t nsegs64,
enum bool swapped,
char *object_addr,
uint64_t object_size,
struct dyld_bind_info **dbi, /* output */
uint64_t *ndbi,
enum chain_format_t *chain_format,
enum bool print_errors)
{
const uint8_t *p, *opcode_start;
uint8_t type;
int segIndex;
uint64_t segOffset;
const char* symbolName;
const char* fromDylib;
enum bool libraryOrdinalSet;
int libraryOrdinal;
int64_t addend;
int64_t flags;
uint32_t count;
uint32_t skip;
uint64_t segStartAddr;
const char* segName;
const char* weak_import;
enum bool done = FALSE;
const char *sectName, *error;
#define MAXERRORCOUNT 20
uint32_t pass, errorCount;
uint64_t n;
uint32_t sizeof_pointer;
char *pointerLocation;
uint64_t offset, ordinalTableCount, ordinalTableIndex, delta;
struct ThreadedBindData *ordinalTable;
uint16_t ordinal;
uint64_t pointerAddress, pointerPageStart;
const char* opName;
*chain_format = CHAIN_FORMAT_NONE;
*dbi = NULL;
*ndbi = 0;
if (start == NULL) {
printf("missing dyld bind info.\n");
return;
}
ordinalTable = NULL;
ordinalTableCount = 0;
sizeof_pointer = segs ? 4 : 8;
errorCount = 0;
n = 0;
for(pass = 1; pass <= 2; pass++){
p = start;
type = 0;
segIndex = -1;
segOffset = 0;
symbolName = NULL;
fromDylib = NULL;
libraryOrdinalSet = FALSE;
libraryOrdinal = 0;
addend = 0;
segStartAddr = 0;
segName = NULL;
sectName = NULL;
weak_import = "";
done = FALSE;
ordinalTableIndex = (uint64_t)-1;
flags = 0;
if(errorCount >= MAXERRORCOUNT){
if(print_errors)
printf("too many bind info errors, giving up.\n");
*dbi = NULL;
*ndbi = 0;
if(ordinalTable != NULL)
free(ordinalTable);
return;
}
if(pass == 2){
*dbi = (struct dyld_bind_info *)
allocate(n * sizeof(struct dyld_bind_info));
memset(*dbi, 0, n * sizeof(struct dyld_bind_info));
*ndbi = n;
n = 0;
}
while(!done && (p < end) && errorCount < MAXERRORCOUNT){
error = NULL;
opcode_start = p;
uint8_t immediate = *p & BIND_IMMEDIATE_MASK;
uint8_t opcode = *p & BIND_OPCODE_MASK;
opcode = *p & BIND_OPCODE_MASK;
++p;
switch(opcode){
case BIND_OPCODE_DONE:
done = TRUE;
break;
case BIND_OPCODE_SET_DYLIB_ORDINAL_IMM:
/*
* consume: immediate
* produce: libraryOrdinal, libraryOrdinalSet, fromDylib
*/
opName = "BIND_OPCODE_SET_DYLIB_ORDINAL_IMM";
libraryOrdinal = immediate;
libraryOrdinalSet = validateOrdinal(libraryOrdinal,
dylibs, ndylibs,
&fromDylib, &error);
if (FALSE == libraryOrdinalSet)
printErrorOrdinal(pass, print_errors, opName,
opcode_start - start,
libraryOrdinal, ndylibs,
error, &errorCount);
break;
case BIND_OPCODE_SET_DYLIB_ORDINAL_ULEB:
/*
* consume: p, end
* produce: libraryOrdinal, libraryOrdinalSet, fromDylib,
* error
*/
opName = "BIND_OPCODE_SET_DYLIB_ORDINAL_ULEB";
libraryOrdinal = (int)read_uleb128(&p, end, &error);
if (error) {
libraryOrdinalSet = FALSE;
}
else {
libraryOrdinalSet = validateOrdinal(libraryOrdinal,
dylibs, ndylibs,
&fromDylib, &error);
}
if (FALSE == libraryOrdinalSet)
printErrorOrdinal(pass, print_errors, opName,
opcode_start - start,
libraryOrdinal, ndylibs,
error, &errorCount);
break;
case BIND_OPCODE_SET_DYLIB_SPECIAL_IMM:
/*
* consume: immediate
* produce: libraryOrdinal, libraryOrdinalSet, fromDylib
*
* the special ordinals are in the range of [-3, 0]
*/
opName = "BIND_OPCODE_SET_DYLIB_SPECIAL_IMM";
if (immediate == 0) {
libraryOrdinal = 0;
}
else {
int8_t signExtended = BIND_OPCODE_MASK | immediate;
libraryOrdinal = signExtended;
}
libraryOrdinalSet = validateOrdinal(libraryOrdinal,
dylibs, ndylibs,
&fromDylib, &error);
if (FALSE == libraryOrdinalSet)
printErrorOrdinal(pass, print_errors, opName,
opcode_start - start,
libraryOrdinal, ndylibs,
error, &errorCount);
break;
case BIND_OPCODE_SET_SYMBOL_TRAILING_FLAGS_IMM:
/*
* consume: p, end, immediate
* produce: p, symbolName, flags, weak_import
*/
opName = "BIND_OPCODE_SET_SYMBOL_TRAILING_FLAGS_IMM";
weak_import = "";
symbolName = (char*)p;
while(*p != '\0' && (p < end))
++p;
if(p == end){
error = "symbol name extends past opcodes";
printErrorBind(pass, print_errors, opName,
opcode_start-start, error, &errorCount);
/*
* Even though the name does not end with a '\0' it will
* not be used as it is past the opcodes so there can't
* be a BIND opcode that follows that will use it.
*/
}
else {
/* string terminator */
++p;
}
flags = immediate;
if((flags & BIND_SYMBOL_FLAGS_WEAK_IMPORT) != 0)
weak_import = " (weak import)";
break;
case BIND_OPCODE_SET_TYPE_IMM:
/*
* consume: immediate
* produce: type
*/
opName = "BIND_OPCODE_SET_TYPE_IMM";
if (immediate == 0 || immediate > BIND_TYPE_TEXT_PCREL32) {
printErrorBindType(pass, print_errors, opName,
opcode_start - start,
immediate, &errorCount);
type = 0;
}
else {
type = immediate;
}
break;
case BIND_OPCODE_SET_ADDEND_SLEB:
/*
* consume: p, end
* produce: addend, error
*/
opName = "BIND_OPCODE_SET_ADDEND_SLEB";
addend = read_sleb128(&p, end, &error);
if (error) {
printErrorBind(pass, print_errors, opName,
opcode_start-start, error, &errorCount);
addend = 0;
}
break;
case BIND_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB:
/*
* consume: immediate
* produce: segIndex, segStartAddr, segName, segOffset,
* error
*/
opName = "BIND_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB";
if (validateSegIndex(immediate, segs, nsegs,
segs64, nsegs64, &error))
{
segOffset = read_uleb128(&p, end, &error);
}
if (!error) {
segIndex = immediate;
segStartAddr = segStartAddress(segIndex, segs, nsegs,
segs64, nsegs64);
segName = segmentName(segIndex, segs, nsegs,
segs64, nsegs64);
error = checkSegAndOffset(segIndex, segOffset,
segs, nsegs,
segs64, nsegs64, TRUE);
}
if (error) {
printErrorBind(pass, print_errors, opName,
opcode_start-start, error, &errorCount);
segIndex = -1;
segStartAddr = 0;
segName = NULL;
segOffset = 0;
error = NULL;
}
break;
case BIND_OPCODE_ADD_ADDR_ULEB:
/*
* consume: p, end, segOffset
* produce: segOffset, error
*/
opName = "BIND_OPCODE_ADD_ADDR_ULEB";
error = NULL;
if (-1 == segIndex) {
error = "segment index is invalid";
}
if (!error) {
segOffset += read_uleb128(&p, end, &error);
}
if (!error) {
error = checkSegAndOffset(segIndex, segOffset,
segs, nsegs,
segs64, nsegs64, TRUE);
}
if (error) {
printErrorBind(pass, print_errors, opName,
opcode_start-start, error, &errorCount);
segIndex = -1;
segName = NULL;
segStartAddr = 0;
segOffset = 0;
error = NULL;
}
break;
case BIND_OPCODE_DO_BIND:
/*
* consume: segIndex, segOffset, symbolName,
* libraryOrdinalSet, ordinalTableIndex,
* ordinalTableCount, ordinalTable, libraryOrdinal,
* addend, flags, type, segName, segStartAddr,
* fromDylib, weak_import
* produce: sectName, segOffset, dbi
*/
opName = "BIND_OPCODE_DO_BIND";
/* start by doing all of the validations */
if(CHAIN_FORMAT_NONE == *chain_format)
{
error = checkSegAndOffset(segIndex, segOffset, segs,
nsegs, segs64, nsegs64, TRUE);
if (error) {
printErrorBind(pass, print_errors, opName,
opcode_start-start, error,
&errorCount);
segIndex = -1;
segName = NULL;
segStartAddr = 0;
segOffset = 0;
sectName = NULL;
}
else {
sectName = sectionName(segIndex,
segStartAddr + segOffset,
segs, nsegs,
segs64, nsegs64);
}
}
if (NULL == symbolName) {
error = "missing preceding BIND_OPCODE_SET_SYMBOL_"
"TRAILING_FLAGS_IMM opcode";
printErrorBind(pass, print_errors, opName,
opcode_start-start, error, &errorCount);
}
if (FALSE == libraryOrdinalSet){
error = "missing preceding BIND_OPCODE_SET_DYLIB_"
"ORDINAL_* opcode";
printErrorBind(pass, print_errors, opName,
opcode_start-start, error, &errorCount);
}
if(CHAIN_FORMAT_ARM64E == *chain_format){
/*
* At this point ordinalTableIndex should not equal
* ordinalTableCount or we have seen too many
* BIND_OPCODE_DO_BIND opcodes and that does not match
* the ordinalTableCount.
*/
if(ordinalTableIndex >= ordinalTableCount){
error = "incorrect ordinal table size: number of "
"BIND_OPCODE_DO_BIND opcodes exceed the count in "
"previous BIND_SUBOPCODE_THREADED_SET_BIND_"
"ORDINAL_TABLE_SIZE_ULEB opcode";
printErrorBind(1, print_errors, opName,
opcode_start-start, error,
&errorCount);
}
else {
/*
* The ordinalTable needs to be built if possible.
* If the previous check failed, ordinalTable[oti]
* is not at all valid.
*/
const uint64_t oti = ordinalTableIndex++;
ordinalTable[oti].symbolName = symbolName;
ordinalTable[oti].addend = addend;
ordinalTable[oti].libraryOrdinal
= libraryOrdinalSet ? libraryOrdinal : 0;
ordinalTable[oti].flags = flags;
ordinalTable[oti].type = type;
}
}
else
{
if(pass == 2){
char* segname = strdup(segName ? segName : "??");
char* sectname = strdup(sectName ? sectName : "??");
if (strlen(segname)>16)
segname[16] = 0;
if (strlen(sectname)>16)
sectname[16] = 0;
(*dbi)[n].segname = segname;
(*dbi)[n].sectname = sectname;
(*dbi)[n].address = segStartAddr + segOffset;
(*dbi)[n].bind_type = type;
(*dbi)[n].addend = addend;
(*dbi)[n].dylibname = fromDylib ? fromDylib : "??";
(*dbi)[n].symbolname = (symbolName ? symbolName :
"Symbol name not set");
(*dbi)[n].weak_import = *weak_import != '\0';
(*dbi)[n].pointer_value = 0;
}
n++;
segOffset += sizeof_pointer;
}
break;
case BIND_OPCODE_DO_BIND_ADD_ADDR_ULEB:
/*
* consume: segIndex, segOffset, symbolName,
* libraryOrdinalSet, ordinalTableIndex,
* ordinalTableCount, ordinalTable, libraryOrdinal,
* addend, flags, type, segName, segStartAddr,
* fromDylib, weak_import
* produce: sectName, segOffset, dbi
*/
opName = "BIND_OPCODE_DO_BIND_ADD_ADDR_ULEB";
error = checkSegAndOffset(segIndex, segOffset, segs, nsegs,
segs64, nsegs64, TRUE);
if (error) {
printErrorBind(pass, print_errors, opName,
opcode_start-start, error, &errorCount);
segIndex = -1;
segName = NULL;
segStartAddr = 0;
segOffset = 0;