linker.c 75 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900
  1. // SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause)
  2. /*
  3. * BPF static linker
  4. *
  5. * Copyright (c) 2021 Facebook
  6. */
  7. #include <stdbool.h>
  8. #include <stddef.h>
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include <unistd.h>
  13. #include <errno.h>
  14. #include <linux/err.h>
  15. #include <linux/btf.h>
  16. #include <elf.h>
  17. #include <libelf.h>
  18. #include <fcntl.h>
  19. #include "libbpf.h"
  20. #include "btf.h"
  21. #include "libbpf_internal.h"
  22. #include "strset.h"
  23. #define BTF_EXTERN_SEC ".extern"
  24. struct src_sec {
  25. const char *sec_name;
  26. /* positional (not necessarily ELF) index in an array of sections */
  27. int id;
  28. /* positional (not necessarily ELF) index of a matching section in a final object file */
  29. int dst_id;
  30. /* section data offset in a matching output section */
  31. int dst_off;
  32. /* whether section is omitted from the final ELF file */
  33. bool skipped;
  34. /* whether section is an ephemeral section, not mapped to an ELF section */
  35. bool ephemeral;
  36. /* ELF info */
  37. size_t sec_idx;
  38. Elf_Scn *scn;
  39. Elf64_Shdr *shdr;
  40. Elf_Data *data;
  41. /* corresponding BTF DATASEC type ID */
  42. int sec_type_id;
  43. };
  44. struct src_obj {
  45. const char *filename;
  46. int fd;
  47. Elf *elf;
  48. /* Section header strings section index */
  49. size_t shstrs_sec_idx;
  50. /* SYMTAB section index */
  51. size_t symtab_sec_idx;
  52. struct btf *btf;
  53. struct btf_ext *btf_ext;
  54. /* List of sections (including ephemeral). Slot zero is unused. */
  55. struct src_sec *secs;
  56. int sec_cnt;
  57. /* mapping of symbol indices from src to dst ELF */
  58. int *sym_map;
  59. /* mapping from the src BTF type IDs to dst ones */
  60. int *btf_type_map;
  61. };
  62. /* single .BTF.ext data section */
  63. struct btf_ext_sec_data {
  64. size_t rec_cnt;
  65. __u32 rec_sz;
  66. void *recs;
  67. };
  68. struct glob_sym {
  69. /* ELF symbol index */
  70. int sym_idx;
  71. /* associated section id for .ksyms, .kconfig, etc, but not .extern */
  72. int sec_id;
  73. /* extern name offset in STRTAB */
  74. int name_off;
  75. /* optional associated BTF type ID */
  76. int btf_id;
  77. /* BTF type ID to which VAR/FUNC type is pointing to; used for
  78. * rewriting types when extern VAR/FUNC is resolved to a concrete
  79. * definition
  80. */
  81. int underlying_btf_id;
  82. /* sec_var index in the corresponding dst_sec, if exists */
  83. int var_idx;
  84. /* extern or resolved/global symbol */
  85. bool is_extern;
  86. /* weak or strong symbol, never goes back from strong to weak */
  87. bool is_weak;
  88. };
  89. struct dst_sec {
  90. char *sec_name;
  91. /* positional (not necessarily ELF) index in an array of sections */
  92. int id;
  93. bool ephemeral;
  94. /* ELF info */
  95. size_t sec_idx;
  96. Elf_Scn *scn;
  97. Elf64_Shdr *shdr;
  98. Elf_Data *data;
  99. /* final output section size */
  100. int sec_sz;
  101. /* final output contents of the section */
  102. void *raw_data;
  103. /* corresponding STT_SECTION symbol index in SYMTAB */
  104. int sec_sym_idx;
  105. /* section's DATASEC variable info, emitted on BTF finalization */
  106. bool has_btf;
  107. int sec_var_cnt;
  108. struct btf_var_secinfo *sec_vars;
  109. /* section's .BTF.ext data */
  110. struct btf_ext_sec_data func_info;
  111. struct btf_ext_sec_data line_info;
  112. struct btf_ext_sec_data core_relo_info;
  113. };
  114. struct bpf_linker {
  115. char *filename;
  116. int fd;
  117. Elf *elf;
  118. Elf64_Ehdr *elf_hdr;
  119. /* Output sections metadata */
  120. struct dst_sec *secs;
  121. int sec_cnt;
  122. struct strset *strtab_strs; /* STRTAB unique strings */
  123. size_t strtab_sec_idx; /* STRTAB section index */
  124. size_t symtab_sec_idx; /* SYMTAB section index */
  125. struct btf *btf;
  126. struct btf_ext *btf_ext;
  127. /* global (including extern) ELF symbols */
  128. int glob_sym_cnt;
  129. struct glob_sym *glob_syms;
  130. };
  131. #define pr_warn_elf(fmt, ...) \
  132. libbpf_print(LIBBPF_WARN, "libbpf: " fmt ": %s\n", ##__VA_ARGS__, elf_errmsg(-1))
  133. static int init_output_elf(struct bpf_linker *linker, const char *file);
  134. static int linker_load_obj_file(struct bpf_linker *linker, const char *filename,
  135. const struct bpf_linker_file_opts *opts,
  136. struct src_obj *obj);
  137. static int linker_sanity_check_elf(struct src_obj *obj);
  138. static int linker_sanity_check_elf_symtab(struct src_obj *obj, struct src_sec *sec);
  139. static int linker_sanity_check_elf_relos(struct src_obj *obj, struct src_sec *sec);
  140. static int linker_sanity_check_btf(struct src_obj *obj);
  141. static int linker_sanity_check_btf_ext(struct src_obj *obj);
  142. static int linker_fixup_btf(struct src_obj *obj);
  143. static int linker_append_sec_data(struct bpf_linker *linker, struct src_obj *obj);
  144. static int linker_append_elf_syms(struct bpf_linker *linker, struct src_obj *obj);
  145. static int linker_append_elf_sym(struct bpf_linker *linker, struct src_obj *obj,
  146. Elf64_Sym *sym, const char *sym_name, int src_sym_idx);
  147. static int linker_append_elf_relos(struct bpf_linker *linker, struct src_obj *obj);
  148. static int linker_append_btf(struct bpf_linker *linker, struct src_obj *obj);
  149. static int linker_append_btf_ext(struct bpf_linker *linker, struct src_obj *obj);
  150. static int finalize_btf(struct bpf_linker *linker);
  151. static int finalize_btf_ext(struct bpf_linker *linker);
  152. void bpf_linker__free(struct bpf_linker *linker)
  153. {
  154. int i;
  155. if (!linker)
  156. return;
  157. free(linker->filename);
  158. if (linker->elf)
  159. elf_end(linker->elf);
  160. if (linker->fd >= 0)
  161. close(linker->fd);
  162. strset__free(linker->strtab_strs);
  163. btf__free(linker->btf);
  164. btf_ext__free(linker->btf_ext);
  165. for (i = 1; i < linker->sec_cnt; i++) {
  166. struct dst_sec *sec = &linker->secs[i];
  167. free(sec->sec_name);
  168. free(sec->raw_data);
  169. free(sec->sec_vars);
  170. free(sec->func_info.recs);
  171. free(sec->line_info.recs);
  172. free(sec->core_relo_info.recs);
  173. }
  174. free(linker->secs);
  175. free(linker->glob_syms);
  176. free(linker);
  177. }
  178. struct bpf_linker *bpf_linker__new(const char *filename, struct bpf_linker_opts *opts)
  179. {
  180. struct bpf_linker *linker;
  181. int err;
  182. if (!OPTS_VALID(opts, bpf_linker_opts))
  183. return errno = EINVAL, NULL;
  184. if (elf_version(EV_CURRENT) == EV_NONE) {
  185. pr_warn_elf("libelf initialization failed");
  186. return errno = EINVAL, NULL;
  187. }
  188. linker = calloc(1, sizeof(*linker));
  189. if (!linker)
  190. return errno = ENOMEM, NULL;
  191. linker->fd = -1;
  192. err = init_output_elf(linker, filename);
  193. if (err)
  194. goto err_out;
  195. return linker;
  196. err_out:
  197. bpf_linker__free(linker);
  198. return errno = -err, NULL;
  199. }
  200. static struct dst_sec *add_dst_sec(struct bpf_linker *linker, const char *sec_name)
  201. {
  202. struct dst_sec *secs = linker->secs, *sec;
  203. size_t new_cnt = linker->sec_cnt ? linker->sec_cnt + 1 : 2;
  204. secs = libbpf_reallocarray(secs, new_cnt, sizeof(*secs));
  205. if (!secs)
  206. return NULL;
  207. /* zero out newly allocated memory */
  208. memset(secs + linker->sec_cnt, 0, (new_cnt - linker->sec_cnt) * sizeof(*secs));
  209. linker->secs = secs;
  210. linker->sec_cnt = new_cnt;
  211. sec = &linker->secs[new_cnt - 1];
  212. sec->id = new_cnt - 1;
  213. sec->sec_name = strdup(sec_name);
  214. if (!sec->sec_name)
  215. return NULL;
  216. return sec;
  217. }
  218. static Elf64_Sym *add_new_sym(struct bpf_linker *linker, size_t *sym_idx)
  219. {
  220. struct dst_sec *symtab = &linker->secs[linker->symtab_sec_idx];
  221. Elf64_Sym *syms, *sym;
  222. size_t sym_cnt = symtab->sec_sz / sizeof(*sym);
  223. syms = libbpf_reallocarray(symtab->raw_data, sym_cnt + 1, sizeof(*sym));
  224. if (!syms)
  225. return NULL;
  226. sym = &syms[sym_cnt];
  227. memset(sym, 0, sizeof(*sym));
  228. symtab->raw_data = syms;
  229. symtab->sec_sz += sizeof(*sym);
  230. symtab->shdr->sh_size += sizeof(*sym);
  231. symtab->data->d_size += sizeof(*sym);
  232. if (sym_idx)
  233. *sym_idx = sym_cnt;
  234. return sym;
  235. }
  236. static int init_output_elf(struct bpf_linker *linker, const char *file)
  237. {
  238. int err, str_off;
  239. Elf64_Sym *init_sym;
  240. struct dst_sec *sec;
  241. linker->filename = strdup(file);
  242. if (!linker->filename)
  243. return -ENOMEM;
  244. linker->fd = open(file, O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC, 0644);
  245. if (linker->fd < 0) {
  246. err = -errno;
  247. pr_warn("failed to create '%s': %d\n", file, err);
  248. return err;
  249. }
  250. linker->elf = elf_begin(linker->fd, ELF_C_WRITE, NULL);
  251. if (!linker->elf) {
  252. pr_warn_elf("failed to create ELF object");
  253. return -EINVAL;
  254. }
  255. /* ELF header */
  256. linker->elf_hdr = elf64_newehdr(linker->elf);
  257. if (!linker->elf_hdr) {
  258. pr_warn_elf("failed to create ELF header");
  259. return -EINVAL;
  260. }
  261. linker->elf_hdr->e_machine = EM_BPF;
  262. linker->elf_hdr->e_type = ET_REL;
  263. #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
  264. linker->elf_hdr->e_ident[EI_DATA] = ELFDATA2LSB;
  265. #elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
  266. linker->elf_hdr->e_ident[EI_DATA] = ELFDATA2MSB;
  267. #else
  268. #error "Unknown __BYTE_ORDER__"
  269. #endif
  270. /* STRTAB */
  271. /* initialize strset with an empty string to conform to ELF */
  272. linker->strtab_strs = strset__new(INT_MAX, "", sizeof(""));
  273. if (libbpf_get_error(linker->strtab_strs))
  274. return libbpf_get_error(linker->strtab_strs);
  275. sec = add_dst_sec(linker, ".strtab");
  276. if (!sec)
  277. return -ENOMEM;
  278. sec->scn = elf_newscn(linker->elf);
  279. if (!sec->scn) {
  280. pr_warn_elf("failed to create STRTAB section");
  281. return -EINVAL;
  282. }
  283. sec->shdr = elf64_getshdr(sec->scn);
  284. if (!sec->shdr)
  285. return -EINVAL;
  286. sec->data = elf_newdata(sec->scn);
  287. if (!sec->data) {
  288. pr_warn_elf("failed to create STRTAB data");
  289. return -EINVAL;
  290. }
  291. str_off = strset__add_str(linker->strtab_strs, sec->sec_name);
  292. if (str_off < 0)
  293. return str_off;
  294. sec->sec_idx = elf_ndxscn(sec->scn);
  295. linker->elf_hdr->e_shstrndx = sec->sec_idx;
  296. linker->strtab_sec_idx = sec->sec_idx;
  297. sec->shdr->sh_name = str_off;
  298. sec->shdr->sh_type = SHT_STRTAB;
  299. sec->shdr->sh_flags = SHF_STRINGS;
  300. sec->shdr->sh_offset = 0;
  301. sec->shdr->sh_link = 0;
  302. sec->shdr->sh_info = 0;
  303. sec->shdr->sh_addralign = 1;
  304. sec->shdr->sh_size = sec->sec_sz = 0;
  305. sec->shdr->sh_entsize = 0;
  306. /* SYMTAB */
  307. sec = add_dst_sec(linker, ".symtab");
  308. if (!sec)
  309. return -ENOMEM;
  310. sec->scn = elf_newscn(linker->elf);
  311. if (!sec->scn) {
  312. pr_warn_elf("failed to create SYMTAB section");
  313. return -EINVAL;
  314. }
  315. sec->shdr = elf64_getshdr(sec->scn);
  316. if (!sec->shdr)
  317. return -EINVAL;
  318. sec->data = elf_newdata(sec->scn);
  319. if (!sec->data) {
  320. pr_warn_elf("failed to create SYMTAB data");
  321. return -EINVAL;
  322. }
  323. str_off = strset__add_str(linker->strtab_strs, sec->sec_name);
  324. if (str_off < 0)
  325. return str_off;
  326. sec->sec_idx = elf_ndxscn(sec->scn);
  327. linker->symtab_sec_idx = sec->sec_idx;
  328. sec->shdr->sh_name = str_off;
  329. sec->shdr->sh_type = SHT_SYMTAB;
  330. sec->shdr->sh_flags = 0;
  331. sec->shdr->sh_offset = 0;
  332. sec->shdr->sh_link = linker->strtab_sec_idx;
  333. /* sh_info should be one greater than the index of the last local
  334. * symbol (i.e., binding is STB_LOCAL). But why and who cares?
  335. */
  336. sec->shdr->sh_info = 0;
  337. sec->shdr->sh_addralign = 8;
  338. sec->shdr->sh_entsize = sizeof(Elf64_Sym);
  339. /* .BTF */
  340. linker->btf = btf__new_empty();
  341. err = libbpf_get_error(linker->btf);
  342. if (err)
  343. return err;
  344. /* add the special all-zero symbol */
  345. init_sym = add_new_sym(linker, NULL);
  346. if (!init_sym)
  347. return -EINVAL;
  348. init_sym->st_name = 0;
  349. init_sym->st_info = 0;
  350. init_sym->st_other = 0;
  351. init_sym->st_shndx = SHN_UNDEF;
  352. init_sym->st_value = 0;
  353. init_sym->st_size = 0;
  354. return 0;
  355. }
  356. int bpf_linker__add_file(struct bpf_linker *linker, const char *filename,
  357. const struct bpf_linker_file_opts *opts)
  358. {
  359. struct src_obj obj = {};
  360. int err = 0;
  361. if (!OPTS_VALID(opts, bpf_linker_file_opts))
  362. return libbpf_err(-EINVAL);
  363. if (!linker->elf)
  364. return libbpf_err(-EINVAL);
  365. err = err ?: linker_load_obj_file(linker, filename, opts, &obj);
  366. err = err ?: linker_append_sec_data(linker, &obj);
  367. err = err ?: linker_append_elf_syms(linker, &obj);
  368. err = err ?: linker_append_elf_relos(linker, &obj);
  369. err = err ?: linker_append_btf(linker, &obj);
  370. err = err ?: linker_append_btf_ext(linker, &obj);
  371. /* free up src_obj resources */
  372. free(obj.btf_type_map);
  373. btf__free(obj.btf);
  374. btf_ext__free(obj.btf_ext);
  375. free(obj.secs);
  376. free(obj.sym_map);
  377. if (obj.elf)
  378. elf_end(obj.elf);
  379. if (obj.fd >= 0)
  380. close(obj.fd);
  381. return libbpf_err(err);
  382. }
  383. static bool is_dwarf_sec_name(const char *name)
  384. {
  385. /* approximation, but the actual list is too long */
  386. return strncmp(name, ".debug_", sizeof(".debug_") - 1) == 0;
  387. }
  388. static bool is_ignored_sec(struct src_sec *sec)
  389. {
  390. Elf64_Shdr *shdr = sec->shdr;
  391. const char *name = sec->sec_name;
  392. /* no special handling of .strtab */
  393. if (shdr->sh_type == SHT_STRTAB)
  394. return true;
  395. /* ignore .llvm_addrsig section as well */
  396. if (shdr->sh_type == SHT_LLVM_ADDRSIG)
  397. return true;
  398. /* no subprograms will lead to an empty .text section, ignore it */
  399. if (shdr->sh_type == SHT_PROGBITS && shdr->sh_size == 0 &&
  400. strcmp(sec->sec_name, ".text") == 0)
  401. return true;
  402. /* DWARF sections */
  403. if (is_dwarf_sec_name(sec->sec_name))
  404. return true;
  405. if (strncmp(name, ".rel", sizeof(".rel") - 1) == 0) {
  406. name += sizeof(".rel") - 1;
  407. /* DWARF section relocations */
  408. if (is_dwarf_sec_name(name))
  409. return true;
  410. /* .BTF and .BTF.ext don't need relocations */
  411. if (strcmp(name, BTF_ELF_SEC) == 0 ||
  412. strcmp(name, BTF_EXT_ELF_SEC) == 0)
  413. return true;
  414. }
  415. return false;
  416. }
  417. static struct src_sec *add_src_sec(struct src_obj *obj, const char *sec_name)
  418. {
  419. struct src_sec *secs = obj->secs, *sec;
  420. size_t new_cnt = obj->sec_cnt ? obj->sec_cnt + 1 : 2;
  421. secs = libbpf_reallocarray(secs, new_cnt, sizeof(*secs));
  422. if (!secs)
  423. return NULL;
  424. /* zero out newly allocated memory */
  425. memset(secs + obj->sec_cnt, 0, (new_cnt - obj->sec_cnt) * sizeof(*secs));
  426. obj->secs = secs;
  427. obj->sec_cnt = new_cnt;
  428. sec = &obj->secs[new_cnt - 1];
  429. sec->id = new_cnt - 1;
  430. sec->sec_name = sec_name;
  431. return sec;
  432. }
  433. static int linker_load_obj_file(struct bpf_linker *linker, const char *filename,
  434. const struct bpf_linker_file_opts *opts,
  435. struct src_obj *obj)
  436. {
  437. #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
  438. const int host_endianness = ELFDATA2LSB;
  439. #elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
  440. const int host_endianness = ELFDATA2MSB;
  441. #else
  442. #error "Unknown __BYTE_ORDER__"
  443. #endif
  444. int err = 0;
  445. Elf_Scn *scn;
  446. Elf_Data *data;
  447. Elf64_Ehdr *ehdr;
  448. Elf64_Shdr *shdr;
  449. struct src_sec *sec;
  450. pr_debug("linker: adding object file '%s'...\n", filename);
  451. obj->filename = filename;
  452. obj->fd = open(filename, O_RDONLY | O_CLOEXEC);
  453. if (obj->fd < 0) {
  454. err = -errno;
  455. pr_warn("failed to open file '%s': %d\n", filename, err);
  456. return err;
  457. }
  458. obj->elf = elf_begin(obj->fd, ELF_C_READ_MMAP, NULL);
  459. if (!obj->elf) {
  460. err = -errno;
  461. pr_warn_elf("failed to parse ELF file '%s'", filename);
  462. return err;
  463. }
  464. /* Sanity check ELF file high-level properties */
  465. ehdr = elf64_getehdr(obj->elf);
  466. if (!ehdr) {
  467. err = -errno;
  468. pr_warn_elf("failed to get ELF header for %s", filename);
  469. return err;
  470. }
  471. if (ehdr->e_ident[EI_DATA] != host_endianness) {
  472. err = -EOPNOTSUPP;
  473. pr_warn_elf("unsupported byte order of ELF file %s", filename);
  474. return err;
  475. }
  476. if (ehdr->e_type != ET_REL
  477. || ehdr->e_machine != EM_BPF
  478. || ehdr->e_ident[EI_CLASS] != ELFCLASS64) {
  479. err = -EOPNOTSUPP;
  480. pr_warn_elf("unsupported kind of ELF file %s", filename);
  481. return err;
  482. }
  483. if (elf_getshdrstrndx(obj->elf, &obj->shstrs_sec_idx)) {
  484. err = -errno;
  485. pr_warn_elf("failed to get SHSTRTAB section index for %s", filename);
  486. return err;
  487. }
  488. scn = NULL;
  489. while ((scn = elf_nextscn(obj->elf, scn)) != NULL) {
  490. size_t sec_idx = elf_ndxscn(scn);
  491. const char *sec_name;
  492. shdr = elf64_getshdr(scn);
  493. if (!shdr) {
  494. err = -errno;
  495. pr_warn_elf("failed to get section #%zu header for %s",
  496. sec_idx, filename);
  497. return err;
  498. }
  499. sec_name = elf_strptr(obj->elf, obj->shstrs_sec_idx, shdr->sh_name);
  500. if (!sec_name) {
  501. err = -errno;
  502. pr_warn_elf("failed to get section #%zu name for %s",
  503. sec_idx, filename);
  504. return err;
  505. }
  506. data = elf_getdata(scn, 0);
  507. if (!data) {
  508. err = -errno;
  509. pr_warn_elf("failed to get section #%zu (%s) data from %s",
  510. sec_idx, sec_name, filename);
  511. return err;
  512. }
  513. sec = add_src_sec(obj, sec_name);
  514. if (!sec)
  515. return -ENOMEM;
  516. sec->scn = scn;
  517. sec->shdr = shdr;
  518. sec->data = data;
  519. sec->sec_idx = elf_ndxscn(scn);
  520. if (is_ignored_sec(sec)) {
  521. sec->skipped = true;
  522. continue;
  523. }
  524. switch (shdr->sh_type) {
  525. case SHT_SYMTAB:
  526. if (obj->symtab_sec_idx) {
  527. err = -EOPNOTSUPP;
  528. pr_warn("multiple SYMTAB sections found, not supported\n");
  529. return err;
  530. }
  531. obj->symtab_sec_idx = sec_idx;
  532. break;
  533. case SHT_STRTAB:
  534. /* we'll construct our own string table */
  535. break;
  536. case SHT_PROGBITS:
  537. if (strcmp(sec_name, BTF_ELF_SEC) == 0) {
  538. obj->btf = btf__new(data->d_buf, shdr->sh_size);
  539. err = libbpf_get_error(obj->btf);
  540. if (err) {
  541. pr_warn("failed to parse .BTF from %s: %d\n", filename, err);
  542. return err;
  543. }
  544. sec->skipped = true;
  545. continue;
  546. }
  547. if (strcmp(sec_name, BTF_EXT_ELF_SEC) == 0) {
  548. obj->btf_ext = btf_ext__new(data->d_buf, shdr->sh_size);
  549. err = libbpf_get_error(obj->btf_ext);
  550. if (err) {
  551. pr_warn("failed to parse .BTF.ext from '%s': %d\n", filename, err);
  552. return err;
  553. }
  554. sec->skipped = true;
  555. continue;
  556. }
  557. /* data & code */
  558. break;
  559. case SHT_NOBITS:
  560. /* BSS */
  561. break;
  562. case SHT_REL:
  563. /* relocations */
  564. break;
  565. default:
  566. pr_warn("unrecognized section #%zu (%s) in %s\n",
  567. sec_idx, sec_name, filename);
  568. err = -EINVAL;
  569. return err;
  570. }
  571. }
  572. err = err ?: linker_sanity_check_elf(obj);
  573. err = err ?: linker_sanity_check_btf(obj);
  574. err = err ?: linker_sanity_check_btf_ext(obj);
  575. err = err ?: linker_fixup_btf(obj);
  576. return err;
  577. }
  578. static int linker_sanity_check_elf(struct src_obj *obj)
  579. {
  580. struct src_sec *sec;
  581. int i, err;
  582. if (!obj->symtab_sec_idx) {
  583. pr_warn("ELF is missing SYMTAB section in %s\n", obj->filename);
  584. return -EINVAL;
  585. }
  586. if (!obj->shstrs_sec_idx) {
  587. pr_warn("ELF is missing section headers STRTAB section in %s\n", obj->filename);
  588. return -EINVAL;
  589. }
  590. for (i = 1; i < obj->sec_cnt; i++) {
  591. sec = &obj->secs[i];
  592. if (sec->sec_name[0] == '\0') {
  593. pr_warn("ELF section #%zu has empty name in %s\n", sec->sec_idx, obj->filename);
  594. return -EINVAL;
  595. }
  596. if (sec->shdr->sh_addralign && !is_pow_of_2(sec->shdr->sh_addralign))
  597. return -EINVAL;
  598. if (sec->shdr->sh_addralign != sec->data->d_align)
  599. return -EINVAL;
  600. if (sec->shdr->sh_size != sec->data->d_size)
  601. return -EINVAL;
  602. switch (sec->shdr->sh_type) {
  603. case SHT_SYMTAB:
  604. err = linker_sanity_check_elf_symtab(obj, sec);
  605. if (err)
  606. return err;
  607. break;
  608. case SHT_STRTAB:
  609. break;
  610. case SHT_PROGBITS:
  611. if (sec->shdr->sh_flags & SHF_EXECINSTR) {
  612. if (sec->shdr->sh_size % sizeof(struct bpf_insn) != 0)
  613. return -EINVAL;
  614. }
  615. break;
  616. case SHT_NOBITS:
  617. break;
  618. case SHT_REL:
  619. err = linker_sanity_check_elf_relos(obj, sec);
  620. if (err)
  621. return err;
  622. break;
  623. case SHT_LLVM_ADDRSIG:
  624. break;
  625. default:
  626. pr_warn("ELF section #%zu (%s) has unrecognized type %zu in %s\n",
  627. sec->sec_idx, sec->sec_name, (size_t)sec->shdr->sh_type, obj->filename);
  628. return -EINVAL;
  629. }
  630. }
  631. return 0;
  632. }
  633. static int linker_sanity_check_elf_symtab(struct src_obj *obj, struct src_sec *sec)
  634. {
  635. struct src_sec *link_sec;
  636. Elf64_Sym *sym;
  637. int i, n;
  638. if (sec->shdr->sh_entsize != sizeof(Elf64_Sym))
  639. return -EINVAL;
  640. if (sec->shdr->sh_size % sec->shdr->sh_entsize != 0)
  641. return -EINVAL;
  642. if (!sec->shdr->sh_link || sec->shdr->sh_link >= obj->sec_cnt) {
  643. pr_warn("ELF SYMTAB section #%zu points to missing STRTAB section #%zu in %s\n",
  644. sec->sec_idx, (size_t)sec->shdr->sh_link, obj->filename);
  645. return -EINVAL;
  646. }
  647. link_sec = &obj->secs[sec->shdr->sh_link];
  648. if (link_sec->shdr->sh_type != SHT_STRTAB) {
  649. pr_warn("ELF SYMTAB section #%zu points to invalid STRTAB section #%zu in %s\n",
  650. sec->sec_idx, (size_t)sec->shdr->sh_link, obj->filename);
  651. return -EINVAL;
  652. }
  653. n = sec->shdr->sh_size / sec->shdr->sh_entsize;
  654. sym = sec->data->d_buf;
  655. for (i = 0; i < n; i++, sym++) {
  656. int sym_type = ELF64_ST_TYPE(sym->st_info);
  657. int sym_bind = ELF64_ST_BIND(sym->st_info);
  658. int sym_vis = ELF64_ST_VISIBILITY(sym->st_other);
  659. if (i == 0) {
  660. if (sym->st_name != 0 || sym->st_info != 0
  661. || sym->st_other != 0 || sym->st_shndx != 0
  662. || sym->st_value != 0 || sym->st_size != 0) {
  663. pr_warn("ELF sym #0 is invalid in %s\n", obj->filename);
  664. return -EINVAL;
  665. }
  666. continue;
  667. }
  668. if (sym_bind != STB_LOCAL && sym_bind != STB_GLOBAL && sym_bind != STB_WEAK) {
  669. pr_warn("ELF sym #%d in section #%zu has unsupported symbol binding %d\n",
  670. i, sec->sec_idx, sym_bind);
  671. return -EINVAL;
  672. }
  673. if (sym_vis != STV_DEFAULT && sym_vis != STV_HIDDEN) {
  674. pr_warn("ELF sym #%d in section #%zu has unsupported symbol visibility %d\n",
  675. i, sec->sec_idx, sym_vis);
  676. return -EINVAL;
  677. }
  678. if (sym->st_shndx == 0) {
  679. if (sym_type != STT_NOTYPE || sym_bind == STB_LOCAL
  680. || sym->st_value != 0 || sym->st_size != 0) {
  681. pr_warn("ELF sym #%d is invalid extern symbol in %s\n",
  682. i, obj->filename);
  683. return -EINVAL;
  684. }
  685. continue;
  686. }
  687. if (sym->st_shndx < SHN_LORESERVE && sym->st_shndx >= obj->sec_cnt) {
  688. pr_warn("ELF sym #%d in section #%zu points to missing section #%zu in %s\n",
  689. i, sec->sec_idx, (size_t)sym->st_shndx, obj->filename);
  690. return -EINVAL;
  691. }
  692. if (sym_type == STT_SECTION) {
  693. if (sym->st_value != 0)
  694. return -EINVAL;
  695. continue;
  696. }
  697. }
  698. return 0;
  699. }
  700. static int linker_sanity_check_elf_relos(struct src_obj *obj, struct src_sec *sec)
  701. {
  702. struct src_sec *link_sec, *sym_sec;
  703. Elf64_Rel *relo;
  704. int i, n;
  705. if (sec->shdr->sh_entsize != sizeof(Elf64_Rel))
  706. return -EINVAL;
  707. if (sec->shdr->sh_size % sec->shdr->sh_entsize != 0)
  708. return -EINVAL;
  709. /* SHT_REL's sh_link should point to SYMTAB */
  710. if (sec->shdr->sh_link != obj->symtab_sec_idx) {
  711. pr_warn("ELF relo section #%zu points to invalid SYMTAB section #%zu in %s\n",
  712. sec->sec_idx, (size_t)sec->shdr->sh_link, obj->filename);
  713. return -EINVAL;
  714. }
  715. /* SHT_REL's sh_info points to relocated section */
  716. if (!sec->shdr->sh_info || sec->shdr->sh_info >= obj->sec_cnt) {
  717. pr_warn("ELF relo section #%zu points to missing section #%zu in %s\n",
  718. sec->sec_idx, (size_t)sec->shdr->sh_info, obj->filename);
  719. return -EINVAL;
  720. }
  721. link_sec = &obj->secs[sec->shdr->sh_info];
  722. /* .rel<secname> -> <secname> pattern is followed */
  723. if (strncmp(sec->sec_name, ".rel", sizeof(".rel") - 1) != 0
  724. || strcmp(sec->sec_name + sizeof(".rel") - 1, link_sec->sec_name) != 0) {
  725. pr_warn("ELF relo section #%zu name has invalid name in %s\n",
  726. sec->sec_idx, obj->filename);
  727. return -EINVAL;
  728. }
  729. /* don't further validate relocations for ignored sections */
  730. if (link_sec->skipped)
  731. return 0;
  732. /* relocatable section is data or instructions */
  733. if (link_sec->shdr->sh_type != SHT_PROGBITS && link_sec->shdr->sh_type != SHT_NOBITS) {
  734. pr_warn("ELF relo section #%zu points to invalid section #%zu in %s\n",
  735. sec->sec_idx, (size_t)sec->shdr->sh_info, obj->filename);
  736. return -EINVAL;
  737. }
  738. /* check sanity of each relocation */
  739. n = sec->shdr->sh_size / sec->shdr->sh_entsize;
  740. relo = sec->data->d_buf;
  741. sym_sec = &obj->secs[obj->symtab_sec_idx];
  742. for (i = 0; i < n; i++, relo++) {
  743. size_t sym_idx = ELF64_R_SYM(relo->r_info);
  744. size_t sym_type = ELF64_R_TYPE(relo->r_info);
  745. if (sym_type != R_BPF_64_64 && sym_type != R_BPF_64_32 &&
  746. sym_type != R_BPF_64_ABS64 && sym_type != R_BPF_64_ABS32) {
  747. pr_warn("ELF relo #%d in section #%zu has unexpected type %zu in %s\n",
  748. i, sec->sec_idx, sym_type, obj->filename);
  749. return -EINVAL;
  750. }
  751. if (!sym_idx || sym_idx * sizeof(Elf64_Sym) >= sym_sec->shdr->sh_size) {
  752. pr_warn("ELF relo #%d in section #%zu points to invalid symbol #%zu in %s\n",
  753. i, sec->sec_idx, sym_idx, obj->filename);
  754. return -EINVAL;
  755. }
  756. if (link_sec->shdr->sh_flags & SHF_EXECINSTR) {
  757. if (relo->r_offset % sizeof(struct bpf_insn) != 0) {
  758. pr_warn("ELF relo #%d in section #%zu points to missing symbol #%zu in %s\n",
  759. i, sec->sec_idx, sym_idx, obj->filename);
  760. return -EINVAL;
  761. }
  762. }
  763. }
  764. return 0;
  765. }
  766. static int check_btf_type_id(__u32 *type_id, void *ctx)
  767. {
  768. struct btf *btf = ctx;
  769. if (*type_id >= btf__type_cnt(btf))
  770. return -EINVAL;
  771. return 0;
  772. }
  773. static int check_btf_str_off(__u32 *str_off, void *ctx)
  774. {
  775. struct btf *btf = ctx;
  776. const char *s;
  777. s = btf__str_by_offset(btf, *str_off);
  778. if (!s)
  779. return -EINVAL;
  780. return 0;
  781. }
  782. static int linker_sanity_check_btf(struct src_obj *obj)
  783. {
  784. struct btf_type *t;
  785. int i, n, err = 0;
  786. if (!obj->btf)
  787. return 0;
  788. n = btf__type_cnt(obj->btf);
  789. for (i = 1; i < n; i++) {
  790. t = btf_type_by_id(obj->btf, i);
  791. err = err ?: btf_type_visit_type_ids(t, check_btf_type_id, obj->btf);
  792. err = err ?: btf_type_visit_str_offs(t, check_btf_str_off, obj->btf);
  793. if (err)
  794. return err;
  795. }
  796. return 0;
  797. }
  798. static int linker_sanity_check_btf_ext(struct src_obj *obj)
  799. {
  800. int err = 0;
  801. if (!obj->btf_ext)
  802. return 0;
  803. /* can't use .BTF.ext without .BTF */
  804. if (!obj->btf)
  805. return -EINVAL;
  806. err = err ?: btf_ext_visit_type_ids(obj->btf_ext, check_btf_type_id, obj->btf);
  807. err = err ?: btf_ext_visit_str_offs(obj->btf_ext, check_btf_str_off, obj->btf);
  808. if (err)
  809. return err;
  810. return 0;
  811. }
  812. static int init_sec(struct bpf_linker *linker, struct dst_sec *dst_sec, struct src_sec *src_sec)
  813. {
  814. Elf_Scn *scn;
  815. Elf_Data *data;
  816. Elf64_Shdr *shdr;
  817. int name_off;
  818. dst_sec->sec_sz = 0;
  819. dst_sec->sec_idx = 0;
  820. dst_sec->ephemeral = src_sec->ephemeral;
  821. /* ephemeral sections are just thin section shells lacking most parts */
  822. if (src_sec->ephemeral)
  823. return 0;
  824. scn = elf_newscn(linker->elf);
  825. if (!scn)
  826. return -ENOMEM;
  827. data = elf_newdata(scn);
  828. if (!data)
  829. return -ENOMEM;
  830. shdr = elf64_getshdr(scn);
  831. if (!shdr)
  832. return -ENOMEM;
  833. dst_sec->scn = scn;
  834. dst_sec->shdr = shdr;
  835. dst_sec->data = data;
  836. dst_sec->sec_idx = elf_ndxscn(scn);
  837. name_off = strset__add_str(linker->strtab_strs, src_sec->sec_name);
  838. if (name_off < 0)
  839. return name_off;
  840. shdr->sh_name = name_off;
  841. shdr->sh_type = src_sec->shdr->sh_type;
  842. shdr->sh_flags = src_sec->shdr->sh_flags;
  843. shdr->sh_size = 0;
  844. /* sh_link and sh_info have different meaning for different types of
  845. * sections, so we leave it up to the caller code to fill them in, if
  846. * necessary
  847. */
  848. shdr->sh_link = 0;
  849. shdr->sh_info = 0;
  850. shdr->sh_addralign = src_sec->shdr->sh_addralign;
  851. shdr->sh_entsize = src_sec->shdr->sh_entsize;
  852. data->d_type = src_sec->data->d_type;
  853. data->d_size = 0;
  854. data->d_buf = NULL;
  855. data->d_align = src_sec->data->d_align;
  856. data->d_off = 0;
  857. return 0;
  858. }
  859. static struct dst_sec *find_dst_sec_by_name(struct bpf_linker *linker, const char *sec_name)
  860. {
  861. struct dst_sec *sec;
  862. int i;
  863. for (i = 1; i < linker->sec_cnt; i++) {
  864. sec = &linker->secs[i];
  865. if (strcmp(sec->sec_name, sec_name) == 0)
  866. return sec;
  867. }
  868. return NULL;
  869. }
  870. static bool secs_match(struct dst_sec *dst, struct src_sec *src)
  871. {
  872. if (dst->ephemeral || src->ephemeral)
  873. return true;
  874. if (dst->shdr->sh_type != src->shdr->sh_type) {
  875. pr_warn("sec %s types mismatch\n", dst->sec_name);
  876. return false;
  877. }
  878. if (dst->shdr->sh_flags != src->shdr->sh_flags) {
  879. pr_warn("sec %s flags mismatch\n", dst->sec_name);
  880. return false;
  881. }
  882. if (dst->shdr->sh_entsize != src->shdr->sh_entsize) {
  883. pr_warn("sec %s entsize mismatch\n", dst->sec_name);
  884. return false;
  885. }
  886. return true;
  887. }
  888. static bool sec_content_is_same(struct dst_sec *dst_sec, struct src_sec *src_sec)
  889. {
  890. if (dst_sec->sec_sz != src_sec->shdr->sh_size)
  891. return false;
  892. if (memcmp(dst_sec->raw_data, src_sec->data->d_buf, dst_sec->sec_sz) != 0)
  893. return false;
  894. return true;
  895. }
  896. static int extend_sec(struct bpf_linker *linker, struct dst_sec *dst, struct src_sec *src)
  897. {
  898. void *tmp;
  899. size_t dst_align, src_align;
  900. size_t dst_align_sz, dst_final_sz;
  901. int err;
  902. /* Ephemeral source section doesn't contribute anything to ELF
  903. * section data.
  904. */
  905. if (src->ephemeral)
  906. return 0;
  907. /* Some sections (like .maps) can contain both externs (and thus be
  908. * ephemeral) and non-externs (map definitions). So it's possible that
  909. * it has to be "upgraded" from ephemeral to non-ephemeral when the
  910. * first non-ephemeral entity appears. In such case, we add ELF
  911. * section, data, etc.
  912. */
  913. if (dst->ephemeral) {
  914. err = init_sec(linker, dst, src);
  915. if (err)
  916. return err;
  917. }
  918. dst_align = dst->shdr->sh_addralign;
  919. src_align = src->shdr->sh_addralign;
  920. if (dst_align == 0)
  921. dst_align = 1;
  922. if (dst_align < src_align)
  923. dst_align = src_align;
  924. dst_align_sz = (dst->sec_sz + dst_align - 1) / dst_align * dst_align;
  925. /* no need to re-align final size */
  926. dst_final_sz = dst_align_sz + src->shdr->sh_size;
  927. if (src->shdr->sh_type != SHT_NOBITS) {
  928. tmp = realloc(dst->raw_data, dst_final_sz);
  929. if (!tmp)
  930. return -ENOMEM;
  931. dst->raw_data = tmp;
  932. /* pad dst section, if it's alignment forced size increase */
  933. memset(dst->raw_data + dst->sec_sz, 0, dst_align_sz - dst->sec_sz);
  934. /* now copy src data at a properly aligned offset */
  935. memcpy(dst->raw_data + dst_align_sz, src->data->d_buf, src->shdr->sh_size);
  936. }
  937. dst->sec_sz = dst_final_sz;
  938. dst->shdr->sh_size = dst_final_sz;
  939. dst->data->d_size = dst_final_sz;
  940. dst->shdr->sh_addralign = dst_align;
  941. dst->data->d_align = dst_align;
  942. src->dst_off = dst_align_sz;
  943. return 0;
  944. }
  945. static bool is_data_sec(struct src_sec *sec)
  946. {
  947. if (!sec || sec->skipped)
  948. return false;
  949. /* ephemeral sections are data sections, e.g., .kconfig, .ksyms */
  950. if (sec->ephemeral)
  951. return true;
  952. return sec->shdr->sh_type == SHT_PROGBITS || sec->shdr->sh_type == SHT_NOBITS;
  953. }
  954. static bool is_relo_sec(struct src_sec *sec)
  955. {
  956. if (!sec || sec->skipped || sec->ephemeral)
  957. return false;
  958. return sec->shdr->sh_type == SHT_REL;
  959. }
  960. static int linker_append_sec_data(struct bpf_linker *linker, struct src_obj *obj)
  961. {
  962. int i, err;
  963. for (i = 1; i < obj->sec_cnt; i++) {
  964. struct src_sec *src_sec;
  965. struct dst_sec *dst_sec;
  966. src_sec = &obj->secs[i];
  967. if (!is_data_sec(src_sec))
  968. continue;
  969. dst_sec = find_dst_sec_by_name(linker, src_sec->sec_name);
  970. if (!dst_sec) {
  971. dst_sec = add_dst_sec(linker, src_sec->sec_name);
  972. if (!dst_sec)
  973. return -ENOMEM;
  974. err = init_sec(linker, dst_sec, src_sec);
  975. if (err) {
  976. pr_warn("failed to init section '%s'\n", src_sec->sec_name);
  977. return err;
  978. }
  979. } else {
  980. if (!secs_match(dst_sec, src_sec)) {
  981. pr_warn("ELF sections %s are incompatible\n", src_sec->sec_name);
  982. return -1;
  983. }
  984. /* "license" and "version" sections are deduped */
  985. if (strcmp(src_sec->sec_name, "license") == 0
  986. || strcmp(src_sec->sec_name, "version") == 0) {
  987. if (!sec_content_is_same(dst_sec, src_sec)) {
  988. pr_warn("non-identical contents of section '%s' are not supported\n", src_sec->sec_name);
  989. return -EINVAL;
  990. }
  991. src_sec->skipped = true;
  992. src_sec->dst_id = dst_sec->id;
  993. continue;
  994. }
  995. }
  996. /* record mapped section index */
  997. src_sec->dst_id = dst_sec->id;
  998. err = extend_sec(linker, dst_sec, src_sec);
  999. if (err)
  1000. return err;
  1001. }
  1002. return 0;
  1003. }
  1004. static int linker_append_elf_syms(struct bpf_linker *linker, struct src_obj *obj)
  1005. {
  1006. struct src_sec *symtab = &obj->secs[obj->symtab_sec_idx];
  1007. Elf64_Sym *sym = symtab->data->d_buf;
  1008. int i, n = symtab->shdr->sh_size / symtab->shdr->sh_entsize, err;
  1009. int str_sec_idx = symtab->shdr->sh_link;
  1010. const char *sym_name;
  1011. obj->sym_map = calloc(n + 1, sizeof(*obj->sym_map));
  1012. if (!obj->sym_map)
  1013. return -ENOMEM;
  1014. for (i = 0; i < n; i++, sym++) {
  1015. /* We already validated all-zero symbol #0 and we already
  1016. * appended it preventively to the final SYMTAB, so skip it.
  1017. */
  1018. if (i == 0)
  1019. continue;
  1020. sym_name = elf_strptr(obj->elf, str_sec_idx, sym->st_name);
  1021. if (!sym_name) {
  1022. pr_warn("can't fetch symbol name for symbol #%d in '%s'\n", i, obj->filename);
  1023. return -EINVAL;
  1024. }
  1025. err = linker_append_elf_sym(linker, obj, sym, sym_name, i);
  1026. if (err)
  1027. return err;
  1028. }
  1029. return 0;
  1030. }
  1031. static Elf64_Sym *get_sym_by_idx(struct bpf_linker *linker, size_t sym_idx)
  1032. {
  1033. struct dst_sec *symtab = &linker->secs[linker->symtab_sec_idx];
  1034. Elf64_Sym *syms = symtab->raw_data;
  1035. return &syms[sym_idx];
  1036. }
  1037. static struct glob_sym *find_glob_sym(struct bpf_linker *linker, const char *sym_name)
  1038. {
  1039. struct glob_sym *glob_sym;
  1040. const char *name;
  1041. int i;
  1042. for (i = 0; i < linker->glob_sym_cnt; i++) {
  1043. glob_sym = &linker->glob_syms[i];
  1044. name = strset__data(linker->strtab_strs) + glob_sym->name_off;
  1045. if (strcmp(name, sym_name) == 0)
  1046. return glob_sym;
  1047. }
  1048. return NULL;
  1049. }
  1050. static struct glob_sym *add_glob_sym(struct bpf_linker *linker)
  1051. {
  1052. struct glob_sym *syms, *sym;
  1053. syms = libbpf_reallocarray(linker->glob_syms, linker->glob_sym_cnt + 1,
  1054. sizeof(*linker->glob_syms));
  1055. if (!syms)
  1056. return NULL;
  1057. sym = &syms[linker->glob_sym_cnt];
  1058. memset(sym, 0, sizeof(*sym));
  1059. sym->var_idx = -1;
  1060. linker->glob_syms = syms;
  1061. linker->glob_sym_cnt++;
  1062. return sym;
  1063. }
  1064. static bool glob_sym_btf_matches(const char *sym_name, bool exact,
  1065. const struct btf *btf1, __u32 id1,
  1066. const struct btf *btf2, __u32 id2)
  1067. {
  1068. const struct btf_type *t1, *t2;
  1069. bool is_static1, is_static2;
  1070. const char *n1, *n2;
  1071. int i, n;
  1072. recur:
  1073. n1 = n2 = NULL;
  1074. t1 = skip_mods_and_typedefs(btf1, id1, &id1);
  1075. t2 = skip_mods_and_typedefs(btf2, id2, &id2);
  1076. /* check if only one side is FWD, otherwise handle with common logic */
  1077. if (!exact && btf_is_fwd(t1) != btf_is_fwd(t2)) {
  1078. n1 = btf__str_by_offset(btf1, t1->name_off);
  1079. n2 = btf__str_by_offset(btf2, t2->name_off);
  1080. if (strcmp(n1, n2) != 0) {
  1081. pr_warn("global '%s': incompatible forward declaration names '%s' and '%s'\n",
  1082. sym_name, n1, n2);
  1083. return false;
  1084. }
  1085. /* validate if FWD kind matches concrete kind */
  1086. if (btf_is_fwd(t1)) {
  1087. if (btf_kflag(t1) && btf_is_union(t2))
  1088. return true;
  1089. if (!btf_kflag(t1) && btf_is_struct(t2))
  1090. return true;
  1091. pr_warn("global '%s': incompatible %s forward declaration and concrete kind %s\n",
  1092. sym_name, btf_kflag(t1) ? "union" : "struct", btf_kind_str(t2));
  1093. } else {
  1094. if (btf_kflag(t2) && btf_is_union(t1))
  1095. return true;
  1096. if (!btf_kflag(t2) && btf_is_struct(t1))
  1097. return true;
  1098. pr_warn("global '%s': incompatible %s forward declaration and concrete kind %s\n",
  1099. sym_name, btf_kflag(t2) ? "union" : "struct", btf_kind_str(t1));
  1100. }
  1101. return false;
  1102. }
  1103. if (btf_kind(t1) != btf_kind(t2)) {
  1104. pr_warn("global '%s': incompatible BTF kinds %s and %s\n",
  1105. sym_name, btf_kind_str(t1), btf_kind_str(t2));
  1106. return false;
  1107. }
  1108. switch (btf_kind(t1)) {
  1109. case BTF_KIND_STRUCT:
  1110. case BTF_KIND_UNION:
  1111. case BTF_KIND_ENUM:
  1112. case BTF_KIND_ENUM64:
  1113. case BTF_KIND_FWD:
  1114. case BTF_KIND_FUNC:
  1115. case BTF_KIND_VAR:
  1116. n1 = btf__str_by_offset(btf1, t1->name_off);
  1117. n2 = btf__str_by_offset(btf2, t2->name_off);
  1118. if (strcmp(n1, n2) != 0) {
  1119. pr_warn("global '%s': incompatible %s names '%s' and '%s'\n",
  1120. sym_name, btf_kind_str(t1), n1, n2);
  1121. return false;
  1122. }
  1123. break;
  1124. default:
  1125. break;
  1126. }
  1127. switch (btf_kind(t1)) {
  1128. case BTF_KIND_UNKN: /* void */
  1129. case BTF_KIND_FWD:
  1130. return true;
  1131. case BTF_KIND_INT:
  1132. case BTF_KIND_FLOAT:
  1133. case BTF_KIND_ENUM:
  1134. case BTF_KIND_ENUM64:
  1135. /* ignore encoding for int and enum values for enum */
  1136. if (t1->size != t2->size) {
  1137. pr_warn("global '%s': incompatible %s '%s' size %u and %u\n",
  1138. sym_name, btf_kind_str(t1), n1, t1->size, t2->size);
  1139. return false;
  1140. }
  1141. return true;
  1142. case BTF_KIND_PTR:
  1143. /* just validate overall shape of the referenced type, so no
  1144. * contents comparison for struct/union, and allowd fwd vs
  1145. * struct/union
  1146. */
  1147. exact = false;
  1148. id1 = t1->type;
  1149. id2 = t2->type;
  1150. goto recur;
  1151. case BTF_KIND_ARRAY:
  1152. /* ignore index type and array size */
  1153. id1 = btf_array(t1)->type;
  1154. id2 = btf_array(t2)->type;
  1155. goto recur;
  1156. case BTF_KIND_FUNC:
  1157. /* extern and global linkages are compatible */
  1158. is_static1 = btf_func_linkage(t1) == BTF_FUNC_STATIC;
  1159. is_static2 = btf_func_linkage(t2) == BTF_FUNC_STATIC;
  1160. if (is_static1 != is_static2) {
  1161. pr_warn("global '%s': incompatible func '%s' linkage\n", sym_name, n1);
  1162. return false;
  1163. }
  1164. id1 = t1->type;
  1165. id2 = t2->type;
  1166. goto recur;
  1167. case BTF_KIND_VAR:
  1168. /* extern and global linkages are compatible */
  1169. is_static1 = btf_var(t1)->linkage == BTF_VAR_STATIC;
  1170. is_static2 = btf_var(t2)->linkage == BTF_VAR_STATIC;
  1171. if (is_static1 != is_static2) {
  1172. pr_warn("global '%s': incompatible var '%s' linkage\n", sym_name, n1);
  1173. return false;
  1174. }
  1175. id1 = t1->type;
  1176. id2 = t2->type;
  1177. goto recur;
  1178. case BTF_KIND_STRUCT:
  1179. case BTF_KIND_UNION: {
  1180. const struct btf_member *m1, *m2;
  1181. if (!exact)
  1182. return true;
  1183. if (btf_vlen(t1) != btf_vlen(t2)) {
  1184. pr_warn("global '%s': incompatible number of %s fields %u and %u\n",
  1185. sym_name, btf_kind_str(t1), btf_vlen(t1), btf_vlen(t2));
  1186. return false;
  1187. }
  1188. n = btf_vlen(t1);
  1189. m1 = btf_members(t1);
  1190. m2 = btf_members(t2);
  1191. for (i = 0; i < n; i++, m1++, m2++) {
  1192. n1 = btf__str_by_offset(btf1, m1->name_off);
  1193. n2 = btf__str_by_offset(btf2, m2->name_off);
  1194. if (strcmp(n1, n2) != 0) {
  1195. pr_warn("global '%s': incompatible field #%d names '%s' and '%s'\n",
  1196. sym_name, i, n1, n2);
  1197. return false;
  1198. }
  1199. if (m1->offset != m2->offset) {
  1200. pr_warn("global '%s': incompatible field #%d ('%s') offsets\n",
  1201. sym_name, i, n1);
  1202. return false;
  1203. }
  1204. if (!glob_sym_btf_matches(sym_name, exact, btf1, m1->type, btf2, m2->type))
  1205. return false;
  1206. }
  1207. return true;
  1208. }
  1209. case BTF_KIND_FUNC_PROTO: {
  1210. const struct btf_param *m1, *m2;
  1211. if (btf_vlen(t1) != btf_vlen(t2)) {
  1212. pr_warn("global '%s': incompatible number of %s params %u and %u\n",
  1213. sym_name, btf_kind_str(t1), btf_vlen(t1), btf_vlen(t2));
  1214. return false;
  1215. }
  1216. n = btf_vlen(t1);
  1217. m1 = btf_params(t1);
  1218. m2 = btf_params(t2);
  1219. for (i = 0; i < n; i++, m1++, m2++) {
  1220. /* ignore func arg names */
  1221. if (!glob_sym_btf_matches(sym_name, exact, btf1, m1->type, btf2, m2->type))
  1222. return false;
  1223. }
  1224. /* now check return type as well */
  1225. id1 = t1->type;
  1226. id2 = t2->type;
  1227. goto recur;
  1228. }
  1229. /* skip_mods_and_typedefs() make this impossible */
  1230. case BTF_KIND_TYPEDEF:
  1231. case BTF_KIND_VOLATILE:
  1232. case BTF_KIND_CONST:
  1233. case BTF_KIND_RESTRICT:
  1234. /* DATASECs are never compared with each other */
  1235. case BTF_KIND_DATASEC:
  1236. default:
  1237. pr_warn("global '%s': unsupported BTF kind %s\n",
  1238. sym_name, btf_kind_str(t1));
  1239. return false;
  1240. }
  1241. }
  1242. static bool map_defs_match(const char *sym_name,
  1243. const struct btf *main_btf,
  1244. const struct btf_map_def *main_def,
  1245. const struct btf_map_def *main_inner_def,
  1246. const struct btf *extra_btf,
  1247. const struct btf_map_def *extra_def,
  1248. const struct btf_map_def *extra_inner_def)
  1249. {
  1250. const char *reason;
  1251. if (main_def->map_type != extra_def->map_type) {
  1252. reason = "type";
  1253. goto mismatch;
  1254. }
  1255. /* check key type/size match */
  1256. if (main_def->key_size != extra_def->key_size) {
  1257. reason = "key_size";
  1258. goto mismatch;
  1259. }
  1260. if (!!main_def->key_type_id != !!extra_def->key_type_id) {
  1261. reason = "key type";
  1262. goto mismatch;
  1263. }
  1264. if ((main_def->parts & MAP_DEF_KEY_TYPE)
  1265. && !glob_sym_btf_matches(sym_name, true /*exact*/,
  1266. main_btf, main_def->key_type_id,
  1267. extra_btf, extra_def->key_type_id)) {
  1268. reason = "key type";
  1269. goto mismatch;
  1270. }
  1271. /* validate value type/size match */
  1272. if (main_def->value_size != extra_def->value_size) {
  1273. reason = "value_size";
  1274. goto mismatch;
  1275. }
  1276. if (!!main_def->value_type_id != !!extra_def->value_type_id) {
  1277. reason = "value type";
  1278. goto mismatch;
  1279. }
  1280. if ((main_def->parts & MAP_DEF_VALUE_TYPE)
  1281. && !glob_sym_btf_matches(sym_name, true /*exact*/,
  1282. main_btf, main_def->value_type_id,
  1283. extra_btf, extra_def->value_type_id)) {
  1284. reason = "key type";
  1285. goto mismatch;
  1286. }
  1287. if (main_def->max_entries != extra_def->max_entries) {
  1288. reason = "max_entries";
  1289. goto mismatch;
  1290. }
  1291. if (main_def->map_flags != extra_def->map_flags) {
  1292. reason = "map_flags";
  1293. goto mismatch;
  1294. }
  1295. if (main_def->numa_node != extra_def->numa_node) {
  1296. reason = "numa_node";
  1297. goto mismatch;
  1298. }
  1299. if (main_def->pinning != extra_def->pinning) {
  1300. reason = "pinning";
  1301. goto mismatch;
  1302. }
  1303. if ((main_def->parts & MAP_DEF_INNER_MAP) != (extra_def->parts & MAP_DEF_INNER_MAP)) {
  1304. reason = "inner map";
  1305. goto mismatch;
  1306. }
  1307. if (main_def->parts & MAP_DEF_INNER_MAP) {
  1308. char inner_map_name[128];
  1309. snprintf(inner_map_name, sizeof(inner_map_name), "%s.inner", sym_name);
  1310. return map_defs_match(inner_map_name,
  1311. main_btf, main_inner_def, NULL,
  1312. extra_btf, extra_inner_def, NULL);
  1313. }
  1314. return true;
  1315. mismatch:
  1316. pr_warn("global '%s': map %s mismatch\n", sym_name, reason);
  1317. return false;
  1318. }
  1319. static bool glob_map_defs_match(const char *sym_name,
  1320. struct bpf_linker *linker, struct glob_sym *glob_sym,
  1321. struct src_obj *obj, Elf64_Sym *sym, int btf_id)
  1322. {
  1323. struct btf_map_def dst_def = {}, dst_inner_def = {};
  1324. struct btf_map_def src_def = {}, src_inner_def = {};
  1325. const struct btf_type *t;
  1326. int err;
  1327. t = btf__type_by_id(obj->btf, btf_id);
  1328. if (!btf_is_var(t)) {
  1329. pr_warn("global '%s': invalid map definition type [%d]\n", sym_name, btf_id);
  1330. return false;
  1331. }
  1332. t = skip_mods_and_typedefs(obj->btf, t->type, NULL);
  1333. err = parse_btf_map_def(sym_name, obj->btf, t, true /*strict*/, &src_def, &src_inner_def);
  1334. if (err) {
  1335. pr_warn("global '%s': invalid map definition\n", sym_name);
  1336. return false;
  1337. }
  1338. /* re-parse existing map definition */
  1339. t = btf__type_by_id(linker->btf, glob_sym->btf_id);
  1340. t = skip_mods_and_typedefs(linker->btf, t->type, NULL);
  1341. err = parse_btf_map_def(sym_name, linker->btf, t, true /*strict*/, &dst_def, &dst_inner_def);
  1342. if (err) {
  1343. /* this should not happen, because we already validated it */
  1344. pr_warn("global '%s': invalid dst map definition\n", sym_name);
  1345. return false;
  1346. }
  1347. /* Currently extern map definition has to be complete and match
  1348. * concrete map definition exactly. This restriction might be lifted
  1349. * in the future.
  1350. */
  1351. return map_defs_match(sym_name, linker->btf, &dst_def, &dst_inner_def,
  1352. obj->btf, &src_def, &src_inner_def);
  1353. }
  1354. static bool glob_syms_match(const char *sym_name,
  1355. struct bpf_linker *linker, struct glob_sym *glob_sym,
  1356. struct src_obj *obj, Elf64_Sym *sym, size_t sym_idx, int btf_id)
  1357. {
  1358. const struct btf_type *src_t;
  1359. /* if we are dealing with externs, BTF types describing both global
  1360. * and extern VARs/FUNCs should be completely present in all files
  1361. */
  1362. if (!glob_sym->btf_id || !btf_id) {
  1363. pr_warn("BTF info is missing for global symbol '%s'\n", sym_name);
  1364. return false;
  1365. }
  1366. src_t = btf__type_by_id(obj->btf, btf_id);
  1367. if (!btf_is_var(src_t) && !btf_is_func(src_t)) {
  1368. pr_warn("only extern variables and functions are supported, but got '%s' for '%s'\n",
  1369. btf_kind_str(src_t), sym_name);
  1370. return false;
  1371. }
  1372. /* deal with .maps definitions specially */
  1373. if (glob_sym->sec_id && strcmp(linker->secs[glob_sym->sec_id].sec_name, MAPS_ELF_SEC) == 0)
  1374. return glob_map_defs_match(sym_name, linker, glob_sym, obj, sym, btf_id);
  1375. if (!glob_sym_btf_matches(sym_name, true /*exact*/,
  1376. linker->btf, glob_sym->btf_id, obj->btf, btf_id))
  1377. return false;
  1378. return true;
  1379. }
  1380. static bool btf_is_non_static(const struct btf_type *t)
  1381. {
  1382. return (btf_is_var(t) && btf_var(t)->linkage != BTF_VAR_STATIC)
  1383. || (btf_is_func(t) && btf_func_linkage(t) != BTF_FUNC_STATIC);
  1384. }
  1385. static int find_glob_sym_btf(struct src_obj *obj, Elf64_Sym *sym, const char *sym_name,
  1386. int *out_btf_sec_id, int *out_btf_id)
  1387. {
  1388. int i, j, n, m, btf_id = 0;
  1389. const struct btf_type *t;
  1390. const struct btf_var_secinfo *vi;
  1391. const char *name;
  1392. if (!obj->btf) {
  1393. pr_warn("failed to find BTF info for object '%s'\n", obj->filename);
  1394. return -EINVAL;
  1395. }
  1396. n = btf__type_cnt(obj->btf);
  1397. for (i = 1; i < n; i++) {
  1398. t = btf__type_by_id(obj->btf, i);
  1399. /* some global and extern FUNCs and VARs might not be associated with any
  1400. * DATASEC, so try to detect them in the same pass
  1401. */
  1402. if (btf_is_non_static(t)) {
  1403. name = btf__str_by_offset(obj->btf, t->name_off);
  1404. if (strcmp(name, sym_name) != 0)
  1405. continue;
  1406. /* remember and still try to find DATASEC */
  1407. btf_id = i;
  1408. continue;
  1409. }
  1410. if (!btf_is_datasec(t))
  1411. continue;
  1412. vi = btf_var_secinfos(t);
  1413. for (j = 0, m = btf_vlen(t); j < m; j++, vi++) {
  1414. t = btf__type_by_id(obj->btf, vi->type);
  1415. name = btf__str_by_offset(obj->btf, t->name_off);
  1416. if (strcmp(name, sym_name) != 0)
  1417. continue;
  1418. if (btf_is_var(t) && btf_var(t)->linkage == BTF_VAR_STATIC)
  1419. continue;
  1420. if (btf_is_func(t) && btf_func_linkage(t) == BTF_FUNC_STATIC)
  1421. continue;
  1422. if (btf_id && btf_id != vi->type) {
  1423. pr_warn("global/extern '%s' BTF is ambiguous: both types #%d and #%u match\n",
  1424. sym_name, btf_id, vi->type);
  1425. return -EINVAL;
  1426. }
  1427. *out_btf_sec_id = i;
  1428. *out_btf_id = vi->type;
  1429. return 0;
  1430. }
  1431. }
  1432. /* free-floating extern or global FUNC */
  1433. if (btf_id) {
  1434. *out_btf_sec_id = 0;
  1435. *out_btf_id = btf_id;
  1436. return 0;
  1437. }
  1438. pr_warn("failed to find BTF info for global/extern symbol '%s'\n", sym_name);
  1439. return -ENOENT;
  1440. }
  1441. static struct src_sec *find_src_sec_by_name(struct src_obj *obj, const char *sec_name)
  1442. {
  1443. struct src_sec *sec;
  1444. int i;
  1445. for (i = 1; i < obj->sec_cnt; i++) {
  1446. sec = &obj->secs[i];
  1447. if (strcmp(sec->sec_name, sec_name) == 0)
  1448. return sec;
  1449. }
  1450. return NULL;
  1451. }
  1452. static int complete_extern_btf_info(struct btf *dst_btf, int dst_id,
  1453. struct btf *src_btf, int src_id)
  1454. {
  1455. struct btf_type *dst_t = btf_type_by_id(dst_btf, dst_id);
  1456. struct btf_type *src_t = btf_type_by_id(src_btf, src_id);
  1457. struct btf_param *src_p, *dst_p;
  1458. const char *s;
  1459. int i, n, off;
  1460. /* We already made sure that source and destination types (FUNC or
  1461. * VAR) match in terms of types and argument names.
  1462. */
  1463. if (btf_is_var(dst_t)) {
  1464. btf_var(dst_t)->linkage = BTF_VAR_GLOBAL_ALLOCATED;
  1465. return 0;
  1466. }
  1467. dst_t->info = btf_type_info(BTF_KIND_FUNC, BTF_FUNC_GLOBAL, 0);
  1468. /* now onto FUNC_PROTO types */
  1469. src_t = btf_type_by_id(src_btf, src_t->type);
  1470. dst_t = btf_type_by_id(dst_btf, dst_t->type);
  1471. /* Fill in all the argument names, which for extern FUNCs are missing.
  1472. * We'll end up with two copies of FUNCs/VARs for externs, but that
  1473. * will be taken care of by BTF dedup at the very end.
  1474. * It might be that BTF types for extern in one file has less/more BTF
  1475. * information (e.g., FWD instead of full STRUCT/UNION information),
  1476. * but that should be (in most cases, subject to BTF dedup rules)
  1477. * handled and resolved by BTF dedup algorithm as well, so we won't
  1478. * worry about it. Our only job is to make sure that argument names
  1479. * are populated on both sides, otherwise BTF dedup will pedantically
  1480. * consider them different.
  1481. */
  1482. src_p = btf_params(src_t);
  1483. dst_p = btf_params(dst_t);
  1484. for (i = 0, n = btf_vlen(dst_t); i < n; i++, src_p++, dst_p++) {
  1485. if (!src_p->name_off)
  1486. continue;
  1487. /* src_btf has more complete info, so add name to dst_btf */
  1488. s = btf__str_by_offset(src_btf, src_p->name_off);
  1489. off = btf__add_str(dst_btf, s);
  1490. if (off < 0)
  1491. return off;
  1492. dst_p->name_off = off;
  1493. }
  1494. return 0;
  1495. }
  1496. static void sym_update_bind(Elf64_Sym *sym, int sym_bind)
  1497. {
  1498. sym->st_info = ELF64_ST_INFO(sym_bind, ELF64_ST_TYPE(sym->st_info));
  1499. }
  1500. static void sym_update_type(Elf64_Sym *sym, int sym_type)
  1501. {
  1502. sym->st_info = ELF64_ST_INFO(ELF64_ST_BIND(sym->st_info), sym_type);
  1503. }
  1504. static void sym_update_visibility(Elf64_Sym *sym, int sym_vis)
  1505. {
  1506. /* libelf doesn't provide setters for ST_VISIBILITY,
  1507. * but it is stored in the lower 2 bits of st_other
  1508. */
  1509. sym->st_other &= ~0x03;
  1510. sym->st_other |= sym_vis;
  1511. }
  1512. static int linker_append_elf_sym(struct bpf_linker *linker, struct src_obj *obj,
  1513. Elf64_Sym *sym, const char *sym_name, int src_sym_idx)
  1514. {
  1515. struct src_sec *src_sec = NULL;
  1516. struct dst_sec *dst_sec = NULL;
  1517. struct glob_sym *glob_sym = NULL;
  1518. int name_off, sym_type, sym_bind, sym_vis, err;
  1519. int btf_sec_id = 0, btf_id = 0;
  1520. size_t dst_sym_idx;
  1521. Elf64_Sym *dst_sym;
  1522. bool sym_is_extern;
  1523. sym_type = ELF64_ST_TYPE(sym->st_info);
  1524. sym_bind = ELF64_ST_BIND(sym->st_info);
  1525. sym_vis = ELF64_ST_VISIBILITY(sym->st_other);
  1526. sym_is_extern = sym->st_shndx == SHN_UNDEF;
  1527. if (sym_is_extern) {
  1528. if (!obj->btf) {
  1529. pr_warn("externs without BTF info are not supported\n");
  1530. return -ENOTSUP;
  1531. }
  1532. } else if (sym->st_shndx < SHN_LORESERVE) {
  1533. src_sec = &obj->secs[sym->st_shndx];
  1534. if (src_sec->skipped)
  1535. return 0;
  1536. dst_sec = &linker->secs[src_sec->dst_id];
  1537. /* allow only one STT_SECTION symbol per section */
  1538. if (sym_type == STT_SECTION && dst_sec->sec_sym_idx) {
  1539. obj->sym_map[src_sym_idx] = dst_sec->sec_sym_idx;
  1540. return 0;
  1541. }
  1542. }
  1543. if (sym_bind == STB_LOCAL)
  1544. goto add_sym;
  1545. /* find matching BTF info */
  1546. err = find_glob_sym_btf(obj, sym, sym_name, &btf_sec_id, &btf_id);
  1547. if (err)
  1548. return err;
  1549. if (sym_is_extern && btf_sec_id) {
  1550. const char *sec_name = NULL;
  1551. const struct btf_type *t;
  1552. t = btf__type_by_id(obj->btf, btf_sec_id);
  1553. sec_name = btf__str_by_offset(obj->btf, t->name_off);
  1554. /* Clang puts unannotated extern vars into
  1555. * '.extern' BTF DATASEC. Treat them the same
  1556. * as unannotated extern funcs (which are
  1557. * currently not put into any DATASECs).
  1558. * Those don't have associated src_sec/dst_sec.
  1559. */
  1560. if (strcmp(sec_name, BTF_EXTERN_SEC) != 0) {
  1561. src_sec = find_src_sec_by_name(obj, sec_name);
  1562. if (!src_sec) {
  1563. pr_warn("failed to find matching ELF sec '%s'\n", sec_name);
  1564. return -ENOENT;
  1565. }
  1566. dst_sec = &linker->secs[src_sec->dst_id];
  1567. }
  1568. }
  1569. glob_sym = find_glob_sym(linker, sym_name);
  1570. if (glob_sym) {
  1571. /* Preventively resolve to existing symbol. This is
  1572. * needed for further relocation symbol remapping in
  1573. * the next step of linking.
  1574. */
  1575. obj->sym_map[src_sym_idx] = glob_sym->sym_idx;
  1576. /* If both symbols are non-externs, at least one of
  1577. * them has to be STB_WEAK, otherwise they are in
  1578. * a conflict with each other.
  1579. */
  1580. if (!sym_is_extern && !glob_sym->is_extern
  1581. && !glob_sym->is_weak && sym_bind != STB_WEAK) {
  1582. pr_warn("conflicting non-weak symbol #%d (%s) definition in '%s'\n",
  1583. src_sym_idx, sym_name, obj->filename);
  1584. return -EINVAL;
  1585. }
  1586. if (!glob_syms_match(sym_name, linker, glob_sym, obj, sym, src_sym_idx, btf_id))
  1587. return -EINVAL;
  1588. dst_sym = get_sym_by_idx(linker, glob_sym->sym_idx);
  1589. /* If new symbol is strong, then force dst_sym to be strong as
  1590. * well; this way a mix of weak and non-weak extern
  1591. * definitions will end up being strong.
  1592. */
  1593. if (sym_bind == STB_GLOBAL) {
  1594. /* We still need to preserve type (NOTYPE or
  1595. * OBJECT/FUNC, depending on whether the symbol is
  1596. * extern or not)
  1597. */
  1598. sym_update_bind(dst_sym, STB_GLOBAL);
  1599. glob_sym->is_weak = false;
  1600. }
  1601. /* Non-default visibility is "contaminating", with stricter
  1602. * visibility overwriting more permissive ones, even if more
  1603. * permissive visibility comes from just an extern definition.
  1604. * Currently only STV_DEFAULT and STV_HIDDEN are allowed and
  1605. * ensured by ELF symbol sanity checks above.
  1606. */
  1607. if (sym_vis > ELF64_ST_VISIBILITY(dst_sym->st_other))
  1608. sym_update_visibility(dst_sym, sym_vis);
  1609. /* If the new symbol is extern, then regardless if
  1610. * existing symbol is extern or resolved global, just
  1611. * keep the existing one untouched.
  1612. */
  1613. if (sym_is_extern)
  1614. return 0;
  1615. /* If existing symbol is a strong resolved symbol, bail out,
  1616. * because we lost resolution battle have nothing to
  1617. * contribute. We already checked abover that there is no
  1618. * strong-strong conflict. We also already tightened binding
  1619. * and visibility, so nothing else to contribute at that point.
  1620. */
  1621. if (!glob_sym->is_extern && sym_bind == STB_WEAK)
  1622. return 0;
  1623. /* At this point, new symbol is strong non-extern,
  1624. * so overwrite glob_sym with new symbol information.
  1625. * Preserve binding and visibility.
  1626. */
  1627. sym_update_type(dst_sym, sym_type);
  1628. dst_sym->st_shndx = dst_sec->sec_idx;
  1629. dst_sym->st_value = src_sec->dst_off + sym->st_value;
  1630. dst_sym->st_size = sym->st_size;
  1631. /* see comment below about dst_sec->id vs dst_sec->sec_idx */
  1632. glob_sym->sec_id = dst_sec->id;
  1633. glob_sym->is_extern = false;
  1634. if (complete_extern_btf_info(linker->btf, glob_sym->btf_id,
  1635. obj->btf, btf_id))
  1636. return -EINVAL;
  1637. /* request updating VAR's/FUNC's underlying BTF type when appending BTF type */
  1638. glob_sym->underlying_btf_id = 0;
  1639. obj->sym_map[src_sym_idx] = glob_sym->sym_idx;
  1640. return 0;
  1641. }
  1642. add_sym:
  1643. name_off = strset__add_str(linker->strtab_strs, sym_name);
  1644. if (name_off < 0)
  1645. return name_off;
  1646. dst_sym = add_new_sym(linker, &dst_sym_idx);
  1647. if (!dst_sym)
  1648. return -ENOMEM;
  1649. dst_sym->st_name = name_off;
  1650. dst_sym->st_info = sym->st_info;
  1651. dst_sym->st_other = sym->st_other;
  1652. dst_sym->st_shndx = dst_sec ? dst_sec->sec_idx : sym->st_shndx;
  1653. dst_sym->st_value = (src_sec ? src_sec->dst_off : 0) + sym->st_value;
  1654. dst_sym->st_size = sym->st_size;
  1655. obj->sym_map[src_sym_idx] = dst_sym_idx;
  1656. if (sym_type == STT_SECTION && dst_sym) {
  1657. dst_sec->sec_sym_idx = dst_sym_idx;
  1658. dst_sym->st_value = 0;
  1659. }
  1660. if (sym_bind != STB_LOCAL) {
  1661. glob_sym = add_glob_sym(linker);
  1662. if (!glob_sym)
  1663. return -ENOMEM;
  1664. glob_sym->sym_idx = dst_sym_idx;
  1665. /* we use dst_sec->id (and not dst_sec->sec_idx), because
  1666. * ephemeral sections (.kconfig, .ksyms, etc) don't have
  1667. * sec_idx (as they don't have corresponding ELF section), but
  1668. * still have id. .extern doesn't have even ephemeral section
  1669. * associated with it, so dst_sec->id == dst_sec->sec_idx == 0.
  1670. */
  1671. glob_sym->sec_id = dst_sec ? dst_sec->id : 0;
  1672. glob_sym->name_off = name_off;
  1673. /* we will fill btf_id in during BTF merging step */
  1674. glob_sym->btf_id = 0;
  1675. glob_sym->is_extern = sym_is_extern;
  1676. glob_sym->is_weak = sym_bind == STB_WEAK;
  1677. }
  1678. return 0;
  1679. }
  1680. static int linker_append_elf_relos(struct bpf_linker *linker, struct src_obj *obj)
  1681. {
  1682. struct src_sec *src_symtab = &obj->secs[obj->symtab_sec_idx];
  1683. struct dst_sec *dst_symtab;
  1684. int i, err;
  1685. for (i = 1; i < obj->sec_cnt; i++) {
  1686. struct src_sec *src_sec, *src_linked_sec;
  1687. struct dst_sec *dst_sec, *dst_linked_sec;
  1688. Elf64_Rel *src_rel, *dst_rel;
  1689. int j, n;
  1690. src_sec = &obj->secs[i];
  1691. if (!is_relo_sec(src_sec))
  1692. continue;
  1693. /* shdr->sh_info points to relocatable section */
  1694. src_linked_sec = &obj->secs[src_sec->shdr->sh_info];
  1695. if (src_linked_sec->skipped)
  1696. continue;
  1697. dst_sec = find_dst_sec_by_name(linker, src_sec->sec_name);
  1698. if (!dst_sec) {
  1699. dst_sec = add_dst_sec(linker, src_sec->sec_name);
  1700. if (!dst_sec)
  1701. return -ENOMEM;
  1702. err = init_sec(linker, dst_sec, src_sec);
  1703. if (err) {
  1704. pr_warn("failed to init section '%s'\n", src_sec->sec_name);
  1705. return err;
  1706. }
  1707. } else if (!secs_match(dst_sec, src_sec)) {
  1708. pr_warn("sections %s are not compatible\n", src_sec->sec_name);
  1709. return -1;
  1710. }
  1711. /* add_dst_sec() above could have invalidated linker->secs */
  1712. dst_symtab = &linker->secs[linker->symtab_sec_idx];
  1713. /* shdr->sh_link points to SYMTAB */
  1714. dst_sec->shdr->sh_link = linker->symtab_sec_idx;
  1715. /* shdr->sh_info points to relocated section */
  1716. dst_linked_sec = &linker->secs[src_linked_sec->dst_id];
  1717. dst_sec->shdr->sh_info = dst_linked_sec->sec_idx;
  1718. src_sec->dst_id = dst_sec->id;
  1719. err = extend_sec(linker, dst_sec, src_sec);
  1720. if (err)
  1721. return err;
  1722. src_rel = src_sec->data->d_buf;
  1723. dst_rel = dst_sec->raw_data + src_sec->dst_off;
  1724. n = src_sec->shdr->sh_size / src_sec->shdr->sh_entsize;
  1725. for (j = 0; j < n; j++, src_rel++, dst_rel++) {
  1726. size_t src_sym_idx = ELF64_R_SYM(src_rel->r_info);
  1727. size_t sym_type = ELF64_R_TYPE(src_rel->r_info);
  1728. Elf64_Sym *src_sym, *dst_sym;
  1729. size_t dst_sym_idx;
  1730. src_sym_idx = ELF64_R_SYM(src_rel->r_info);
  1731. src_sym = src_symtab->data->d_buf + sizeof(*src_sym) * src_sym_idx;
  1732. dst_sym_idx = obj->sym_map[src_sym_idx];
  1733. dst_sym = dst_symtab->raw_data + sizeof(*dst_sym) * dst_sym_idx;
  1734. dst_rel->r_offset += src_linked_sec->dst_off;
  1735. sym_type = ELF64_R_TYPE(src_rel->r_info);
  1736. dst_rel->r_info = ELF64_R_INFO(dst_sym_idx, sym_type);
  1737. if (ELF64_ST_TYPE(src_sym->st_info) == STT_SECTION) {
  1738. struct src_sec *sec = &obj->secs[src_sym->st_shndx];
  1739. struct bpf_insn *insn;
  1740. if (src_linked_sec->shdr->sh_flags & SHF_EXECINSTR) {
  1741. /* calls to the very first static function inside
  1742. * .text section at offset 0 will
  1743. * reference section symbol, not the
  1744. * function symbol. Fix that up,
  1745. * otherwise it won't be possible to
  1746. * relocate calls to two different
  1747. * static functions with the same name
  1748. * (rom two different object files)
  1749. */
  1750. insn = dst_linked_sec->raw_data + dst_rel->r_offset;
  1751. if (insn->code == (BPF_JMP | BPF_CALL))
  1752. insn->imm += sec->dst_off / sizeof(struct bpf_insn);
  1753. else
  1754. insn->imm += sec->dst_off;
  1755. } else {
  1756. pr_warn("relocation against STT_SECTION in non-exec section is not supported!\n");
  1757. return -EINVAL;
  1758. }
  1759. }
  1760. }
  1761. }
  1762. return 0;
  1763. }
  1764. static Elf64_Sym *find_sym_by_name(struct src_obj *obj, size_t sec_idx,
  1765. int sym_type, const char *sym_name)
  1766. {
  1767. struct src_sec *symtab = &obj->secs[obj->symtab_sec_idx];
  1768. Elf64_Sym *sym = symtab->data->d_buf;
  1769. int i, n = symtab->shdr->sh_size / symtab->shdr->sh_entsize;
  1770. int str_sec_idx = symtab->shdr->sh_link;
  1771. const char *name;
  1772. for (i = 0; i < n; i++, sym++) {
  1773. if (sym->st_shndx != sec_idx)
  1774. continue;
  1775. if (ELF64_ST_TYPE(sym->st_info) != sym_type)
  1776. continue;
  1777. name = elf_strptr(obj->elf, str_sec_idx, sym->st_name);
  1778. if (!name)
  1779. return NULL;
  1780. if (strcmp(sym_name, name) != 0)
  1781. continue;
  1782. return sym;
  1783. }
  1784. return NULL;
  1785. }
  1786. static int linker_fixup_btf(struct src_obj *obj)
  1787. {
  1788. const char *sec_name;
  1789. struct src_sec *sec;
  1790. int i, j, n, m;
  1791. if (!obj->btf)
  1792. return 0;
  1793. n = btf__type_cnt(obj->btf);
  1794. for (i = 1; i < n; i++) {
  1795. struct btf_var_secinfo *vi;
  1796. struct btf_type *t;
  1797. t = btf_type_by_id(obj->btf, i);
  1798. if (btf_kind(t) != BTF_KIND_DATASEC)
  1799. continue;
  1800. sec_name = btf__str_by_offset(obj->btf, t->name_off);
  1801. sec = find_src_sec_by_name(obj, sec_name);
  1802. if (sec) {
  1803. /* record actual section size, unless ephemeral */
  1804. if (sec->shdr)
  1805. t->size = sec->shdr->sh_size;
  1806. } else {
  1807. /* BTF can have some sections that are not represented
  1808. * in ELF, e.g., .kconfig, .ksyms, .extern, which are used
  1809. * for special extern variables.
  1810. *
  1811. * For all but one such special (ephemeral)
  1812. * sections, we pre-create "section shells" to be able
  1813. * to keep track of extra per-section metadata later
  1814. * (e.g., those BTF extern variables).
  1815. *
  1816. * .extern is even more special, though, because it
  1817. * contains extern variables that need to be resolved
  1818. * by static linker, not libbpf and kernel. When such
  1819. * externs are resolved, we are going to remove them
  1820. * from .extern BTF section and might end up not
  1821. * needing it at all. Each resolved extern should have
  1822. * matching non-extern VAR/FUNC in other sections.
  1823. *
  1824. * We do support leaving some of the externs
  1825. * unresolved, though, to support cases of building
  1826. * libraries, which will later be linked against final
  1827. * BPF applications. So if at finalization we still
  1828. * see unresolved externs, we'll create .extern
  1829. * section on our own.
  1830. */
  1831. if (strcmp(sec_name, BTF_EXTERN_SEC) == 0)
  1832. continue;
  1833. sec = add_src_sec(obj, sec_name);
  1834. if (!sec)
  1835. return -ENOMEM;
  1836. sec->ephemeral = true;
  1837. sec->sec_idx = 0; /* will match UNDEF shndx in ELF */
  1838. }
  1839. /* remember ELF section and its BTF type ID match */
  1840. sec->sec_type_id = i;
  1841. /* fix up variable offsets */
  1842. vi = btf_var_secinfos(t);
  1843. for (j = 0, m = btf_vlen(t); j < m; j++, vi++) {
  1844. const struct btf_type *vt = btf__type_by_id(obj->btf, vi->type);
  1845. const char *var_name = btf__str_by_offset(obj->btf, vt->name_off);
  1846. int var_linkage = btf_var(vt)->linkage;
  1847. Elf64_Sym *sym;
  1848. /* no need to patch up static or extern vars */
  1849. if (var_linkage != BTF_VAR_GLOBAL_ALLOCATED)
  1850. continue;
  1851. sym = find_sym_by_name(obj, sec->sec_idx, STT_OBJECT, var_name);
  1852. if (!sym) {
  1853. pr_warn("failed to find symbol for variable '%s' in section '%s'\n", var_name, sec_name);
  1854. return -ENOENT;
  1855. }
  1856. vi->offset = sym->st_value;
  1857. }
  1858. }
  1859. return 0;
  1860. }
  1861. static int remap_type_id(__u32 *type_id, void *ctx)
  1862. {
  1863. int *id_map = ctx;
  1864. int new_id = id_map[*type_id];
  1865. /* Error out if the type wasn't remapped. Ignore VOID which stays VOID. */
  1866. if (new_id == 0 && *type_id != 0) {
  1867. pr_warn("failed to find new ID mapping for original BTF type ID %u\n", *type_id);
  1868. return -EINVAL;
  1869. }
  1870. *type_id = id_map[*type_id];
  1871. return 0;
  1872. }
  1873. static int linker_append_btf(struct bpf_linker *linker, struct src_obj *obj)
  1874. {
  1875. const struct btf_type *t;
  1876. int i, j, n, start_id, id;
  1877. const char *name;
  1878. if (!obj->btf)
  1879. return 0;
  1880. start_id = btf__type_cnt(linker->btf);
  1881. n = btf__type_cnt(obj->btf);
  1882. obj->btf_type_map = calloc(n + 1, sizeof(int));
  1883. if (!obj->btf_type_map)
  1884. return -ENOMEM;
  1885. for (i = 1; i < n; i++) {
  1886. struct glob_sym *glob_sym = NULL;
  1887. t = btf__type_by_id(obj->btf, i);
  1888. /* DATASECs are handled specially below */
  1889. if (btf_kind(t) == BTF_KIND_DATASEC)
  1890. continue;
  1891. if (btf_is_non_static(t)) {
  1892. /* there should be glob_sym already */
  1893. name = btf__str_by_offset(obj->btf, t->name_off);
  1894. glob_sym = find_glob_sym(linker, name);
  1895. /* VARs without corresponding glob_sym are those that
  1896. * belong to skipped/deduplicated sections (i.e.,
  1897. * license and version), so just skip them
  1898. */
  1899. if (!glob_sym)
  1900. continue;
  1901. /* linker_append_elf_sym() might have requested
  1902. * updating underlying type ID, if extern was resolved
  1903. * to strong symbol or weak got upgraded to non-weak
  1904. */
  1905. if (glob_sym->underlying_btf_id == 0)
  1906. glob_sym->underlying_btf_id = -t->type;
  1907. /* globals from previous object files that match our
  1908. * VAR/FUNC already have a corresponding associated
  1909. * BTF type, so just make sure to use it
  1910. */
  1911. if (glob_sym->btf_id) {
  1912. /* reuse existing BTF type for global var/func */
  1913. obj->btf_type_map[i] = glob_sym->btf_id;
  1914. continue;
  1915. }
  1916. }
  1917. id = btf__add_type(linker->btf, obj->btf, t);
  1918. if (id < 0) {
  1919. pr_warn("failed to append BTF type #%d from file '%s'\n", i, obj->filename);
  1920. return id;
  1921. }
  1922. obj->btf_type_map[i] = id;
  1923. /* record just appended BTF type for var/func */
  1924. if (glob_sym) {
  1925. glob_sym->btf_id = id;
  1926. glob_sym->underlying_btf_id = -t->type;
  1927. }
  1928. }
  1929. /* remap all the types except DATASECs */
  1930. n = btf__type_cnt(linker->btf);
  1931. for (i = start_id; i < n; i++) {
  1932. struct btf_type *dst_t = btf_type_by_id(linker->btf, i);
  1933. if (btf_type_visit_type_ids(dst_t, remap_type_id, obj->btf_type_map))
  1934. return -EINVAL;
  1935. }
  1936. /* Rewrite VAR/FUNC underlying types (i.e., FUNC's FUNC_PROTO and VAR's
  1937. * actual type), if necessary
  1938. */
  1939. for (i = 0; i < linker->glob_sym_cnt; i++) {
  1940. struct glob_sym *glob_sym = &linker->glob_syms[i];
  1941. struct btf_type *glob_t;
  1942. if (glob_sym->underlying_btf_id >= 0)
  1943. continue;
  1944. glob_sym->underlying_btf_id = obj->btf_type_map[-glob_sym->underlying_btf_id];
  1945. glob_t = btf_type_by_id(linker->btf, glob_sym->btf_id);
  1946. glob_t->type = glob_sym->underlying_btf_id;
  1947. }
  1948. /* append DATASEC info */
  1949. for (i = 1; i < obj->sec_cnt; i++) {
  1950. struct src_sec *src_sec;
  1951. struct dst_sec *dst_sec;
  1952. const struct btf_var_secinfo *src_var;
  1953. struct btf_var_secinfo *dst_var;
  1954. src_sec = &obj->secs[i];
  1955. if (!src_sec->sec_type_id || src_sec->skipped)
  1956. continue;
  1957. dst_sec = &linker->secs[src_sec->dst_id];
  1958. /* Mark section as having BTF regardless of the presence of
  1959. * variables. In some cases compiler might generate empty BTF
  1960. * with no variables information. E.g., when promoting local
  1961. * array/structure variable initial values and BPF object
  1962. * file otherwise has no read-only static variables in
  1963. * .rodata. We need to preserve such empty BTF and just set
  1964. * correct section size.
  1965. */
  1966. dst_sec->has_btf = true;
  1967. t = btf__type_by_id(obj->btf, src_sec->sec_type_id);
  1968. src_var = btf_var_secinfos(t);
  1969. n = btf_vlen(t);
  1970. for (j = 0; j < n; j++, src_var++) {
  1971. void *sec_vars = dst_sec->sec_vars;
  1972. int new_id = obj->btf_type_map[src_var->type];
  1973. struct glob_sym *glob_sym = NULL;
  1974. t = btf_type_by_id(linker->btf, new_id);
  1975. if (btf_is_non_static(t)) {
  1976. name = btf__str_by_offset(linker->btf, t->name_off);
  1977. glob_sym = find_glob_sym(linker, name);
  1978. if (glob_sym->sec_id != dst_sec->id) {
  1979. pr_warn("global '%s': section mismatch %d vs %d\n",
  1980. name, glob_sym->sec_id, dst_sec->id);
  1981. return -EINVAL;
  1982. }
  1983. }
  1984. /* If there is already a member (VAR or FUNC) mapped
  1985. * to the same type, don't add a duplicate entry.
  1986. * This will happen when multiple object files define
  1987. * the same extern VARs/FUNCs.
  1988. */
  1989. if (glob_sym && glob_sym->var_idx >= 0) {
  1990. __s64 sz;
  1991. dst_var = &dst_sec->sec_vars[glob_sym->var_idx];
  1992. /* Because underlying BTF type might have
  1993. * changed, so might its size have changed, so
  1994. * re-calculate and update it in sec_var.
  1995. */
  1996. sz = btf__resolve_size(linker->btf, glob_sym->underlying_btf_id);
  1997. if (sz < 0) {
  1998. pr_warn("global '%s': failed to resolve size of underlying type: %d\n",
  1999. name, (int)sz);
  2000. return -EINVAL;
  2001. }
  2002. dst_var->size = sz;
  2003. continue;
  2004. }
  2005. sec_vars = libbpf_reallocarray(sec_vars,
  2006. dst_sec->sec_var_cnt + 1,
  2007. sizeof(*dst_sec->sec_vars));
  2008. if (!sec_vars)
  2009. return -ENOMEM;
  2010. dst_sec->sec_vars = sec_vars;
  2011. dst_sec->sec_var_cnt++;
  2012. dst_var = &dst_sec->sec_vars[dst_sec->sec_var_cnt - 1];
  2013. dst_var->type = obj->btf_type_map[src_var->type];
  2014. dst_var->size = src_var->size;
  2015. dst_var->offset = src_sec->dst_off + src_var->offset;
  2016. if (glob_sym)
  2017. glob_sym->var_idx = dst_sec->sec_var_cnt - 1;
  2018. }
  2019. }
  2020. return 0;
  2021. }
  2022. static void *add_btf_ext_rec(struct btf_ext_sec_data *ext_data, const void *src_rec)
  2023. {
  2024. void *tmp;
  2025. tmp = libbpf_reallocarray(ext_data->recs, ext_data->rec_cnt + 1, ext_data->rec_sz);
  2026. if (!tmp)
  2027. return NULL;
  2028. ext_data->recs = tmp;
  2029. tmp += ext_data->rec_cnt * ext_data->rec_sz;
  2030. memcpy(tmp, src_rec, ext_data->rec_sz);
  2031. ext_data->rec_cnt++;
  2032. return tmp;
  2033. }
  2034. static int linker_append_btf_ext(struct bpf_linker *linker, struct src_obj *obj)
  2035. {
  2036. const struct btf_ext_info_sec *ext_sec;
  2037. const char *sec_name, *s;
  2038. struct src_sec *src_sec;
  2039. struct dst_sec *dst_sec;
  2040. int rec_sz, str_off, i;
  2041. if (!obj->btf_ext)
  2042. return 0;
  2043. rec_sz = obj->btf_ext->func_info.rec_size;
  2044. for_each_btf_ext_sec(&obj->btf_ext->func_info, ext_sec) {
  2045. struct bpf_func_info_min *src_rec, *dst_rec;
  2046. sec_name = btf__name_by_offset(obj->btf, ext_sec->sec_name_off);
  2047. src_sec = find_src_sec_by_name(obj, sec_name);
  2048. if (!src_sec) {
  2049. pr_warn("can't find section '%s' referenced from .BTF.ext\n", sec_name);
  2050. return -EINVAL;
  2051. }
  2052. dst_sec = &linker->secs[src_sec->dst_id];
  2053. if (dst_sec->func_info.rec_sz == 0)
  2054. dst_sec->func_info.rec_sz = rec_sz;
  2055. if (dst_sec->func_info.rec_sz != rec_sz) {
  2056. pr_warn("incompatible .BTF.ext record sizes for section '%s'\n", sec_name);
  2057. return -EINVAL;
  2058. }
  2059. for_each_btf_ext_rec(&obj->btf_ext->func_info, ext_sec, i, src_rec) {
  2060. dst_rec = add_btf_ext_rec(&dst_sec->func_info, src_rec);
  2061. if (!dst_rec)
  2062. return -ENOMEM;
  2063. dst_rec->insn_off += src_sec->dst_off;
  2064. dst_rec->type_id = obj->btf_type_map[dst_rec->type_id];
  2065. }
  2066. }
  2067. rec_sz = obj->btf_ext->line_info.rec_size;
  2068. for_each_btf_ext_sec(&obj->btf_ext->line_info, ext_sec) {
  2069. struct bpf_line_info_min *src_rec, *dst_rec;
  2070. sec_name = btf__name_by_offset(obj->btf, ext_sec->sec_name_off);
  2071. src_sec = find_src_sec_by_name(obj, sec_name);
  2072. if (!src_sec) {
  2073. pr_warn("can't find section '%s' referenced from .BTF.ext\n", sec_name);
  2074. return -EINVAL;
  2075. }
  2076. dst_sec = &linker->secs[src_sec->dst_id];
  2077. if (dst_sec->line_info.rec_sz == 0)
  2078. dst_sec->line_info.rec_sz = rec_sz;
  2079. if (dst_sec->line_info.rec_sz != rec_sz) {
  2080. pr_warn("incompatible .BTF.ext record sizes for section '%s'\n", sec_name);
  2081. return -EINVAL;
  2082. }
  2083. for_each_btf_ext_rec(&obj->btf_ext->line_info, ext_sec, i, src_rec) {
  2084. dst_rec = add_btf_ext_rec(&dst_sec->line_info, src_rec);
  2085. if (!dst_rec)
  2086. return -ENOMEM;
  2087. dst_rec->insn_off += src_sec->dst_off;
  2088. s = btf__str_by_offset(obj->btf, src_rec->file_name_off);
  2089. str_off = btf__add_str(linker->btf, s);
  2090. if (str_off < 0)
  2091. return -ENOMEM;
  2092. dst_rec->file_name_off = str_off;
  2093. s = btf__str_by_offset(obj->btf, src_rec->line_off);
  2094. str_off = btf__add_str(linker->btf, s);
  2095. if (str_off < 0)
  2096. return -ENOMEM;
  2097. dst_rec->line_off = str_off;
  2098. /* dst_rec->line_col is fine */
  2099. }
  2100. }
  2101. rec_sz = obj->btf_ext->core_relo_info.rec_size;
  2102. for_each_btf_ext_sec(&obj->btf_ext->core_relo_info, ext_sec) {
  2103. struct bpf_core_relo *src_rec, *dst_rec;
  2104. sec_name = btf__name_by_offset(obj->btf, ext_sec->sec_name_off);
  2105. src_sec = find_src_sec_by_name(obj, sec_name);
  2106. if (!src_sec) {
  2107. pr_warn("can't find section '%s' referenced from .BTF.ext\n", sec_name);
  2108. return -EINVAL;
  2109. }
  2110. dst_sec = &linker->secs[src_sec->dst_id];
  2111. if (dst_sec->core_relo_info.rec_sz == 0)
  2112. dst_sec->core_relo_info.rec_sz = rec_sz;
  2113. if (dst_sec->core_relo_info.rec_sz != rec_sz) {
  2114. pr_warn("incompatible .BTF.ext record sizes for section '%s'\n", sec_name);
  2115. return -EINVAL;
  2116. }
  2117. for_each_btf_ext_rec(&obj->btf_ext->core_relo_info, ext_sec, i, src_rec) {
  2118. dst_rec = add_btf_ext_rec(&dst_sec->core_relo_info, src_rec);
  2119. if (!dst_rec)
  2120. return -ENOMEM;
  2121. dst_rec->insn_off += src_sec->dst_off;
  2122. dst_rec->type_id = obj->btf_type_map[dst_rec->type_id];
  2123. s = btf__str_by_offset(obj->btf, src_rec->access_str_off);
  2124. str_off = btf__add_str(linker->btf, s);
  2125. if (str_off < 0)
  2126. return -ENOMEM;
  2127. dst_rec->access_str_off = str_off;
  2128. /* dst_rec->kind is fine */
  2129. }
  2130. }
  2131. return 0;
  2132. }
  2133. int bpf_linker__finalize(struct bpf_linker *linker)
  2134. {
  2135. struct dst_sec *sec;
  2136. size_t strs_sz;
  2137. const void *strs;
  2138. int err, i;
  2139. if (!linker->elf)
  2140. return libbpf_err(-EINVAL);
  2141. err = finalize_btf(linker);
  2142. if (err)
  2143. return libbpf_err(err);
  2144. /* Finalize strings */
  2145. strs_sz = strset__data_size(linker->strtab_strs);
  2146. strs = strset__data(linker->strtab_strs);
  2147. sec = &linker->secs[linker->strtab_sec_idx];
  2148. sec->data->d_align = 1;
  2149. sec->data->d_off = 0LL;
  2150. sec->data->d_buf = (void *)strs;
  2151. sec->data->d_type = ELF_T_BYTE;
  2152. sec->data->d_size = strs_sz;
  2153. sec->shdr->sh_size = strs_sz;
  2154. for (i = 1; i < linker->sec_cnt; i++) {
  2155. sec = &linker->secs[i];
  2156. /* STRTAB is handled specially above */
  2157. if (sec->sec_idx == linker->strtab_sec_idx)
  2158. continue;
  2159. /* special ephemeral sections (.ksyms, .kconfig, etc) */
  2160. if (!sec->scn)
  2161. continue;
  2162. sec->data->d_buf = sec->raw_data;
  2163. }
  2164. /* Finalize ELF layout */
  2165. if (elf_update(linker->elf, ELF_C_NULL) < 0) {
  2166. err = -errno;
  2167. pr_warn_elf("failed to finalize ELF layout");
  2168. return libbpf_err(err);
  2169. }
  2170. /* Write out final ELF contents */
  2171. if (elf_update(linker->elf, ELF_C_WRITE) < 0) {
  2172. err = -errno;
  2173. pr_warn_elf("failed to write ELF contents");
  2174. return libbpf_err(err);
  2175. }
  2176. elf_end(linker->elf);
  2177. close(linker->fd);
  2178. linker->elf = NULL;
  2179. linker->fd = -1;
  2180. return 0;
  2181. }
  2182. static int emit_elf_data_sec(struct bpf_linker *linker, const char *sec_name,
  2183. size_t align, const void *raw_data, size_t raw_sz)
  2184. {
  2185. Elf_Scn *scn;
  2186. Elf_Data *data;
  2187. Elf64_Shdr *shdr;
  2188. int name_off;
  2189. name_off = strset__add_str(linker->strtab_strs, sec_name);
  2190. if (name_off < 0)
  2191. return name_off;
  2192. scn = elf_newscn(linker->elf);
  2193. if (!scn)
  2194. return -ENOMEM;
  2195. data = elf_newdata(scn);
  2196. if (!data)
  2197. return -ENOMEM;
  2198. shdr = elf64_getshdr(scn);
  2199. if (!shdr)
  2200. return -EINVAL;
  2201. shdr->sh_name = name_off;
  2202. shdr->sh_type = SHT_PROGBITS;
  2203. shdr->sh_flags = 0;
  2204. shdr->sh_size = raw_sz;
  2205. shdr->sh_link = 0;
  2206. shdr->sh_info = 0;
  2207. shdr->sh_addralign = align;
  2208. shdr->sh_entsize = 0;
  2209. data->d_type = ELF_T_BYTE;
  2210. data->d_size = raw_sz;
  2211. data->d_buf = (void *)raw_data;
  2212. data->d_align = align;
  2213. data->d_off = 0;
  2214. return 0;
  2215. }
  2216. static int finalize_btf(struct bpf_linker *linker)
  2217. {
  2218. LIBBPF_OPTS(btf_dedup_opts, opts);
  2219. struct btf *btf = linker->btf;
  2220. const void *raw_data;
  2221. int i, j, id, err;
  2222. __u32 raw_sz;
  2223. /* bail out if no BTF data was produced */
  2224. if (btf__type_cnt(linker->btf) == 1)
  2225. return 0;
  2226. for (i = 1; i < linker->sec_cnt; i++) {
  2227. struct dst_sec *sec = &linker->secs[i];
  2228. if (!sec->has_btf)
  2229. continue;
  2230. id = btf__add_datasec(btf, sec->sec_name, sec->sec_sz);
  2231. if (id < 0) {
  2232. pr_warn("failed to add consolidated BTF type for datasec '%s': %d\n",
  2233. sec->sec_name, id);
  2234. return id;
  2235. }
  2236. for (j = 0; j < sec->sec_var_cnt; j++) {
  2237. struct btf_var_secinfo *vi = &sec->sec_vars[j];
  2238. if (btf__add_datasec_var_info(btf, vi->type, vi->offset, vi->size))
  2239. return -EINVAL;
  2240. }
  2241. }
  2242. err = finalize_btf_ext(linker);
  2243. if (err) {
  2244. pr_warn(".BTF.ext generation failed: %d\n", err);
  2245. return err;
  2246. }
  2247. opts.btf_ext = linker->btf_ext;
  2248. err = btf__dedup(linker->btf, &opts);
  2249. if (err) {
  2250. pr_warn("BTF dedup failed: %d\n", err);
  2251. return err;
  2252. }
  2253. /* Emit .BTF section */
  2254. raw_data = btf__raw_data(linker->btf, &raw_sz);
  2255. if (!raw_data)
  2256. return -ENOMEM;
  2257. err = emit_elf_data_sec(linker, BTF_ELF_SEC, 8, raw_data, raw_sz);
  2258. if (err) {
  2259. pr_warn("failed to write out .BTF ELF section: %d\n", err);
  2260. return err;
  2261. }
  2262. /* Emit .BTF.ext section */
  2263. if (linker->btf_ext) {
  2264. raw_data = btf_ext__get_raw_data(linker->btf_ext, &raw_sz);
  2265. if (!raw_data)
  2266. return -ENOMEM;
  2267. err = emit_elf_data_sec(linker, BTF_EXT_ELF_SEC, 8, raw_data, raw_sz);
  2268. if (err) {
  2269. pr_warn("failed to write out .BTF.ext ELF section: %d\n", err);
  2270. return err;
  2271. }
  2272. }
  2273. return 0;
  2274. }
  2275. static int emit_btf_ext_data(struct bpf_linker *linker, void *output,
  2276. const char *sec_name, struct btf_ext_sec_data *sec_data)
  2277. {
  2278. struct btf_ext_info_sec *sec_info;
  2279. void *cur = output;
  2280. int str_off;
  2281. size_t sz;
  2282. if (!sec_data->rec_cnt)
  2283. return 0;
  2284. str_off = btf__add_str(linker->btf, sec_name);
  2285. if (str_off < 0)
  2286. return -ENOMEM;
  2287. sec_info = cur;
  2288. sec_info->sec_name_off = str_off;
  2289. sec_info->num_info = sec_data->rec_cnt;
  2290. cur += sizeof(struct btf_ext_info_sec);
  2291. sz = sec_data->rec_cnt * sec_data->rec_sz;
  2292. memcpy(cur, sec_data->recs, sz);
  2293. cur += sz;
  2294. return cur - output;
  2295. }
  2296. static int finalize_btf_ext(struct bpf_linker *linker)
  2297. {
  2298. size_t funcs_sz = 0, lines_sz = 0, core_relos_sz = 0, total_sz = 0;
  2299. size_t func_rec_sz = 0, line_rec_sz = 0, core_relo_rec_sz = 0;
  2300. struct btf_ext_header *hdr;
  2301. void *data, *cur;
  2302. int i, err, sz;
  2303. /* validate that all sections have the same .BTF.ext record sizes
  2304. * and calculate total data size for each type of data (func info,
  2305. * line info, core relos)
  2306. */
  2307. for (i = 1; i < linker->sec_cnt; i++) {
  2308. struct dst_sec *sec = &linker->secs[i];
  2309. if (sec->func_info.rec_cnt) {
  2310. if (func_rec_sz == 0)
  2311. func_rec_sz = sec->func_info.rec_sz;
  2312. if (func_rec_sz != sec->func_info.rec_sz) {
  2313. pr_warn("mismatch in func_info record size %zu != %u\n",
  2314. func_rec_sz, sec->func_info.rec_sz);
  2315. return -EINVAL;
  2316. }
  2317. funcs_sz += sizeof(struct btf_ext_info_sec) + func_rec_sz * sec->func_info.rec_cnt;
  2318. }
  2319. if (sec->line_info.rec_cnt) {
  2320. if (line_rec_sz == 0)
  2321. line_rec_sz = sec->line_info.rec_sz;
  2322. if (line_rec_sz != sec->line_info.rec_sz) {
  2323. pr_warn("mismatch in line_info record size %zu != %u\n",
  2324. line_rec_sz, sec->line_info.rec_sz);
  2325. return -EINVAL;
  2326. }
  2327. lines_sz += sizeof(struct btf_ext_info_sec) + line_rec_sz * sec->line_info.rec_cnt;
  2328. }
  2329. if (sec->core_relo_info.rec_cnt) {
  2330. if (core_relo_rec_sz == 0)
  2331. core_relo_rec_sz = sec->core_relo_info.rec_sz;
  2332. if (core_relo_rec_sz != sec->core_relo_info.rec_sz) {
  2333. pr_warn("mismatch in core_relo_info record size %zu != %u\n",
  2334. core_relo_rec_sz, sec->core_relo_info.rec_sz);
  2335. return -EINVAL;
  2336. }
  2337. core_relos_sz += sizeof(struct btf_ext_info_sec) + core_relo_rec_sz * sec->core_relo_info.rec_cnt;
  2338. }
  2339. }
  2340. if (!funcs_sz && !lines_sz && !core_relos_sz)
  2341. return 0;
  2342. total_sz += sizeof(struct btf_ext_header);
  2343. if (funcs_sz) {
  2344. funcs_sz += sizeof(__u32); /* record size prefix */
  2345. total_sz += funcs_sz;
  2346. }
  2347. if (lines_sz) {
  2348. lines_sz += sizeof(__u32); /* record size prefix */
  2349. total_sz += lines_sz;
  2350. }
  2351. if (core_relos_sz) {
  2352. core_relos_sz += sizeof(__u32); /* record size prefix */
  2353. total_sz += core_relos_sz;
  2354. }
  2355. cur = data = calloc(1, total_sz);
  2356. if (!data)
  2357. return -ENOMEM;
  2358. hdr = cur;
  2359. hdr->magic = BTF_MAGIC;
  2360. hdr->version = BTF_VERSION;
  2361. hdr->flags = 0;
  2362. hdr->hdr_len = sizeof(struct btf_ext_header);
  2363. cur += sizeof(struct btf_ext_header);
  2364. /* All offsets are in bytes relative to the end of this header */
  2365. hdr->func_info_off = 0;
  2366. hdr->func_info_len = funcs_sz;
  2367. hdr->line_info_off = funcs_sz;
  2368. hdr->line_info_len = lines_sz;
  2369. hdr->core_relo_off = funcs_sz + lines_sz;
  2370. hdr->core_relo_len = core_relos_sz;
  2371. if (funcs_sz) {
  2372. *(__u32 *)cur = func_rec_sz;
  2373. cur += sizeof(__u32);
  2374. for (i = 1; i < linker->sec_cnt; i++) {
  2375. struct dst_sec *sec = &linker->secs[i];
  2376. sz = emit_btf_ext_data(linker, cur, sec->sec_name, &sec->func_info);
  2377. if (sz < 0) {
  2378. err = sz;
  2379. goto out;
  2380. }
  2381. cur += sz;
  2382. }
  2383. }
  2384. if (lines_sz) {
  2385. *(__u32 *)cur = line_rec_sz;
  2386. cur += sizeof(__u32);
  2387. for (i = 1; i < linker->sec_cnt; i++) {
  2388. struct dst_sec *sec = &linker->secs[i];
  2389. sz = emit_btf_ext_data(linker, cur, sec->sec_name, &sec->line_info);
  2390. if (sz < 0) {
  2391. err = sz;
  2392. goto out;
  2393. }
  2394. cur += sz;
  2395. }
  2396. }
  2397. if (core_relos_sz) {
  2398. *(__u32 *)cur = core_relo_rec_sz;
  2399. cur += sizeof(__u32);
  2400. for (i = 1; i < linker->sec_cnt; i++) {
  2401. struct dst_sec *sec = &linker->secs[i];
  2402. sz = emit_btf_ext_data(linker, cur, sec->sec_name, &sec->core_relo_info);
  2403. if (sz < 0) {
  2404. err = sz;
  2405. goto out;
  2406. }
  2407. cur += sz;
  2408. }
  2409. }
  2410. linker->btf_ext = btf_ext__new(data, total_sz);
  2411. err = libbpf_get_error(linker->btf_ext);
  2412. if (err) {
  2413. linker->btf_ext = NULL;
  2414. pr_warn("failed to parse final .BTF.ext data: %d\n", err);
  2415. goto out;
  2416. }
  2417. out:
  2418. free(data);
  2419. return err;
  2420. }