Skip to content

Commit 0b91a0e

Browse files
committed
linuxkm/linuxkm_wc_port.h, linuxkm/module_hooks.c, linuxkm/Makefile: refactor wc_linuxkm_normalize_relocations() and associated types and objects:
* change wc_linuxkm_pie_reloc_tab from unsigned int[] to struct wc_linuxkm_pie_reloc_tab_ent[], with dest_segment and reloc_type members; * add enum wc_reloc_dest_segment and enum wc_reloc_type; * update GENERATE_RELOC_TAB recipe in Makefile to render the dest segment and reloc type; * add struct reloc_layout_ent, and reloc_layouts[] fully populated for x86 and ARM relocations; * refactor find_reloc_tab_offset() and wc_linuxkm_normalize_relocations() to reflect the above; linuxkm/module_hooks.c: tweak various printf format characters and arguments for compatibility with ARM32; linuxkm/linuxkm_wc_port.h: include linux/inet.h and define wc_linuxkm_inet_pton() and XINET_PTON(), unless WOLFCRYPT_ONLY.
1 parent 8719df2 commit 0b91a0e

3 files changed

Lines changed: 452 additions & 158 deletions

File tree

linuxkm/Makefile

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ GENERATE_RELOC_TAB := $(AWK) ' \
148148
bad_relocs=0; \
149149
print "\#include <wolfssl/wolfcrypt/libwolfssl_sources.h>"; \
150150
printf("%s\n ", \
151-
"WOLFSSL_LOCAL const unsigned int wc_linuxkm_pie_reloc_tab[] = { "); \
151+
"WOLFSSL_LOCAL const struct wc_linuxkm_pie_reloc_tab_ent wc_linuxkm_pie_reloc_tab[] = { "); \
152152
if ("SECTION_MAP" in ENVIRON) { \
153153
while (getline <ENVIRON["SECTION_MAP"] > 0) \
154154
section_map[$$1] = $$2; \
@@ -177,44 +177,45 @@ GENERATE_RELOC_TAB := $(AWK) ' \
177177
if (section) { \
178178
switch (section) { \
179179
case ".text_wolfcrypt": \
180-
section_tag = 0; \
180+
section_tag = "WC_R_SEG_TEXT"; \
181181
break; \
182182
case ".rodata_wolfcrypt": \
183-
section_tag = 1; \
183+
section_tag = "WC_R_SEG_RODATA"; \
184184
break; \
185185
case ".data_wolfcrypt": \
186-
section_tag = 2; \
186+
section_tag = "WC_R_SEG_RWDATA"; \
187187
break; \
188188
case ".bss_wolfcrypt": \
189-
section_tag = 3; \
189+
section_tag = "WC_R_SEG_BSS"; \
190190
break; \
191191
default: \
192192
print "Unexpected section:\n" $$0 >"/dev/stderr"; \
193193
++bad_relocs; \
194-
section_tag = 4; \
194+
section_tag = "WC_R_SEG_OTHER"; \
195195
} \
196196
} else { \
197197
print "Unresolvable symbol reference for relocation:\n" $$0 >"/dev/stderr";\
198198
++bad_relocs; \
199-
section_tag = 4; \
199+
section_tag = "WC_R_SEG_OTHER"; \
200200
} \
201+
reloc_type = $$3; \
201202
if (strtonum("0x" gensub("^0*","",1,$$1)) >= lshift(1, 29)) { \
202203
print "Relocation offset overflow:" >"/dev/stderr"; \
203204
print >"/dev/stderr"; \
204205
exit(1); \
205206
} \
206-
printf("0x%xU%s", \
207-
or(strtonum("0x" gensub("^0*","",1,$$1)), \
208-
lshift(section_tag, 29)), \
209-
((++n%8) ? ", " : ",\n ")); \
207+
printf(" { .offset = 0x%xU, .dest_segment = %s, .reloc_type = WC_%s },\n", \
208+
strtonum("0x" gensub("^0*","",1,$$1)), \
209+
section_tag, reloc_type); \
210210
} \
211211
} \
212212
END { \
213213
if (bad_relocs) { \
214214
print "Found " bad_relocs " unresolvable relocations." >"/dev/stderr"; \
215215
exit(1); \
216216
} \
217-
print "~0U };\nWOLFSSL_LOCAL const unsigned long wc_linuxkm_pie_reloc_tab_length = sizeof wc_linuxkm_pie_reloc_tab / sizeof wc_linuxkm_pie_reloc_tab[0];";\
217+
print " { .offset = ~0U, .dest_segment = WC_R_SEG_NONE, .reloc_type = WC_R_NONE } };"; \
218+
print "WOLFSSL_LOCAL const unsigned long wc_linuxkm_pie_reloc_tab_length = sizeof wc_linuxkm_pie_reloc_tab / sizeof wc_linuxkm_pie_reloc_tab[0];"; \
218219
}'
219220

