-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcubin_function_patch.h
More file actions
2753 lines (2587 loc) · 105 KB
/
Copy pathcubin_function_patch.h
File metadata and controls
2753 lines (2587 loc) · 105 KB
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
/*
* SPDX-FileCopyrightText: Copyright (c) 2026 Charlie Durham
* SPDX-License-Identifier: MIT
*
* cubin_function_patch.h
*
* Single-header C99 API for replacing reserved CUDA function bodies in a fully
* linked cubin/ELF with self-contained function bodies from nvPTXCompiler
* --compile-only RDC ELF output.
*
* Contract:
* - The handle stores a pointer to the linked cubin template; the caller keeps
* that cubin alive and immutable for the handle lifetime.
* - The handle is immutable after create. Multiple threads may call patch
* routines through the same handle when each call uses its own output cubin
* buffer.
* - The replacement function must have the same symbol name, fit inside the
* reserved linked function body, target the same ELF machine, and have no
* relocations targeting the replacement function text except for supported
* CUDA constant-bank relocations into matching reserved side sections.
* - The linked cubin metadata is preserved. Build the reserved template with
* worst-case register/local/stack requirements for the functions being
* replaced.
* - On cubins with NVIDIA cap/merc text mirror sections, the patcher also
* patches the matching .nv.capmerc.text.<symbol> section. Metadata remains
* the template metadata.
* - No allocation is performed internally. Create uses caller-provided handle
* memory. Patch writes the new cubin into caller-provided output memory.
*
* Define CUBIN_FUNCTION_PATCH_IMPLEMENTATION in exactly one translation unit.
*/
#ifndef CUBIN_FUNCTION_PATCH_H_INCLUDE
#define CUBIN_FUNCTION_PATCH_H_INCLUDE
#define CUBIN_FUNCTION_PATCH_VERSION_MAJOR 1 //!< Cubin Function Patch major version.
#define CUBIN_FUNCTION_PATCH_VERSION_MINOR 0 //!< Cubin Function Patch minor version.
#define CUBIN_FUNCTION_PATCH_VERSION_PATCH 1 //!< Cubin Function Patch patch version.
#ifndef CUBIN_FUNCTION_PATCH_VERSION_STRING
#define CUBIN_FUNCTION_PATCH_VERSION_STRING "1.0.1"
#endif
#define CUBIN_FUNCTION_PATCH_VERSION \
(CUBIN_FUNCTION_PATCH_VERSION_MAJOR * 10000 + \
CUBIN_FUNCTION_PATCH_VERSION_MINOR * 100 + \
CUBIN_FUNCTION_PATCH_VERSION_PATCH)
#include <stddef.h>
#include <stdint.h>
#ifdef __cplusplus
#define CUBIN_FUNCTION_PATCH_PUBLIC_DEC extern "C"
#define CUBIN_FUNCTION_PATCH_PUBLIC_DEF extern "C"
#else
#define CUBIN_FUNCTION_PATCH_PUBLIC_DEC extern
#define CUBIN_FUNCTION_PATCH_PUBLIC_DEF
#endif
/**
* Result code returned by cubin_function_patch API calls.
*/
typedef enum {
CUBIN_FUNCTION_PATCH_SUCCESS = 0,
CUBIN_FUNCTION_PATCH_ERROR_INVALID_VALUE = 1,
CUBIN_FUNCTION_PATCH_ERROR_INVALID_ELF = 2,
CUBIN_FUNCTION_PATCH_ERROR_WORKSPACE_EXHAUSTED = 3,
CUBIN_FUNCTION_PATCH_ERROR_SYMBOL_NOT_FOUND = 4,
CUBIN_FUNCTION_PATCH_ERROR_REPLACEMENT_TOO_LARGE = 5,
CUBIN_FUNCTION_PATCH_ERROR_RELOCATION_UNSUPPORTED = 6,
CUBIN_FUNCTION_PATCH_ERROR_MACHINE_MISMATCH = 7,
CUBIN_FUNCTION_PATCH_ERROR_AMBIGUOUS_SYMBOL = 8,
CUBIN_FUNCTION_PATCH_ERROR_TAIL_POLICY = 9
} CubinFunctionPatchResult;
/**
* Policy for bytes after a smaller replacement function body.
*/
typedef enum {
CUBIN_FUNCTION_PATCH_TAIL_LEAVE_UNCHANGED = 0,
CUBIN_FUNCTION_PATCH_TAIL_REQUIRE_EXACT_SIZE = 1
} CubinFunctionPatchTailPolicy;
/**
* Immutable patch handle created from a fully linked cubin template.
*
* The handle does not own the linked cubin bytes. The caller must keep the
* linked cubin alive and immutable until cubin_function_patch_destroy is called.
* After creation, the same handle can be used concurrently by multiple threads
* as long as each thread writes to a distinct output cubin buffer.
*/
typedef struct CubinFunctionPatchHandle CubinFunctionPatchHandle;
/**
* Optional per-symbol patch report.
*
* symbol_name points into the handle and remains valid for the handle lifetime.
* reserved_size is the linked template function-body size. replacement_size is
* the size copied from the replacement RDC ELF.
*/
typedef struct {
const char* symbol_name;
size_t reserved_size;
size_t replacement_size;
} CubinFunctionPatchReport;
/**
* Return a stable string for a CubinFunctionPatchResult value.
*/
CUBIN_FUNCTION_PATCH_PUBLIC_DEC const char* cubin_function_patch_result_to_string(CubinFunctionPatchResult result);
/**
* Measure caller-provided memory required for a patch handle.
*
* symbol_names is the list of function symbols that will be patchable. The same
* list must be passed to cubin_function_patch_create. The returned byte count
* includes storage for copied symbol names.
*/
CUBIN_FUNCTION_PATCH_PUBLIC_DEC CubinFunctionPatchResult
cubin_function_patch_handle_size(
const char* const* symbol_names,
size_t num_symbols,
size_t* bytes_out
);
/**
* Create a patch handle by scanning a fully linked cubin template once.
*
* linked_cubin must be a fully linked CUDA cubin/ELF containing each requested
* symbol as an STT_FUNC symbol in an executable section. handle_memory is caller
* owned and must be at least the size returned by cubin_function_patch_handle_size.
*
* The function records each symbol's linked text offset and reserved size. It
* does not copy linked_cubin; keep linked_cubin alive for the handle lifetime.
*/
CUBIN_FUNCTION_PATCH_PUBLIC_DEC CubinFunctionPatchResult
cubin_function_patch_create(
const void* linked_cubin,
size_t linked_cubin_bytes,
const char* const* symbol_names,
size_t num_symbols,
void* handle_memory,
size_t handle_memory_size,
CubinFunctionPatchHandle** handle_out
);
/**
* Destroy a handle created by cubin_function_patch_create.
*
* This currently performs no deallocation because handle memory is caller-owned.
* It exists to keep the API symmetric if future handle cleanup is needed.
*/
CUBIN_FUNCTION_PATCH_PUBLIC_DEC void cubin_function_patch_destroy(CubinFunctionPatchHandle* handle);
/**
* Return the number of patchable function sites stored in the handle.
*/
CUBIN_FUNCTION_PATCH_PUBLIC_DEC size_t cubin_function_patch_num_sites(const CubinFunctionPatchHandle* handle);
/**
* Return the symbol name for a patch site by index, or NULL for an invalid index.
*
* The returned pointer is owned by the handle and remains valid for the handle
* lifetime.
*/
CUBIN_FUNCTION_PATCH_PUBLIC_DEC const char* cubin_function_patch_site_symbol(const CubinFunctionPatchHandle* handle, size_t site_index);
/**
* Return the reserved linked function-body size for a patch site.
*
* This is useful for checking whether a worst-case template is large enough for
* expected replacement functions.
*/
CUBIN_FUNCTION_PATCH_PUBLIC_DEC CubinFunctionPatchResult
cubin_function_patch_site_reserved_size(
const CubinFunctionPatchHandle* handle,
size_t site_index,
size_t* bytes_out
);
/**
* Return the number of bytes required for an output cubin buffer.
*
* The output cubin has the same byte size as the linked template cubin.
*/
CUBIN_FUNCTION_PATCH_PUBLIC_DEC CubinFunctionPatchResult
cubin_function_patch_output_size(
const CubinFunctionPatchHandle* handle,
size_t* bytes_out
);
/**
* Copy the linked template cubin into an output cubin buffer.
*
* Use this when patching multiple independent replacement RDC objects into the
* same output cubin with the in-place patch calls. For the common case where
* one replacement RDC provides all functions, use cubin_function_patch_apply_all.
*/
CUBIN_FUNCTION_PATCH_PUBLIC_DEC CubinFunctionPatchResult
cubin_function_patch_begin(
const CubinFunctionPatchHandle* handle,
void* output_cubin,
size_t output_cubin_capacity,
size_t* output_cubin_bytes_out
);
/**
* Patch one function in an existing output cubin buffer.
*
* output_cubin should usually be initialized by cubin_function_patch_begin.
* replacement_rdc_elf must be nvPTXCompiler --compile-only output containing a
* same-named self-contained STT_FUNC body. The replacement is rejected if it is
* larger than the reserved body, targets a different ELF machine, or has a
* relocation targeting the replacement function text.
*
* If the replacement is smaller than the reserved body, bytes after copied
* replacement representations are left as they were in the template. The
* reserved template must therefore use safe tail padding and the replacement
* must not branch into that tail. Use cubin_function_patch_apply_one_in_place_ex with
* CUBIN_FUNCTION_PATCH_TAIL_REQUIRE_EXACT_SIZE to reject smaller replacements.
*/
CUBIN_FUNCTION_PATCH_PUBLIC_DEC CubinFunctionPatchResult
cubin_function_patch_apply_one_in_place(
const CubinFunctionPatchHandle* handle,
void* output_cubin,
size_t output_cubin_bytes,
const char* symbol_name,
const void* replacement_rdc_elf,
size_t replacement_rdc_elf_bytes,
CubinFunctionPatchReport* report_out
);
/**
* Patch one function in an existing output cubin buffer with an explicit tail policy.
*/
CUBIN_FUNCTION_PATCH_PUBLIC_DEC CubinFunctionPatchResult
cubin_function_patch_apply_one_in_place_ex(
const CubinFunctionPatchHandle* handle,
void* output_cubin,
size_t output_cubin_bytes,
const char* symbol_name,
const void* replacement_rdc_elf,
size_t replacement_rdc_elf_bytes,
CubinFunctionPatchTailPolicy tail_policy,
CubinFunctionPatchReport* report_out
);
/**
* Patch every site in the handle from one replacement RDC ELF.
*
* The replacement RDC must contain every symbol in the handle. reports may be
* NULL. If reports is not NULL, report_count must be at least the handle site
* count.
*
* On failure, patches before the failing site may already have been applied.
*/
CUBIN_FUNCTION_PATCH_PUBLIC_DEC CubinFunctionPatchResult
cubin_function_patch_apply_all_in_place(
const CubinFunctionPatchHandle* handle,
void* output_cubin,
size_t output_cubin_bytes,
const void* replacement_rdc_elf,
size_t replacement_rdc_elf_bytes,
CubinFunctionPatchReport* reports,
size_t report_count
);
/**
* Patch every site in the handle from one replacement RDC ELF with an explicit tail policy.
*/
CUBIN_FUNCTION_PATCH_PUBLIC_DEC CubinFunctionPatchResult
cubin_function_patch_apply_all_in_place_ex(
const CubinFunctionPatchHandle* handle,
void* output_cubin,
size_t output_cubin_bytes,
const void* replacement_rdc_elf,
size_t replacement_rdc_elf_bytes,
CubinFunctionPatchTailPolicy tail_policy,
CubinFunctionPatchReport* reports,
size_t report_count
);
/**
* Copy the template cubin and patch one function into the output buffer.
*
* This is the one-shot equivalent of cubin_function_patch_begin followed by
* cubin_function_patch_apply_one_in_place.
*
* The output buffer receives a copy of the template before patching. If patching
* fails, the buffer may contain an unpatched template cubin.
*/
CUBIN_FUNCTION_PATCH_PUBLIC_DEC CubinFunctionPatchResult
cubin_function_patch_apply_one(
const CubinFunctionPatchHandle* handle,
const char* symbol_name,
const void* replacement_rdc_elf,
size_t replacement_rdc_elf_bytes,
void* output_cubin,
size_t output_cubin_capacity,
size_t* output_cubin_bytes_out,
CubinFunctionPatchReport* report_out
);
/**
* Copy the template cubin and patch one function with an explicit tail policy.
*/
CUBIN_FUNCTION_PATCH_PUBLIC_DEC CubinFunctionPatchResult
cubin_function_patch_apply_one_ex(
const CubinFunctionPatchHandle* handle,
const char* symbol_name,
const void* replacement_rdc_elf,
size_t replacement_rdc_elf_bytes,
CubinFunctionPatchTailPolicy tail_policy,
void* output_cubin,
size_t output_cubin_capacity,
size_t* output_cubin_bytes_out,
CubinFunctionPatchReport* report_out
);
/**
* Copy the template cubin and patch every site from one replacement RDC ELF.
*
* This is the simplest hot-path call when one replacement object contains every
* function body that should change.
*
* The output buffer receives a copy of the template before patching. If patching
* fails, the buffer may contain a partially patched cubin.
*/
CUBIN_FUNCTION_PATCH_PUBLIC_DEC CubinFunctionPatchResult
cubin_function_patch_apply_all(
const CubinFunctionPatchHandle* handle,
const void* replacement_rdc_elf,
size_t replacement_rdc_elf_bytes,
void* output_cubin,
size_t output_cubin_capacity,
size_t* output_cubin_bytes_out,
CubinFunctionPatchReport* reports,
size_t report_count
);
/**
* Copy the template cubin and patch every site with an explicit tail policy.
*/
CUBIN_FUNCTION_PATCH_PUBLIC_DEC CubinFunctionPatchResult
cubin_function_patch_apply_all_ex(
const CubinFunctionPatchHandle* handle,
const void* replacement_rdc_elf,
size_t replacement_rdc_elf_bytes,
CubinFunctionPatchTailPolicy tail_policy,
void* output_cubin,
size_t output_cubin_capacity,
size_t* output_cubin_bytes_out,
CubinFunctionPatchReport* reports,
size_t report_count
);
#endif /* CUBIN_FUNCTION_PATCH_H_INCLUDE */
#ifdef CUBIN_FUNCTION_PATCH_IMPLEMENTATION
#ifndef CUBIN_FUNCTION_PATCH_IMPLEMENTATION_ONCE
#define CUBIN_FUNCTION_PATCH_IMPLEMENTATION_ONCE
#include <elf.h>
#include <string.h>
typedef struct {
size_t linked_offset;
size_t reserved_size;
size_t section_index;
uint64_t section_offset;
int kind;
} CubinFunctionPatchSideSection;
enum {
CUBIN_FUNCTION_PATCH_SIDE_SECTION_INFO = 1,
CUBIN_FUNCTION_PATCH_SIDE_SECTION_CONSTANT2 = 2,
CUBIN_FUNCTION_PATCH_MAX_SIDE_SECTIONS = 4
};
typedef struct {
char* symbol_name;
size_t symbol_index;
size_t symbol_table_index;
size_t linked_offset;
size_t reserved_size;
size_t section_index;
uint64_t section_offset;
size_t capmerc_linked_offset;
size_t capmerc_reserved_size;
size_t capmerc_section_index;
uint64_t capmerc_section_offset;
size_t num_side_sections;
CubinFunctionPatchSideSection side_sections[CUBIN_FUNCTION_PATCH_MAX_SIDE_SECTIONS];
int has_capmerc;
} CubinFunctionPatchSite;
struct CubinFunctionPatchHandle {
const unsigned char* linked_cubin;
size_t linked_cubin_bytes;
uint16_t elf_machine;
uint32_t elf_flags;
uint32_t sm_version;
size_t num_sites;
CubinFunctionPatchSite sites[1];
};
typedef struct {
size_t file_offset;
size_t size;
size_t section_index;
uint64_t section_offset;
} CubinFunctionPatchSpan;
#ifndef CUBIN_FUNCTION_PATCH_SHT_NV_CAPMERC_TEXT
#define CUBIN_FUNCTION_PATCH_SHT_NV_CAPMERC_TEXT ((Elf64_Word)(SHT_LOPROC + 0x16u))
#endif
#ifndef CUBIN_FUNCTION_PATCH_SHT_NV_MERC_RELA
#define CUBIN_FUNCTION_PATCH_SHT_NV_MERC_RELA ((Elf64_Word)(SHT_LOPROC + 0x82u))
#endif
#ifndef CUBIN_FUNCTION_PATCH_SHT_CUDA_INFO
#define CUBIN_FUNCTION_PATCH_SHT_CUDA_INFO ((Elf64_Word)(SHT_LOPROC + 0x0u))
#endif
#ifndef CUBIN_FUNCTION_PATCH_SHT_CUDA_CONSTANT_B2
#define CUBIN_FUNCTION_PATCH_SHT_CUDA_CONSTANT_B2 ((Elf64_Word)(SHT_LOPROC + 0x66u))
#endif
#ifndef CUBIN_FUNCTION_PATCH_R_CUDA_CONST_BANK
#define CUBIN_FUNCTION_PATCH_R_CUDA_CONST_BANK 0x42u
#endif
#define CUBIN_FUNCTION_PATCH_EIFMT_SVAL 0x04u
#define CUBIN_FUNCTION_PATCH_EIATTR_FRAME_SIZE 0x11u
#define CUBIN_FUNCTION_PATCH_EIATTR_MIN_STACK_SIZE 0x12u
#define CUBIN_FUNCTION_PATCH_EIATTR_MAX_STACK_SIZE 0x23u
#define CUBIN_FUNCTION_PATCH_EIATTR_REGCOUNT 0x2fu
static size_t
cubin_function_patch_strnlen(
const char* s,
size_t max_len
) {
size_t i;
if (s == NULL) return 0u;
for (i = 0u; i < max_len; ++i) {
if (s[i] == '\0') return i;
}
return max_len;
}
static size_t
cubin_function_patch_strlen(
const char* s
) {
size_t n = 0u;
if (s == NULL) return 0u;
while (s[n] != '\0') n++;
return n;
}
static CubinFunctionPatchResult
cubin_function_patch_align_up_size(
size_t value,
size_t align,
size_t* out
) {
size_t mask;
size_t aligned;
if (out == NULL || align == 0u || (align & (align - 1u)) != 0u) {
return CUBIN_FUNCTION_PATCH_ERROR_INVALID_VALUE;
}
mask = align - 1u;
if (value > (size_t)-1 - mask) return CUBIN_FUNCTION_PATCH_ERROR_INVALID_VALUE;
aligned = (value + mask) & ~mask;
if (aligned < value) return CUBIN_FUNCTION_PATCH_ERROR_INVALID_VALUE;
*out = aligned;
return CUBIN_FUNCTION_PATCH_SUCCESS;
}
static CubinFunctionPatchResult
cubin_function_patch_add_size(
size_t a,
size_t b,
size_t* out
) {
if (out == NULL) return CUBIN_FUNCTION_PATCH_ERROR_INVALID_VALUE;
if (b > (size_t)-1 - a) return CUBIN_FUNCTION_PATCH_ERROR_INVALID_VALUE;
*out = a + b;
return CUBIN_FUNCTION_PATCH_SUCCESS;
}
static CubinFunctionPatchResult
cubin_function_patch_mul_size(
size_t a,
size_t b,
size_t* out
) {
if (out == NULL) return CUBIN_FUNCTION_PATCH_ERROR_INVALID_VALUE;
if (a != 0u && b > (size_t)-1 / a) return CUBIN_FUNCTION_PATCH_ERROR_INVALID_VALUE;
*out = a * b;
return CUBIN_FUNCTION_PATCH_SUCCESS;
}
static CubinFunctionPatchResult
cubin_function_patch_u64_to_size(
uint64_t value,
size_t* out
) {
if (out == NULL) return CUBIN_FUNCTION_PATCH_ERROR_INVALID_VALUE;
if ((uint64_t)(size_t)value != value) return CUBIN_FUNCTION_PATCH_ERROR_INVALID_ELF;
*out = (size_t)value;
return CUBIN_FUNCTION_PATCH_SUCCESS;
}
static int
cubin_function_patch_range_ok(
size_t offset,
size_t bytes,
size_t total
) {
if (offset > total) return 0;
if (bytes > total - offset) return 0;
return 1;
}
static CubinFunctionPatchResult
cubin_function_patch_check_symbol_names(
const char* const* symbol_names,
size_t num_symbols
) {
size_t i;
size_t j;
if (symbol_names == NULL || num_symbols == 0u) return CUBIN_FUNCTION_PATCH_ERROR_INVALID_VALUE;
for (i = 0u; i < num_symbols; ++i) {
if (symbol_names[i] == NULL || symbol_names[i][0] == '\0') {
return CUBIN_FUNCTION_PATCH_ERROR_INVALID_VALUE;
}
for (j = 0u; j < i; ++j) {
if (strcmp(symbol_names[i], symbol_names[j]) == 0) {
return CUBIN_FUNCTION_PATCH_ERROR_AMBIGUOUS_SYMBOL;
}
}
}
return CUBIN_FUNCTION_PATCH_SUCCESS;
}
static int
cubin_function_patch_read(
const void* data,
size_t bytes,
size_t offset,
void* out,
size_t out_bytes
) {
if (data == NULL || out == NULL) return 0;
if (!cubin_function_patch_range_ok(offset, out_bytes, bytes)) return 0;
memcpy(out, (const unsigned char*)data + offset, out_bytes);
return 1;
}
static uint16_t
cubin_function_patch_read_u16_unaligned(
const unsigned char* p
) {
uint16_t value;
memcpy(&value, p, sizeof(value));
return value;
}
static uint32_t
cubin_function_patch_read_u32_unaligned(
const unsigned char* p
) {
uint32_t value;
memcpy(&value, p, sizeof(value));
return value;
}
static void
cubin_function_patch_write_u32_unaligned(
unsigned char* p,
uint32_t value
) {
memcpy(p, &value, sizeof(value));
}
static uint32_t
cubin_function_patch_sm_from_elf_flags(
uint32_t flags
) {
uint32_t sm = (flags >> 8u) & 0xffu;
if (sm >= 30u) return sm;
sm = flags & 0xffu;
if (sm >= 30u) return sm;
sm = (flags >> 16u) & 0xffu;
if (sm >= 30u) return sm;
return 0u;
}
static CubinFunctionPatchResult
cubin_function_patch_read_ehdr(
const void* elf_data,
size_t elf_bytes,
Elf64_Ehdr* eh_out
) {
Elf64_Ehdr eh;
size_t section_offset;
size_t section_bytes;
CubinFunctionPatchResult result;
if (!cubin_function_patch_read(elf_data, elf_bytes, 0u, &eh, sizeof(eh))) {
return CUBIN_FUNCTION_PATCH_ERROR_INVALID_ELF;
}
if (memcmp(eh.e_ident, ELFMAG, SELFMAG) != 0) return CUBIN_FUNCTION_PATCH_ERROR_INVALID_ELF;
if (eh.e_ident[EI_CLASS] != ELFCLASS64) return CUBIN_FUNCTION_PATCH_ERROR_INVALID_ELF;
if (eh.e_ident[EI_DATA] != ELFDATA2LSB) return CUBIN_FUNCTION_PATCH_ERROR_INVALID_ELF;
if (eh.e_shentsize != sizeof(Elf64_Shdr)) return CUBIN_FUNCTION_PATCH_ERROR_INVALID_ELF;
if (eh.e_shnum == 0u) return CUBIN_FUNCTION_PATCH_ERROR_INVALID_ELF;
result = cubin_function_patch_u64_to_size(eh.e_shoff, §ion_offset);
if (result != CUBIN_FUNCTION_PATCH_SUCCESS) return result;
result = cubin_function_patch_mul_size((size_t)eh.e_shnum, sizeof(Elf64_Shdr), §ion_bytes);
if (result != CUBIN_FUNCTION_PATCH_SUCCESS) return CUBIN_FUNCTION_PATCH_ERROR_INVALID_ELF;
if (!cubin_function_patch_range_ok(section_offset, section_bytes, elf_bytes)) {
return CUBIN_FUNCTION_PATCH_ERROR_INVALID_ELF;
}
*eh_out = eh;
return CUBIN_FUNCTION_PATCH_SUCCESS;
}
static CubinFunctionPatchResult
cubin_function_patch_read_shdr(
const void* elf_data,
size_t elf_bytes,
const Elf64_Ehdr* eh,
size_t section_index,
Elf64_Shdr* sh_out
) {
size_t offset;
size_t section_table_offset;
size_t section_delta;
CubinFunctionPatchResult result;
if (eh == NULL || sh_out == NULL || section_index >= eh->e_shnum) {
return CUBIN_FUNCTION_PATCH_ERROR_INVALID_ELF;
}
result = cubin_function_patch_u64_to_size(eh->e_shoff, §ion_table_offset);
if (result != CUBIN_FUNCTION_PATCH_SUCCESS) return result;
result = cubin_function_patch_mul_size(section_index, sizeof(Elf64_Shdr), §ion_delta);
if (result != CUBIN_FUNCTION_PATCH_SUCCESS) return CUBIN_FUNCTION_PATCH_ERROR_INVALID_ELF;
result = cubin_function_patch_add_size(section_table_offset, section_delta, &offset);
if (result != CUBIN_FUNCTION_PATCH_SUCCESS) return CUBIN_FUNCTION_PATCH_ERROR_INVALID_ELF;
if (!cubin_function_patch_read(elf_data, elf_bytes, offset, sh_out, sizeof(*sh_out))) {
return CUBIN_FUNCTION_PATCH_ERROR_INVALID_ELF;
}
return CUBIN_FUNCTION_PATCH_SUCCESS;
}
static CubinFunctionPatchResult
cubin_function_patch_read_sym(
const void* elf_data,
size_t elf_bytes,
const Elf64_Shdr* symtab,
size_t symbol_index,
Elf64_Sym* sym_out
) {
size_t offset;
size_t table_offset;
size_t symbol_delta;
CubinFunctionPatchResult result;
if (symtab == NULL || sym_out == NULL || symtab->sh_entsize != sizeof(Elf64_Sym)) {
return CUBIN_FUNCTION_PATCH_ERROR_INVALID_ELF;
}
result = cubin_function_patch_u64_to_size(symtab->sh_offset, &table_offset);
if (result != CUBIN_FUNCTION_PATCH_SUCCESS) return result;
result = cubin_function_patch_mul_size(symbol_index, sizeof(Elf64_Sym), &symbol_delta);
if (result != CUBIN_FUNCTION_PATCH_SUCCESS) return CUBIN_FUNCTION_PATCH_ERROR_INVALID_ELF;
result = cubin_function_patch_add_size(table_offset, symbol_delta, &offset);
if (result != CUBIN_FUNCTION_PATCH_SUCCESS) return CUBIN_FUNCTION_PATCH_ERROR_INVALID_ELF;
if (!cubin_function_patch_read(elf_data, elf_bytes, offset, sym_out, sizeof(*sym_out))) {
return CUBIN_FUNCTION_PATCH_ERROR_INVALID_ELF;
}
return CUBIN_FUNCTION_PATCH_SUCCESS;
}
static int
cubin_function_patch_symbol_name_equals(
const void* elf_data,
size_t elf_bytes,
const Elf64_Shdr* strtab,
Elf64_Word name_offset,
const char* name
) {
const char* symbol_name;
size_t remaining;
size_t strtab_offset;
size_t strtab_size;
CubinFunctionPatchResult result;
if (strtab == NULL || name == NULL) return 0;
if (name_offset >= strtab->sh_size) return 0;
result = cubin_function_patch_u64_to_size(strtab->sh_offset, &strtab_offset);
if (result != CUBIN_FUNCTION_PATCH_SUCCESS) return 0;
result = cubin_function_patch_u64_to_size(strtab->sh_size, &strtab_size);
if (result != CUBIN_FUNCTION_PATCH_SUCCESS) return 0;
if (!cubin_function_patch_range_ok(strtab_offset, strtab_size, elf_bytes)) return 0;
symbol_name = (const char*)elf_data + strtab_offset + (size_t)name_offset;
remaining = strtab_size - (size_t)name_offset;
return cubin_function_patch_strnlen(symbol_name, remaining) < remaining && strcmp(symbol_name, name) == 0;
}
static int
cubin_function_patch_section_name_matches_prefix_symbol(
const void* elf_data,
size_t elf_bytes,
const Elf64_Ehdr* eh,
size_t section_index,
const char* prefix,
const char* symbol_name
) {
Elf64_Shdr shstrtab;
Elf64_Shdr section;
const char* section_name;
size_t section_name_len;
size_t prefix_len;
size_t symbol_len;
size_t shstrtab_offset;
size_t shstrtab_size;
size_t remaining;
CubinFunctionPatchResult result;
if (elf_data == NULL || eh == NULL || prefix == NULL || symbol_name == NULL) return 0;
if (eh->e_shstrndx == SHN_UNDEF || eh->e_shstrndx >= eh->e_shnum) return 0;
result = cubin_function_patch_read_shdr(elf_data, elf_bytes, eh, eh->e_shstrndx, &shstrtab);
if (result != CUBIN_FUNCTION_PATCH_SUCCESS || shstrtab.sh_type != SHT_STRTAB) return 0;
result = cubin_function_patch_read_shdr(elf_data, elf_bytes, eh, section_index, §ion);
if (result != CUBIN_FUNCTION_PATCH_SUCCESS) return 0;
if (section.sh_name >= shstrtab.sh_size) return 0;
result = cubin_function_patch_u64_to_size(shstrtab.sh_offset, &shstrtab_offset);
if (result != CUBIN_FUNCTION_PATCH_SUCCESS) return 0;
result = cubin_function_patch_u64_to_size(shstrtab.sh_size, &shstrtab_size);
if (result != CUBIN_FUNCTION_PATCH_SUCCESS) return 0;
if (!cubin_function_patch_range_ok(shstrtab_offset, shstrtab_size, elf_bytes)) return 0;
section_name = (const char*)elf_data + shstrtab_offset + (size_t)section.sh_name;
remaining = shstrtab_size - (size_t)section.sh_name;
section_name_len = cubin_function_patch_strnlen(section_name, remaining);
if (section_name_len >= remaining) return 0;
prefix_len = cubin_function_patch_strlen(prefix);
symbol_len = cubin_function_patch_strlen(symbol_name);
if (section_name_len != prefix_len + symbol_len) return 0;
if (memcmp(section_name, prefix, prefix_len) != 0) return 0;
if (memcmp(section_name + prefix_len, symbol_name, symbol_len) != 0) return 0;
return 1;
}
static int
cubin_function_patch_section_name_starts_with(
const void* elf_data,
size_t elf_bytes,
const Elf64_Ehdr* eh,
size_t section_index,
const char* prefix
) {
Elf64_Shdr shstrtab;
Elf64_Shdr section;
const char* section_name;
size_t section_name_len;
size_t prefix_len;
size_t shstrtab_offset;
size_t shstrtab_size;
size_t remaining;
CubinFunctionPatchResult result;
if (elf_data == NULL || eh == NULL || prefix == NULL) return 0;
if (eh->e_shstrndx == SHN_UNDEF || eh->e_shstrndx >= eh->e_shnum) return 0;
result = cubin_function_patch_read_shdr(elf_data, elf_bytes, eh, eh->e_shstrndx, &shstrtab);
if (result != CUBIN_FUNCTION_PATCH_SUCCESS || shstrtab.sh_type != SHT_STRTAB) return 0;
result = cubin_function_patch_read_shdr(elf_data, elf_bytes, eh, section_index, §ion);
if (result != CUBIN_FUNCTION_PATCH_SUCCESS) return 0;
if (section.sh_name >= shstrtab.sh_size) return 0;
result = cubin_function_patch_u64_to_size(shstrtab.sh_offset, &shstrtab_offset);
if (result != CUBIN_FUNCTION_PATCH_SUCCESS) return 0;
result = cubin_function_patch_u64_to_size(shstrtab.sh_size, &shstrtab_size);
if (result != CUBIN_FUNCTION_PATCH_SUCCESS) return 0;
if (!cubin_function_patch_range_ok(shstrtab_offset, shstrtab_size, elf_bytes)) return 0;
section_name = (const char*)elf_data + shstrtab_offset + (size_t)section.sh_name;
remaining = shstrtab_size - (size_t)section.sh_name;
section_name_len = cubin_function_patch_strnlen(section_name, remaining);
if (section_name_len >= remaining) return 0;
prefix_len = cubin_function_patch_strlen(prefix);
if (section_name_len < prefix_len) return 0;
return memcmp(section_name, prefix, prefix_len) == 0;
}
static int
cubin_function_patch_section_name_equals(
const void* elf_data,
size_t elf_bytes,
const Elf64_Ehdr* eh,
size_t section_index,
const char* expected_name
) {
Elf64_Shdr shstrtab;
Elf64_Shdr section;
const char* section_name;
size_t section_name_len;
size_t expected_len;
size_t shstrtab_offset;
size_t shstrtab_size;
size_t remaining;
CubinFunctionPatchResult result;
if (elf_data == NULL || eh == NULL || expected_name == NULL) return 0;
if (eh->e_shstrndx == SHN_UNDEF || eh->e_shstrndx >= eh->e_shnum) return 0;
result = cubin_function_patch_read_shdr(elf_data, elf_bytes, eh, eh->e_shstrndx, &shstrtab);
if (result != CUBIN_FUNCTION_PATCH_SUCCESS || shstrtab.sh_type != SHT_STRTAB) return 0;
result = cubin_function_patch_read_shdr(elf_data, elf_bytes, eh, section_index, §ion);
if (result != CUBIN_FUNCTION_PATCH_SUCCESS) return 0;
if (section.sh_name >= shstrtab.sh_size) return 0;
result = cubin_function_patch_u64_to_size(shstrtab.sh_offset, &shstrtab_offset);
if (result != CUBIN_FUNCTION_PATCH_SUCCESS) return 0;
result = cubin_function_patch_u64_to_size(shstrtab.sh_size, &shstrtab_size);
if (result != CUBIN_FUNCTION_PATCH_SUCCESS) return 0;
if (!cubin_function_patch_range_ok(shstrtab_offset, shstrtab_size, elf_bytes)) return 0;
section_name = (const char*)elf_data + shstrtab_offset + (size_t)section.sh_name;
remaining = shstrtab_size - (size_t)section.sh_name;
section_name_len = cubin_function_patch_strnlen(section_name, remaining);
if (section_name_len >= remaining) return 0;
expected_len = cubin_function_patch_strlen(expected_name);
if (section_name_len != expected_len) return 0;
return memcmp(section_name, expected_name, expected_len) == 0;
}
static int
cubin_function_patch_parse_decimal_suffix(
const char* text,
size_t* value_out
) {
const char* p;
const char* start;
size_t value = 0u;
if (text == NULL || text[0] == '\0' || value_out == NULL) return 0;
p = text + cubin_function_patch_strlen(text);
start = p;
while (start != text && start[-1] >= '0' && start[-1] <= '9') {
--start;
}
if (start == p) return 0;
while (start != p) {
size_t digit = (size_t)(*start - '0');
if (value > ((size_t)-1 - digit) / 10u) return 0;
value = value * 10u + digit;
++start;
}
*value_out = value;
return 1;
}
static int
cubin_function_patch_section_name_has_decimal_suffix(
const void* elf_data,
size_t elf_bytes,
const Elf64_Ehdr* eh,
size_t section_index,
const char* prefix,
size_t requested_suffix
) {
Elf64_Shdr shstrtab;
Elf64_Shdr section;
const char* section_name;
size_t section_name_len;
size_t prefix_len;
size_t suffix = 0u;
size_t shstrtab_offset;
size_t shstrtab_size;
size_t remaining;
CubinFunctionPatchResult result;
if (elf_data == NULL || eh == NULL || prefix == NULL) return 0;
if (eh->e_shstrndx == SHN_UNDEF || eh->e_shstrndx >= eh->e_shnum) return 0;
result = cubin_function_patch_read_shdr(elf_data, elf_bytes, eh, eh->e_shstrndx, &shstrtab);
if (result != CUBIN_FUNCTION_PATCH_SUCCESS || shstrtab.sh_type != SHT_STRTAB) return 0;
result = cubin_function_patch_read_shdr(elf_data, elf_bytes, eh, section_index, §ion);
if (result != CUBIN_FUNCTION_PATCH_SUCCESS) return 0;
if (section.sh_name >= shstrtab.sh_size) return 0;
result = cubin_function_patch_u64_to_size(shstrtab.sh_offset, &shstrtab_offset);
if (result != CUBIN_FUNCTION_PATCH_SUCCESS) return 0;
result = cubin_function_patch_u64_to_size(shstrtab.sh_size, &shstrtab_size);
if (result != CUBIN_FUNCTION_PATCH_SUCCESS) return 0;
if (!cubin_function_patch_range_ok(shstrtab_offset, shstrtab_size, elf_bytes)) return 0;
section_name = (const char*)elf_data + shstrtab_offset + (size_t)section.sh_name;
remaining = shstrtab_size - (size_t)section.sh_name;
section_name_len = cubin_function_patch_strnlen(section_name, remaining);
if (section_name_len >= remaining) return 0;
prefix_len = cubin_function_patch_strlen(prefix);
if (section_name_len < prefix_len) return 0;
if (memcmp(section_name, prefix, prefix_len) != 0) return 0;
if (!cubin_function_patch_parse_decimal_suffix(section_name, &suffix)) return 0;
return suffix == requested_suffix;
}
static CubinFunctionPatchResult
cubin_function_patch_find_capmerc_span(
const void* elf_data,
size_t elf_bytes,
const char* symbol_name,
CubinFunctionPatchSpan* span_out
) {
static const char prefix[] = ".nv.capmerc.text.";
Elf64_Ehdr eh;
CubinFunctionPatchResult result;
size_t i;
size_t match_count = 0u;
CubinFunctionPatchSpan matched_span;
if (symbol_name == NULL || symbol_name[0] == '\0' || span_out == NULL) return CUBIN_FUNCTION_PATCH_ERROR_INVALID_VALUE;
memset(span_out, 0, sizeof(*span_out));
memset(&matched_span, 0, sizeof(matched_span));
result = cubin_function_patch_read_ehdr(elf_data, elf_bytes, &eh);
if (result != CUBIN_FUNCTION_PATCH_SUCCESS) return result;
for (i = 0u; i < eh.e_shnum; ++i) {
Elf64_Shdr section;
size_t section_offset;
size_t section_size;
result = cubin_function_patch_read_shdr(elf_data, elf_bytes, &eh, i, §ion);
if (result != CUBIN_FUNCTION_PATCH_SUCCESS) return result;
if (section.sh_type != CUBIN_FUNCTION_PATCH_SHT_NV_CAPMERC_TEXT) continue;
if (!cubin_function_patch_section_name_matches_prefix_symbol(elf_data, elf_bytes, &eh, i, prefix, symbol_name)) continue;
result = cubin_function_patch_u64_to_size(section.sh_offset, §ion_offset);
if (result != CUBIN_FUNCTION_PATCH_SUCCESS) return result;
result = cubin_function_patch_u64_to_size(section.sh_size, §ion_size);
if (result != CUBIN_FUNCTION_PATCH_SUCCESS) return result;
if (!cubin_function_patch_range_ok(section_offset, section_size, elf_bytes)) {
return CUBIN_FUNCTION_PATCH_ERROR_INVALID_ELF;
}
matched_span.file_offset = section_offset;
matched_span.size = section_size;
matched_span.section_index = i;
matched_span.section_offset = 0u;
match_count++;
}
if (match_count == 0u) return CUBIN_FUNCTION_PATCH_ERROR_SYMBOL_NOT_FOUND;
if (match_count != 1u) return CUBIN_FUNCTION_PATCH_ERROR_AMBIGUOUS_SYMBOL;
*span_out = matched_span;
return CUBIN_FUNCTION_PATCH_SUCCESS;
}
static const char*
cubin_function_patch_side_section_prefix(
int kind
) {
switch (kind) {
case CUBIN_FUNCTION_PATCH_SIDE_SECTION_INFO:
return ".nv.info.";
case CUBIN_FUNCTION_PATCH_SIDE_SECTION_CONSTANT2:
return ".nv.constant2.";
}
return NULL;
}
static Elf64_Word
cubin_function_patch_side_section_type(
int kind
) {
switch (kind) {
case CUBIN_FUNCTION_PATCH_SIDE_SECTION_INFO:
return CUBIN_FUNCTION_PATCH_SHT_CUDA_INFO;
case CUBIN_FUNCTION_PATCH_SIDE_SECTION_CONSTANT2:
return CUBIN_FUNCTION_PATCH_SHT_CUDA_CONSTANT_B2;
}
return SHT_NULL;
}
static int
cubin_function_patch_section_type_matches_side_kind(
Elf64_Word section_type,
int kind
) {
switch (kind) {
case CUBIN_FUNCTION_PATCH_SIDE_SECTION_INFO:
return section_type == CUBIN_FUNCTION_PATCH_SHT_CUDA_INFO;
case CUBIN_FUNCTION_PATCH_SIDE_SECTION_CONSTANT2:
return section_type == CUBIN_FUNCTION_PATCH_SHT_CUDA_CONSTANT_B2 ||
section_type == SHT_PROGBITS;
}
return 0;
}
static int
cubin_function_patch_site_has_side_kind(
const CubinFunctionPatchSite* site,
int kind
) {
size_t i;
if (site == NULL) return 0;
for (i = 0u; i < site->num_side_sections; ++i) {
if (site->side_sections[i].kind == kind) return 1;
}
return 0;
}
static CubinFunctionPatchResult