forked from jirislaby/ksplice
-
Notifications
You must be signed in to change notification settings - Fork 0
/
objmanip.c
3641 lines (3272 loc) · 113 KB
/
objmanip.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
/* This file is based in part on objcopy.c from GNU Binutils v2.17.
*
* Copyright (C) 1991-2006 Free Software Foundation, Inc.
* Copyright (C) 2007-2009 Ksplice, Inc.
* Authors: Jeff Arnold, Anders Kaseorg, Tim Abbott
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License, version 2.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA
* 02110-1301, USA.
*/
/* objmanip performs various object file manipulations for Ksplice. Its first
* two arguments are always an input object file and an output object file.
*
* - keep-new-code: "objmanip <post.o> <out.o> keep-new-code <pre.o> <kid>"
*
* This mode prepares the object file to be installed as a ksplice update. The
* kid argument is the ksplice id string for the ksplice update being built.
*
* - keep-old-code: "objmanip <pre.o> <out.o> keep-old-code"
*
* This mode prepares the object file to be used for run-pre matching. This
* involves replacing all ELF relocations with ksplice relocations and
* writing ksplice_section structures for each ELF text or data section.
*
* - rmsyms mode: "objmanip <in.o> <out.o> rmsyms
*
* In this mode, any ELF relocations involving the list of symbol names given on
* standard input are replaced with ksplice relocations. This is used only
* for KSPLICE_STANDALONE.
*
* - finalize mode: "objmanip <in.o> <out.o> finalize"
*
* In this mode, any ELF relocations to undefined symbols are replaced with
* ksplice relocations.
*/
/* Always define KSPLICE_STANDALONE, even if you're using integrated Ksplice.
objmanip won't compile without it. */
#define KSPLICE_STANDALONE
#define _GNU_SOURCE
#include "objcommon.h"
#include "kmodsrc/ksplice.h"
#include "kmodsrc/offsets.h"
#include "ksplice-patch/ksplice-patch.h"
#include <stdint.h>
#include <stdarg.h>
#include <stdlib.h>
#include <stdio.h>
#include <limits.h>
#define KSPLICE_SYMBOL_STR "KSPLICE_SYMBOL_"
#define symbol_init(sym) *(sym) = (asymbol *)NULL
DEFINE_HASH_TYPE(asymbol *, symbol_hash, symbol_hash_init, symbol_hash_free,
symbol_hash_lookup, symbol_init);
DECLARE_VEC_TYPE(const char *, str_vec);
DECLARE_VEC_TYPE(unsigned long, ulong_vec);
#define bool_init(b) *(b) = false
DEFINE_HASH_TYPE(bool, bool_hash, bool_hash_init, bool_hash_free,
bool_hash_lookup, bool_init);
#define ulong_init(x) *(x) = 0
DEFINE_HASH_TYPE(unsigned long, ulong_hash, ulong_hash_init,
ulong_hash_free, ulong_hash_lookup, ulong_init);
void do_keep_new_code(struct superbfd *isbfd, const char *pre);
void do_keep_old_code(struct superbfd *isbfd);
void do_finalize(struct superbfd *isbfd);
void do_rmsyms(struct superbfd *isbfd);
bool relocs_equal(struct supersect *old_src_ss, struct supersect *new_src_ss,
arelent *old_reloc, arelent *new_reloc);
bfd_vma non_dst_mask(struct supersect *ss, arelent *reloc);
bool all_relocs_equal(struct span *old_span, struct span *new_span);
static bool part_of_reloc(struct supersect *ss, unsigned long addr);
static bool nonrelocs_equal(struct span *old_span, struct span *new_span);
static void handle_section_symbol_renames(struct superbfd *oldsbfd,
struct superbfd *newsbfd);
static void compute_entry_points(struct superbfd *sbfd);
static void copy_patched_entry_points(struct superbfd *oldsbfd,
struct superbfd *newsbfd);
enum supersect_type supersect_type(struct supersect *ss);
void initialize_supersect_types(struct superbfd *sbfd);
static void initialize_spans(struct superbfd *sbfd);
static void initialize_string_spans(struct supersect *ss);
static void initialize_table_spans(struct superbfd *sbfd,
struct table_section *s);
static void initialize_table_section_spans(struct superbfd *sbfd);
static void initialize_ksplice_call_spans(struct supersect *ss);
struct span *reloc_target_span(struct supersect *ss, arelent *reloc);
static struct span *span_offset_target_span(struct span *span, int offset);
static bfd_vma reloc_target_offset(struct supersect *ss, arelent *reloc);
struct span *find_span(struct supersect *ss, bfd_size_type address);
void remove_unkept_spans(struct superbfd *sbfd);
void compute_span_shifts(struct superbfd *sbfd);
static struct span *new_span(struct supersect *ss, bfd_vma start, bfd_vma size);
static bool is_table_section(const char *name, bool consider_other,
bool consider_crc);
const struct table_section *get_table_section(const char *name);
void mangle_section_name(struct superbfd *sbfd, const char *name);
void rm_relocs(struct superbfd *isbfd);
void rm_some_relocs(struct supersect *ss);
void write_ksplice_reloc(struct supersect *ss, arelent *orig_reloc);
static void write_ksplice_reloc_howto(struct supersect *ss, const
struct ksplice_reloc_howto *const *addr,
reloc_howto_type *howto,
enum ksplice_reloc_howto_type type);
static void write_ksplice_date_reloc(struct supersect *ss, unsigned long offset,
const char *str,
enum ksplice_reloc_howto_type type);
static void write_ksplice_patch_reloc(struct supersect *ss,
const char *sectname, unsigned long *addr,
bfd_size_type size, const char *label,
long addend);
static void write_ksplice_nonreloc_howto(struct supersect *ss,
const struct ksplice_reloc_howto
*const *addr,
enum ksplice_reloc_howto_type type,
int size);
static void write_date_relocs(struct superbfd *sbfd, const char *str,
enum ksplice_reloc_howto_type type);
static void write_table_relocs(struct superbfd *sbfd, const char *sectname,
enum ksplice_reloc_howto_type type);
static void write_ksplice_table_reloc(struct supersect *ss,
unsigned long address,
const char *label,
enum ksplice_reloc_howto_type type);
void load_ksplice_symbol_offsets(struct superbfd *sbfd);
void write_canary(struct supersect *ss, int offset, bfd_size_type size,
bfd_vma dst_mask);
static void write_ksplice_section(struct span *span);
void write_ksplice_patches(struct superbfd *sbfd, struct span *span);
void write_ksplice_patch(struct superbfd *sbfd, struct span *span,
const char *label, long offset);
void *write_patch_storage(struct supersect *ss, struct ksplice_patch *patch,
size_t size, struct supersect **data_ssp);
void write_ksplice_deleted_patch(struct superbfd *sbfd, const char *name,
const char *label, const char *sectname,
long offset);
static void write_bugline_patches(struct superbfd *sbfd);
asymbol **make_undefined_symbolp(struct superbfd *sbfd, const char *name);
void filter_table_sections(struct superbfd *isbfd);
void filter_table_section(struct superbfd *sbfd, const struct table_section *s);
void keep_referenced_sections(struct superbfd *sbfd);
void mark_precallable_spans(struct superbfd *sbfd);
bfd_boolean copy_object(bfd *ibfd, bfd *obfd);
void setup_section(bfd *ibfd, asection *isection, void *obfdarg);
static void setup_new_section(bfd *obfd, struct supersect *ss);
static void write_section(bfd *obfd, asection *osection, void *arg);
static void delete_obsolete_relocs(struct supersect *ss);
void mark_symbols_used_in_relocations(bfd *abfd, asection *isection,
void *ignored);
static void ss_mark_symbols_used_in_relocations(struct supersect *ss);
void filter_symbols(bfd *ibfd, bfd *obfd, struct asymbolp_vec *osyms,
struct asymbolp_vec *isyms);
static bool deleted_table_section_symbol(bfd *abfd, asymbol *sym);
struct supersect *__attribute((format(printf, 2, 3)))
make_section(struct superbfd *sbfd, const char *fmt, ...);
void __attribute__((format(printf, 3, 4)))
write_string(struct supersect *ss, const char **addr, const char *fmt, ...);
void write_ksplice_export(struct superbfd *sbfd, struct span *span, bool del);
void write_reloc(struct supersect *ss, const void *addr, asymbol **symp,
bfd_vma offset);
arelent *create_reloc(struct supersect *ss, const void *addr, asymbol **symp,
bfd_vma offset);
static void foreach_symbol_pair(struct superbfd *oldsbfd, struct superbfd *newsbfd,
void (*fn)(struct span *old_span,
asymbol *oldsym,
struct span *new_span,
asymbol *newsym));
static void check_global_symbols(struct span *old_span, asymbol *oldsym,
struct span *new_span, asymbol *newsym);
static void match_global_symbols(struct span *old_span, asymbol *oldsym,
struct span *new_span, asymbol *newsym);
static void match_symbol_spans(struct span *old_span, asymbol *oldsym,
struct span *new_span, asymbol *newsym);
static void match_table_spans(struct span *old_span, struct span *new_span);
static void match_other_spans(struct span *old_span, struct span *new_span);
static struct span *get_crc_span(struct span *span,
const struct table_section *ts);
static void foreach_span_pair(struct superbfd *oldsbfd,
struct superbfd *newsbfd,
void (*fn)(struct span *old_span,
struct span *new_span));
static void match_spans_by_label(struct span *old_span, struct span *new_span);
static void match_string_spans(struct span *old_span, struct span *new_span);
static void mark_new_spans(struct superbfd *sbfd);
static void handle_deleted_spans(struct superbfd *oldsbfd,
struct superbfd *newsbfd);
static void unmatch_addr_spans(struct span *old_span, struct span *new_span,
const struct table_section *ts);
static void compare_matched_spans(struct superbfd *newsbfd);
static void compare_spans(struct span *old_span, struct span *new_span);
static void update_nonzero_offsets(struct superbfd *sbfd);
static void handle_nonzero_offset_relocs(struct supersect *ss);
static void keep_span(struct span *span);
static void init_objmanip_superbfd(struct superbfd *sbfd);
static const char *label_lookup(struct superbfd *sbfd, asymbol *sym);
static void label_map_set(struct superbfd *sbfd, const char *oldlabel,
const char *label);
static void print_label_changes(struct superbfd *sbfd);
static void init_label_map(struct superbfd *sbfd);
static void change_initial_label(struct span *span, const char *label);
static asymbol **symbolp_scan(struct supersect *ss, bfd_vma value);
static void init_csyms(struct superbfd *sbfd);
static void init_callers(struct superbfd *sbfd);
static asymbol *canonical_symbol(struct superbfd *sbfd, asymbol *sym);
static asymbol **canonical_symbolp(struct superbfd *sbfd, asymbol *sym);
static char *static_local_symbol(struct superbfd *sbfd, asymbol *sym);
static char *symbol_label(struct superbfd *sbfd, asymbol *sym);
int verbose = 0;
#define debug_(sbfd, level, fmt, ...) \
do { \
if (verbose >= (level)) \
printf("%s: " fmt, (sbfd)->abfd->filename, \
## __VA_ARGS__); \
} while (0)
#define debug0(sbfd, fmt, ...) debug_(sbfd, 0, fmt, ## __VA_ARGS__)
#define debug1(sbfd, fmt, ...) debug_(sbfd, 1, fmt, ## __VA_ARGS__)
#define err(sbfd, fmt, ...) \
do { \
fprintf(stderr, "%s: " fmt, (sbfd)->abfd->filename, \
## __VA_ARGS__); \
} while (0)
struct str_vec delsects;
struct asymbolp_vec extract_syms;
bool changed;
struct ksplice_config *config;
const char *modestr, *kid, *finalize_target = NULL;
bool write_output = true;
struct superbfd *offsets_sbfd = NULL;
#define mode(str) strstarts(modestr, str)
DECLARE_VEC_TYPE(unsigned long, addr_vec);
DEFINE_HASH_TYPE(struct addr_vec, addr_vec_hash,
addr_vec_hash_init, addr_vec_hash_free, addr_vec_hash_lookup,
vec_init);
struct addr_vec_hash system_map;
struct bool_hash system_map_written;
struct ulong_hash ksplice_symbol_offset;
struct ulong_hash ksplice_howto_offset;
struct ulong_hash ksplice_string_offset;
void load_system_map()
{
const char *config_dir = getenv("KSPLICE_CONFIG_DIR");
assert(config_dir);
FILE *fp = fopen(strprintf("%s/System.map", config_dir), "r");
assert(fp);
addr_vec_hash_init(&system_map);
unsigned long addr;
char type;
char *sym;
while (fscanf(fp, "%lx %c %as\n", &addr, &type, &sym) == 3)
*vec_grow(addr_vec_hash_lookup(&system_map, sym, TRUE),
1) = addr;
fclose(fp);
}
void load_ksplice_symbol_offsets(struct superbfd *sbfd)
{
asection *sect = bfd_get_section_by_name(sbfd->abfd,
".ksplice_symbols");
if (sect == NULL)
return;
struct supersect *ss = fetch_supersect(sbfd, sect);
struct ksplice_symbol *ksym;
for (ksym = ss->contents.data;
(void *)ksym < ss->contents.data + ss->contents.size; ksym++) {
const char *label = read_string(ss, &ksym->label);
unsigned long *ksymbol_offp =
ulong_hash_lookup(&ksplice_symbol_offset, label, TRUE);
*ksymbol_offp = addr_offset(ss, ksym);
}
}
void load_offsets()
{
char *kmodsrc = getenv("KSPLICE_KMODSRC");
assert(kmodsrc != NULL);
bfd *offsets_bfd = bfd_openr(strprintf("%s/offsets.o", kmodsrc), NULL);
assert(offsets_bfd != NULL);
char **matching;
assert(bfd_check_format_matches(offsets_bfd, bfd_object, &matching));
offsets_sbfd = fetch_superbfd(offsets_bfd);
asection *config_sect = bfd_get_section_by_name(offsets_sbfd->abfd,
".ksplice_config");
struct supersect *config_ss =
fetch_supersect(offsets_sbfd, config_sect);
config = config_ss->contents.data;
}
void load_options(struct superbfd *sbfd)
{
asection *sect = bfd_get_section_by_name(sbfd->abfd,
".ksplice_options");
if (sect == NULL)
return;
struct supersect *ss = fetch_supersect(sbfd, sect);
const struct ksplice_option *opt;
for (opt = ss->contents.data;
(void *)opt < ss->contents.data + ss->contents.size; opt++) {
if (opt->type == KSPLICE_OPTION_ASSUME_RODATA) {
arelent *reloc = find_reloc(ss, &opt->target);
assert(reloc != NULL);
struct span *span = reloc_target_span(ss, reloc);
assert(span != NULL);
assert(span->ss->type == SS_TYPE_DATA);
assert(span->start == 0 &&
span->size == span->ss->contents.size);
span->ss->type = SS_TYPE_RODATA;
} else if (opt->type == KSPLICE_OPTION_MATCH_DATA_EARLY) {
arelent *reloc = find_reloc(ss, &opt->target);
assert(reloc != NULL);
struct span *span = reloc_target_span(ss, reloc);
assert(span != NULL);
assert(span->ss->type == SS_TYPE_DATA);
assert(span->start == 0 &&
span->size == span->ss->contents.size);
span->ss->match_data_early = true;
} else {
err(sbfd, "Unrecognized Ksplice option %d\n",
opt->type);
DIE;
}
}
}
bool matchable_data_section(struct supersect *ss)
{
if (ss->type == SS_TYPE_STRING)
return true;
if (ss->type == SS_TYPE_RODATA)
return true;
if (ss->type == SS_TYPE_DATA && ss->relocs.size != 0)
return true;
if (ss->type == SS_TYPE_EXPORT)
return true;
if (ss->type == SS_TYPE_BUGTABLE)
return true;
return false;
}
bool unchangeable_section(struct supersect *ss)
{
if (ss->type == SS_TYPE_DATA)
return true;
if (ss->type == SS_TYPE_IGNORED && !strstarts(ss->name, ".debug") &&
strcmp(ss->name, "__ksymtab_strings") != 0)
return true;
return false;
}
int main(int argc, char *argv[])
{
if (getenv("KSPLICE_VERBOSE") != NULL)
verbose = atoi(getenv("KSPLICE_VERBOSE"));
bfd_init();
bfd *ibfd = bfd_openr(argv[1], NULL);
assert(ibfd);
char **matching;
if (bfd_check_format_matches(ibfd, bfd_archive, &matching) &&
bfd_openr_next_archived_file(ibfd, NULL) == NULL)
return 66; /* empty archive */
assert(bfd_check_format_matches(ibfd, bfd_object, &matching));
const char *output_target = bfd_get_target(ibfd);
load_system_map();
load_offsets();
bool_hash_init(&system_map_written);
ulong_hash_init(&ksplice_symbol_offset);
ulong_hash_init(&ksplice_howto_offset);
ulong_hash_init(&ksplice_string_offset);
struct superbfd *isbfd = fetch_superbfd(ibfd);
modestr = argv[3];
if (mode("finalize"))
finalize_target = argv[4];
init_objmanip_superbfd(isbfd);
if (mode("keep-new-code")) {
kid = argv[5];
do_keep_new_code(isbfd, argv[4]);
} else if (mode("keep-old-code")) {
do_keep_old_code(isbfd);
} else if (mode("finalize")) {
do_finalize(isbfd);
} else if (mode("rmsyms")) {
do_rmsyms(isbfd);
}
if (write_output) {
bfd *obfd = bfd_openw(argv[2], output_target);
assert(obfd);
copy_object(ibfd, obfd);
assert(bfd_close(obfd));
}
if (offsets_sbfd != NULL)
assert(bfd_close(offsets_sbfd->abfd));
assert(bfd_close(ibfd));
return EXIT_SUCCESS;
}
void do_keep_new_code(struct superbfd *isbfd, const char *pre)
{
struct bfd *prebfd = bfd_openr(pre, NULL);
assert(prebfd != NULL);
char **matching;
assert(bfd_check_format_matches(prebfd, bfd_object, &matching));
struct superbfd *presbfd = fetch_superbfd(prebfd);
init_objmanip_superbfd(presbfd);
foreach_symbol_pair(presbfd, isbfd, match_global_symbols);
debug1(isbfd, "Matched global\n");
foreach_span_pair(presbfd, isbfd, match_string_spans);
debug1(isbfd, "Matched string spans\n");
foreach_symbol_pair(presbfd, isbfd, match_symbol_spans);
debug1(isbfd, "Matched by name\n");
foreach_span_pair(presbfd, isbfd, match_spans_by_label);
debug1(isbfd, "Matched by label\n");
foreach_span_pair(presbfd, isbfd, match_table_spans);
debug1(isbfd, "Matched table spans\n");
foreach_span_pair(presbfd, isbfd, match_other_spans);
debug1(isbfd, "Matched other spans\n");
do {
changed = false;
compare_matched_spans(isbfd);
update_nonzero_offsets(isbfd);
mark_new_spans(isbfd);
} while (changed);
vec_init(&delsects);
foreach_symbol_pair(presbfd, isbfd, check_global_symbols);
handle_deleted_spans(presbfd, isbfd);
handle_section_symbol_renames(presbfd, isbfd);
copy_patched_entry_points(presbfd, isbfd);
assert(bfd_close(prebfd));
do {
changed = false;
mark_precallable_spans(isbfd);
} while (changed);
asection *sect;
for (sect = isbfd->abfd->sections; sect != NULL; sect = sect->next) {
struct supersect *ss = fetch_supersect(isbfd, sect);
ss->keep = false;
struct span *span;
for (span = ss->spans.data;
span < ss->spans.data + ss->spans.size; span++) {
if (strstarts(ss->name, ".ksplice_options"))
span->keep = false;
else if (span->new || span->patch || span->datapatch)
keep_span(span);
else
span->keep = false;
if (span->patch && span->precallable) {
err(isbfd, "Patched span %s can be reached "
"by a precall function\n", span->label);
DIE;
}
}
}
print_label_changes(isbfd);
for (sect = isbfd->abfd->sections; sect != NULL; sect = sect->next) {
struct supersect *ss = fetch_supersect(isbfd, sect);
struct span *span;
for (span = ss->spans.data;
span < ss->spans.data + ss->spans.size; span++) {
if (span->patch || span->bugpatch || span->datapatch)
debug0(isbfd, "Patching span %s\n",
span->label);
}
}
for (sect = isbfd->abfd->sections; sect != NULL; sect = sect->next) {
struct supersect *ss = fetch_supersect(isbfd, sect);
struct span *span;
for (span = ss->spans.data;
span < ss->spans.data + ss->spans.size; span++) {
if (span->new)
debug0(isbfd, "New span %s\n", span->label);
}
}
write_output = false;
const char **sectname;
for (sectname = delsects.data;
sectname < delsects.data + delsects.size; sectname++) {
write_output = true;
debug0(isbfd, "Deleted section: %s\n", *sectname);
}
filter_table_sections(isbfd);
compute_span_shifts(isbfd);
for (sect = isbfd->abfd->sections; sect != NULL; sect = sect->next) {
struct supersect *ss = fetch_supersect(isbfd, sect);
if (ss->type == SS_TYPE_KSPLICE_CALL)
continue;
struct span *span;
for (span = ss->spans.data;
span < ss->spans.data + ss->spans.size; span++) {
if (span->keep || span->bugpatch)
write_output = true;
if (span->patch || span->new || span->datapatch)
write_ksplice_section(span);
if (span->patch || span->datapatch)
write_ksplice_patches(isbfd, span);
if (ss->type == SS_TYPE_EXPORT && span->new)
write_ksplice_export(isbfd, span, false);
}
}
write_bugline_patches(isbfd);
rm_relocs(isbfd);
remove_unkept_spans(isbfd);
}
void do_keep_old_code(struct superbfd *isbfd)
{
asection *sect;
for (sect = isbfd->abfd->sections; sect != NULL; sect = sect->next) {
struct supersect *ss = fetch_supersect(isbfd, sect);
ss->keep = false;
struct span *span;
for (span = ss->spans.data;
span < ss->spans.data + ss->spans.size; span++) {
span->keep = false;
if (ss->type == SS_TYPE_TEXT &&
!strstarts(ss->name, ".fixup"))
keep_span(span);
if (ss->type == SS_TYPE_EXPORT)
keep_span(span);
}
}
asymbol **symp;
for (symp = isbfd->syms.data;
symp < isbfd->syms.data + isbfd->syms.size; symp++) {
asymbol *sym = *symp;
if (!bfd_is_const_section(sym->section) &&
(sym->flags & BSF_GLOBAL) != 0) {
struct supersect *sym_ss =
fetch_supersect(isbfd, sym->section);
if (sym->value == sym_ss->contents.size)
continue;
struct span *span = find_span(sym_ss, sym->value);
assert(span != NULL);
if (sym_ss->type != SS_TYPE_IGNORED)
keep_span(span);
}
}
do {
changed = false;
keep_referenced_sections(isbfd);
} while (changed);
filter_table_sections(isbfd);
compute_span_shifts(isbfd);
for (sect = isbfd->abfd->sections; sect != NULL; sect = sect->next) {
struct supersect *ss = fetch_supersect(isbfd, sect);
asymbol *sym = canonical_symbol(isbfd, sect->symbol);
if (sym == NULL)
continue;
if ((sym->flags & BSF_WEAK) != 0)
continue;
if (bfd_get_section_size(sect) == 0)
continue;
if (!ss->keep)
continue;
if (ss->type != SS_TYPE_TEXT && !matchable_data_section(ss))
continue;
struct span *span;
for (span = ss->spans.data;
span < ss->spans.data + ss->spans.size; span++) {
if (span->keep)
write_ksplice_section(span);
}
}
write_table_relocs(isbfd, "__bug_table", KSPLICE_HOWTO_BUG);
write_table_relocs(isbfd, "__ex_table", KSPLICE_HOWTO_EXTABLE);
rm_relocs(isbfd);
remove_unkept_spans(isbfd);
mangle_section_name(isbfd, "__markers");
mangle_section_name(isbfd, "__tracepoints");
mangle_section_name(isbfd, "__ex_table");
for (sect = isbfd->abfd->sections; sect != NULL; sect = sect->next) {
struct supersect *ss = fetch_supersect(isbfd, sect);
if (ss->type == SS_TYPE_EXPORT)
mangle_section_name(isbfd, ss->name);
}
}
void do_finalize(struct superbfd *isbfd)
{
load_ksplice_symbol_offsets(isbfd);
asection *sect;
for (sect = isbfd->abfd->sections; sect != NULL; sect = sect->next) {
struct supersect *ss = fetch_supersect(isbfd, sect);
if (ss->type == SS_TYPE_EXIT) {
struct span *span;
for (span = ss->spans.data;
span < ss->spans.data + ss->spans.size; span++)
span->keep = false;
ss->keep = false;
}
}
write_date_relocs(isbfd, "<{DATE...}>", KSPLICE_HOWTO_DATE);
write_date_relocs(isbfd, "<{TIME}>", KSPLICE_HOWTO_TIME);
rm_relocs(isbfd);
}
void do_rmsyms(struct superbfd *isbfd)
{
asection *extract_sect = bfd_get_section_by_name(isbfd->abfd,
".ksplice_extract");
if (extract_sect != NULL) {
struct supersect *extract_ss = fetch_supersect(isbfd,
extract_sect);
arelent **relocp;
for (relocp = extract_ss->relocs.data;
relocp < extract_ss->relocs.data + extract_ss->relocs.size;
relocp++) {
asymbol *sym = *(*relocp)->sym_ptr_ptr;
if (bfd_is_und_section(sym->section)) {
debug1(isbfd, "extracting symbol %s\n",
sym->name);
*vec_grow(&extract_syms, 1) = sym;
}
}
}
rm_relocs(isbfd);
}
void match_spans(struct span *old_span, struct span *new_span)
{
struct superbfd *sbfd = new_span->ss->parent;
if (old_span->match == new_span && new_span->match == old_span)
return;
if (old_span->match != NULL) {
err(sbfd, "Matching conflict: old %s: %s != %s\n",
old_span->label, old_span->match->label, new_span->label);
DIE;
}
if (new_span->match != NULL) {
err(sbfd, "Matching conflict: new %s: %s != %s\n",
new_span->label, new_span->match->label, old_span->label);
DIE;
}
old_span->match = new_span;
new_span->match = old_span;
debug1(sbfd, "Matched old %s to new %s\n", old_span->label,
new_span->label);
if (old_span->ss->type != new_span->ss->type &&
old_span->ss->type == new_span->ss->orig_type)
old_span->ss->type = new_span->ss->type;
const struct table_section *ts = get_table_section(old_span->ss->name);
if (ts == NULL || !ts->has_addr || ts->other_sect == NULL)
return;
struct span *old_sym_span =
span_offset_target_span(old_span, ts->other_offset);
struct span *new_sym_span =
span_offset_target_span(new_span, ts->other_offset);
assert(old_sym_span != NULL && new_sym_span != NULL);
match_spans(old_sym_span, new_sym_span);
}
void unmatch_span(struct span *old_span)
{
struct span *new_span = old_span->match;
old_span->match = NULL;
new_span->match = NULL;
new_span->bugpatch = false;
if (old_span->ss->type == SS_TYPE_SPECIAL) {
const struct table_section *ts =
get_table_section(old_span->ss->name);
if (ts != NULL && ts->has_addr)
unmatch_addr_spans(old_span, new_span, ts);
}
new_span->patch = false;
new_span->bugpatch = false;
new_span->datapatch = false;
changed = true;
}
static void match_global_symbols(struct span *old_span, asymbol *oldsym,
struct span *new_span, asymbol *newsym)
{
if (newsym == NULL ||
(oldsym->flags & BSF_GLOBAL) == 0 ||
(newsym->flags & BSF_GLOBAL) == 0)
return;
match_spans(old_span, new_span);
}
static void check_global_symbols(struct span *old_span, asymbol *oldsym,
struct span *new_span, asymbol *newsym)
{
if ((oldsym->flags & BSF_GLOBAL) == 0 ||
(newsym != NULL && (newsym->flags & BSF_GLOBAL) == 0))
return;
if (old_span->ss->type == SS_TYPE_IGNORED)
return;
if (old_span->match != new_span) {
if (new_span != NULL)
err(new_span->ss->parent,
"Global symbol span mismatch: %s %s/%s\n",
oldsym->name, old_span->label, new_span->label);
else
err(old_span->ss->parent,
"Global symbol span mismatch: %s %s/NULL\n",
oldsym->name, old_span->label);
DIE;
}
}
static void foreach_symbol_pair(struct superbfd *oldsbfd, struct superbfd *newsbfd,
void (*fn)(struct span *old_span,
asymbol *oldsym,
struct span *new_span,
asymbol *newsym))
{
asymbol **oldsymp, **newsymp;
for (oldsymp = oldsbfd->syms.data;
oldsymp < oldsbfd->syms.data + oldsbfd->syms.size; oldsymp++) {
asymbol *oldsym = *oldsymp;
if ((oldsym->flags & BSF_DEBUGGING) != 0 ||
bfd_is_const_section(oldsym->section))
continue;
struct supersect *old_ss =
fetch_supersect(oldsbfd, oldsym->section);
if (old_ss->type == SS_TYPE_SPECIAL ||
old_ss->type == SS_TYPE_EXPORT)
continue;
struct span *old_span = find_span(old_ss, oldsym->value);
if (old_span == NULL) {
err(oldsbfd, "Could not find span for %s\n",
oldsym->name);
DIE;
}
bool found = false;
for (newsymp = newsbfd->syms.data;
newsymp < newsbfd->syms.data + newsbfd->syms.size;
newsymp++) {
asymbol *newsym = *newsymp;
if ((newsym->flags & BSF_DEBUGGING) != 0 ||
bfd_is_const_section(newsym->section))
continue;
if (strcmp(oldsym->name, newsym->name) != 0)
continue;
struct supersect *new_ss =
fetch_supersect(newsbfd, newsym->section);
if (old_ss->type != new_ss->type &&
old_ss->type != new_ss->orig_type)
continue;
assert(!found);
found = true;
struct span *new_span =
find_span(new_ss, newsym->value);
if (new_span == NULL) {
err(newsbfd, "Could not find span for %s\n",
newsym->name);
DIE;
}
fn(old_span, oldsym, new_span, newsym);
}
if (!found)
fn(old_span, oldsym, NULL, NULL);
}
}
static void match_symbol_spans(struct span *old_span, asymbol *oldsym,
struct span *new_span, asymbol *newsym)
{
if (newsym == NULL)
return;
if (old_span->ss->type == SS_TYPE_SPECIAL)
return;
if (static_local_symbol(old_span->ss->parent, oldsym) ||
static_local_symbol(new_span->ss->parent, newsym))
return;
if (old_span->match == NULL && new_span->match == NULL)
match_spans(old_span, new_span);
}
static void match_spans_by_label(struct span *old_span, struct span *new_span)
{
if (old_span->ss->type == SS_TYPE_STRING ||
(is_table_section(old_span->ss->name, true, false) &&
!is_table_section(old_span->ss->name, false, false)))
return;
if (strcmp(old_span->label, new_span->label) == 0)
match_spans(old_span, new_span);
}
static void match_string_spans(struct span *old_span, struct span *new_span)
{
if (old_span->ss->type != SS_TYPE_STRING ||
strcmp(old_span->ss->name, new_span->ss->name) != 0)
return;
if (strcmp((char *)old_span->ss->contents.data + old_span->start,
(char *)new_span->ss->contents.data + new_span->start) == 0)
match_spans(old_span, new_span);
}
static void foreach_span_pair(struct superbfd *oldsbfd,
struct superbfd *newsbfd,
void (*fn)(struct span *old_span,
struct span *new_span))
{
asection *oldsect, *newsect;
struct supersect *oldss, *newss;
struct span *old_span, *new_span;
for (newsect = newsbfd->abfd->sections; newsect != NULL;
newsect = newsect->next) {
newss = fetch_supersect(newsbfd, newsect);
for (oldsect = oldsbfd->abfd->sections; oldsect != NULL;
oldsect = oldsect->next) {
oldss = fetch_supersect(oldsbfd, oldsect);
if (oldss->type != newss->type)
continue;
for (new_span = newss->spans.data;
new_span < newss->spans.data + newss->spans.size;
new_span++) {
for (old_span = oldss->spans.data;
old_span < oldss->spans.data +
oldss->spans.size; old_span++)
fn(old_span, new_span);
}
}
}
}
static void mark_new_spans(struct superbfd *sbfd)
{
asection *sect;
for (sect = sbfd->abfd->sections; sect != NULL; sect = sect->next) {
struct supersect *ss = fetch_supersect(sbfd, sect);
if (ss->type == SS_TYPE_SPECIAL || ss->type == SS_TYPE_IGNORED)
continue;
struct span *span;
for (span = ss->spans.data;
span < ss->spans.data + ss->spans.size; span++) {
if (span->match == NULL)
span->new = true;
}
}
}
static void handle_deleted_spans(struct superbfd *oldsbfd,
struct superbfd *newsbfd)
{
asection *sect;
for (sect = oldsbfd->abfd->sections; sect != NULL; sect = sect->next) {
struct supersect *ss = fetch_supersect(oldsbfd, sect);
struct span *span;
for (span = ss->spans.data;
span < ss->spans.data + ss->spans.size; span++) {
if (span->match != NULL)
continue;
if (ss->type == SS_TYPE_EXPORT) {
*vec_grow(&delsects, 1) = span->label;
write_ksplice_export(newsbfd, span, true);
} else if (ss->type == SS_TYPE_TEXT) {
*vec_grow(&delsects, 1) = span->label;
if (span->symbol == NULL)
DIE;
write_ksplice_deleted_patch
(newsbfd, span->symbol->name, span->label,
span->ss->name, 0);
}
}
}
}
static void handle_nonzero_offset_relocs(struct supersect *ss)
{
struct span *address_span, *target_span;
arelent **relocp;
for (relocp = ss->relocs.data;
relocp < ss->relocs.data + ss->relocs.size; relocp++) {
arelent *reloc = *relocp;
address_span = find_span(ss, reloc->address);
if (!address_span->new && !address_span->patch)
continue;
asymbol *sym = *reloc->sym_ptr_ptr;
if (bfd_is_const_section(sym->section))
continue;
bfd_vma offset = reloc_target_offset(ss, reloc);
target_span = reloc_target_span(ss, reloc);
if (sym->value + offset == target_span->start)
continue;
if (target_span->ss->type != SS_TYPE_TEXT)
continue;
if (target_span->patch)
continue;
target_span->patch = true;
changed = true;
debug1(ss->parent, "Changing %s because a relocation from sect "
"%s has a nonzero offset %lx+%lx into it\n",
target_span->label, ss->name, (unsigned long)sym->value,
(unsigned long)offset);
}
}
static void update_nonzero_offsets(struct superbfd *sbfd)
{
asection *sect;
for (sect = sbfd->abfd->sections; sect != NULL; sect = sect->next) {
struct supersect *ss = fetch_supersect(sbfd, sect);
if (ss->type == SS_TYPE_SPECIAL || ss->type == SS_TYPE_IGNORED)
continue;
handle_nonzero_offset_relocs(ss);
}
}
static void unmatch_addr_spans(struct span *old_span, struct span *new_span,
const struct table_section *ts)
{
struct span *old_sym_span =
span_offset_target_span(old_span, ts->addr_offset);
struct span *new_sym_span =
span_offset_target_span(new_span, ts->addr_offset);
assert(old_sym_span != NULL && new_sym_span != NULL);
if (old_sym_span->match == new_sym_span &&
new_sym_span->match == old_sym_span &&
!(new_sym_span->patch && new_sym_span->ss->type == SS_TYPE_TEXT)) {
if (old_sym_span->ss->type == SS_TYPE_TEXT) {
debug1(new_span->ss->parent, "Patching %s due "
"to relocations from special section %s\n",
new_sym_span->label, new_span->label);
new_sym_span->patch = true;
} else {
debug1(new_span->ss->parent, "Unmatching %s and %s due "
"to relocations from special section %s/%s\n",
old_sym_span->label, new_sym_span->label,