220221
ifeq "$(V)" "1"

linuxkm/linuxkm_wc_port.h

Lines changed: 73 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -483,6 +483,9 @@
483483
#ifndef WC_CONTAINERIZE_THIS
484484
#include <linux/kthread.h>
485485
#include <linux/net.h>
486+
#ifndef WOLFCRYPT_ONLY
487+
#include <linux/inet.h>
488+
#endif
486489
#endif
487490

488491
#include <linux/slab.h>
@@ -815,7 +818,52 @@
815818
__wc_bss_start[],
816819
__wc_bss_end[];
817820

818-
extern const unsigned int wc_linuxkm_pie_reloc_tab[];
821+
struct wc_linuxkm_pie_reloc_tab_ent {
822+
unsigned int offset;
823+
#define WC_RELOC_DEST_SEGMENT_BITS 3
824+
unsigned int dest_segment:WC_RELOC_DEST_SEGMENT_BITS;
825+
#define WC_RELOC_TYPE_BITS 5
826+
unsigned int reloc_type:WC_RELOC_TYPE_BITS;
827+
};
828+
829+
enum wc_reloc_dest_segment {
830+
WC_R_SEG_NONE = 0,
831+
WC_R_SEG_TEXT,
832+
WC_R_SEG_RODATA,
833+
WC_R_SEG_RWDATA,
834+
WC_R_SEG_BSS,
835+
WC_R_SEG_OTHER
836+
};
837+
838+
enum wc_reloc_type {
839+
WC_R_NONE = 0,
840+
WC_R_X86_64_32,
841+
WC_R_X86_64_32S,
842+
WC_R_X86_64_64,
843+
WC_R_X86_64_PC32,
844+
WC_R_X86_64_PLT32,
845+
WC_R_AARCH64_ABS32,
846+
WC_R_AARCH64_ABS64,
847+
WC_R_AARCH64_ADD_ABS_LO12_NC,
848+
WC_R_AARCH64_ADR_PREL_PG_HI21,
849+
WC_R_AARCH64_CALL26,
850+
WC_R_AARCH64_JUMP26,
851+
WC_R_AARCH64_LDST8_ABS_LO12_NC,
852+
WC_R_AARCH64_LDST16_ABS_LO12_NC,
853+
WC_R_AARCH64_LDST32_ABS_LO12_NC,
854+
WC_R_AARCH64_LDST64_ABS_LO12_NC,
855+
WC_R_AARCH64_PREL32,
856+
WC_R_ARM_ABS32,
857+
WC_R_ARM_PREL31,
858+
WC_R_ARM_REL32,
859+
WC_R_ARM_THM_CALL,
860+
WC_R_ARM_THM_JUMP11,
861+
WC_R_ARM_THM_JUMP24,
862+
WC_R_ARM_THM_MOVT_ABS,
863+
WC_R_ARM_THM_MOVW_ABS_NC
864+
};
865+
866+
extern const struct wc_linuxkm_pie_reloc_tab_ent wc_linuxkm_pie_reloc_tab[];
819867
extern const unsigned long wc_linuxkm_pie_reloc_tab_length;
820868
extern ssize_t wc_linuxkm_normalize_relocations(
821869
const u8 *text_in,
@@ -1738,4 +1786,28 @@
17381786
#define WC_DUMP_BACKTRACE_NONDEBUG dump_stack()
17391787
#endif
17401788

1789+
#if !defined(WOLFCRYPT_ONLY) && !defined(WC_CONTAINERIZE_THIS)
1790+
static inline int wc_linuxkm_inet_pton(int af, const char *src, void *dst)
1791+
{
1792+
int ret;
1793+
1794+
if (!src || !dst)
1795+
return -EFAULT;
1796+
1797+
switch (af) {
1798+
case AF_INET:
1799+
ret = in4_pton(src, -1, (u8 *)dst, '\0', NULL);
1800+
return ret == 1 ? 1 : 0;
1801+
1802+
case AF_INET6:
1803+
ret = in6_pton(src, -1, (u8 *)dst, '\0', NULL);
1804+
return ret == 1 ? 1 : 0;
1805+
1806+
default:
1807+
return -EAFNOSUPPORT;
1808+
}
1809+
}
1810+
#define XINET_PTON(af, src, dst) wc_linuxkm_inet_pton(af, src, dst)
1811+
#endif /* !WOLFCRYPT_ONLY && !WC_CONTAINERIZE_THIS */
1812+
17411813
#endif /* LINUXKM_WC_PORT_H */

0 commit comments

Comments
 (0)