linker.c 81 KB

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