vms-lib.c 60 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444
  1. /* BFD back-end for VMS archive files.
  2. Copyright (C) 2010-2022 Free Software Foundation, Inc.
  3. Written by Tristan Gingold <gingold@adacore.com>, AdaCore.
  4. This file is part of BFD, the Binary File Descriptor library.
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 3 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
  16. MA 02110-1301, USA. */
  17. #include "sysdep.h"
  18. #include "bfd.h"
  19. #include "libbfd.h"
  20. #include "safe-ctype.h"
  21. #include "bfdver.h"
  22. #include "libiberty.h"
  23. #include "vms.h"
  24. #include "vms/lbr.h"
  25. #include "vms/dcx.h"
  26. /* The standard VMS disk block size. */
  27. #ifndef VMS_BLOCK_SIZE
  28. #define VMS_BLOCK_SIZE 512
  29. #endif
  30. /* Maximum key length (which is also the maximum symbol length in archive). */
  31. #define MAX_KEYLEN 128
  32. #define MAX_EKEYLEN 1024
  33. /* DCX Submaps. */
  34. struct dcxsbm_desc
  35. {
  36. unsigned char min_char;
  37. unsigned char max_char;
  38. unsigned char *flags;
  39. unsigned char *nodes;
  40. unsigned short *next;
  41. };
  42. /* Kind of library. Used to filter in archive_p. */
  43. enum vms_lib_kind
  44. {
  45. vms_lib_vax,
  46. vms_lib_alpha,
  47. vms_lib_ia64,
  48. vms_lib_txt
  49. };
  50. /* Back-end private data. */
  51. struct lib_tdata
  52. {
  53. /* Standard tdata for an archive. But we don't use many fields. */
  54. struct artdata artdata;
  55. /* Major version. */
  56. unsigned char ver;
  57. /* Type of the archive. */
  58. unsigned char type;
  59. /* Kind of archive. Summary of its type. */
  60. enum vms_lib_kind kind;
  61. /* Total size of the mhd (element header). */
  62. unsigned int mhd_size;
  63. /* Creation date. */
  64. unsigned int credat_lo;
  65. unsigned int credat_hi;
  66. /* Vector of modules (archive elements), already sorted. */
  67. unsigned int nbr_modules;
  68. struct carsym *modules;
  69. bfd **cache;
  70. /* DCX (decompression) data. */
  71. unsigned int nbr_dcxsbm;
  72. struct dcxsbm_desc *dcxsbm;
  73. };
  74. #define bfd_libdata(bfd) ((struct lib_tdata *)((bfd)->tdata.any))
  75. /* End-Of-Text pattern. This is a special record to mark the end of file. */
  76. static const unsigned char eotdesc[] = { 0x03, 0x00, 0x77, 0x00, 0x77, 0x00 };
  77. /* Describe the current state of carsym entries while building the archive
  78. table of content. Things are simple with Alpha archives as the number
  79. of entries is known, but with IA64 archives a entry can make a reference
  80. to severals members. Therefore we must be able to extend the table on the
  81. fly, but it should be allocated on the bfd - which doesn't support realloc.
  82. To reduce the overhead, the table is initially allocated in the BFD's
  83. objalloc and extended if necessary on the heap. In the later case, it
  84. is finally copied to the BFD's objalloc so that it will automatically be
  85. freed. */
  86. struct carsym_mem
  87. {
  88. /* The table of content. */
  89. struct carsym *idx;
  90. /* Number of entries used in the table. */
  91. unsigned int nbr;
  92. /* Maximum number of entries. */
  93. unsigned int max;
  94. /* Do not allocate more that this number of entries. */
  95. unsigned int limit;
  96. /* If true, the table was reallocated on the heap. If false, it is still
  97. in the BFD's objalloc. */
  98. bool realloced;
  99. };
  100. /* Simply add a name to the index. */
  101. static bool
  102. vms_add_index (struct carsym_mem *cs, char *name,
  103. unsigned int idx_vbn, unsigned int idx_off)
  104. {
  105. if (cs->nbr == cs->max)
  106. {
  107. struct carsym *n;
  108. size_t amt;
  109. if (cs->max > -33u / 2 || cs->max >= cs->limit)
  110. {
  111. bfd_set_error (bfd_error_file_too_big);
  112. return false;
  113. }
  114. cs->max = 2 * cs->max + 32;
  115. if (cs->max > cs->limit)
  116. cs->max = cs->limit;
  117. if (_bfd_mul_overflow (cs->max, sizeof (struct carsym), &amt))
  118. {
  119. bfd_set_error (bfd_error_file_too_big);
  120. return false;
  121. }
  122. if (!cs->realloced)
  123. {
  124. n = bfd_malloc (amt);
  125. if (n == NULL)
  126. return false;
  127. memcpy (n, cs->idx, cs->nbr * sizeof (struct carsym));
  128. /* And unfortunately we can't free cs->idx. */
  129. }
  130. else
  131. {
  132. n = bfd_realloc_or_free (cs->idx, amt);
  133. if (n == NULL)
  134. return false;
  135. }
  136. cs->idx = n;
  137. cs->realloced = true;
  138. }
  139. cs->idx[cs->nbr].file_offset = (idx_vbn - 1) * VMS_BLOCK_SIZE + idx_off;
  140. cs->idx[cs->nbr].name = name;
  141. cs->nbr++;
  142. return true;
  143. }
  144. /* Follow all member of a lns list (pointed by RFA) and add indexes for
  145. NAME. Return FALSE in case of error. */
  146. static bool
  147. vms_add_indexes_from_list (bfd *abfd, struct carsym_mem *cs, char *name,
  148. struct vms_rfa *rfa)
  149. {
  150. struct vms_lns lns;
  151. unsigned int vbn;
  152. file_ptr off;
  153. while (1)
  154. {
  155. vbn = bfd_getl32 (rfa->vbn);
  156. if (vbn == 0)
  157. return true;
  158. /* Read the LHS. */
  159. off = (vbn - 1) * VMS_BLOCK_SIZE + bfd_getl16 (rfa->offset);
  160. if (bfd_seek (abfd, off, SEEK_SET) != 0
  161. || bfd_bread (&lns, sizeof (lns), abfd) != sizeof (lns))
  162. return false;
  163. if (!vms_add_index (cs, name,
  164. bfd_getl32 (lns.modrfa.vbn),
  165. bfd_getl16 (lns.modrfa.offset)))
  166. return false;
  167. rfa = &lns.nxtrfa;
  168. }
  169. }
  170. /* Read block VBN from ABFD and store it into BLK. Return FALSE in case of error. */
  171. static bool
  172. vms_read_block (bfd *abfd, unsigned int vbn, void *blk)
  173. {
  174. file_ptr off;
  175. off = (vbn - 1) * VMS_BLOCK_SIZE;
  176. if (bfd_seek (abfd, off, SEEK_SET) != 0
  177. || bfd_bread (blk, VMS_BLOCK_SIZE, abfd) != VMS_BLOCK_SIZE)
  178. return false;
  179. return true;
  180. }
  181. /* Write the content of BLK to block VBN of ABFD. Return FALSE in case of error. */
  182. static bool
  183. vms_write_block (bfd *abfd, unsigned int vbn, void *blk)
  184. {
  185. file_ptr off;
  186. off = (vbn - 1) * VMS_BLOCK_SIZE;
  187. if (bfd_seek (abfd, off, SEEK_SET) != 0
  188. || bfd_bwrite (blk, VMS_BLOCK_SIZE, abfd) != VMS_BLOCK_SIZE)
  189. return false;
  190. return true;
  191. }
  192. /* Read index block VBN and put the entry in **IDX (which is updated).
  193. If the entry is indirect, recurse. */
  194. static bool
  195. vms_traverse_index (bfd *abfd, unsigned int vbn, struct carsym_mem *cs,
  196. unsigned int recur_count)
  197. {
  198. struct vms_indexdef indexdef;
  199. file_ptr off;
  200. unsigned char *p;
  201. unsigned char *endp;
  202. unsigned int n;
  203. if (recur_count == 100)
  204. {
  205. bfd_set_error (bfd_error_bad_value);
  206. return false;
  207. }
  208. /* Read the index block. */
  209. BFD_ASSERT (sizeof (indexdef) == VMS_BLOCK_SIZE);
  210. if (!vms_read_block (abfd, vbn, &indexdef))
  211. return false;
  212. /* Traverse it. */
  213. p = &indexdef.keys[0];
  214. n = bfd_getl16 (indexdef.used);
  215. if (n > sizeof (indexdef.keys))
  216. return false;
  217. endp = p + n;
  218. while (p < endp)
  219. {
  220. unsigned int idx_vbn;
  221. unsigned int idx_off;
  222. unsigned int keylen;
  223. unsigned char *keyname;
  224. unsigned int flags;
  225. /* Extract key length. */
  226. if (bfd_libdata (abfd)->ver == LBR_MAJORID
  227. && offsetof (struct vms_idx, keyname) <= (size_t) (endp - p))
  228. {
  229. struct vms_idx *ridx = (struct vms_idx *)p;
  230. idx_vbn = bfd_getl32 (ridx->rfa.vbn);
  231. idx_off = bfd_getl16 (ridx->rfa.offset);
  232. keylen = ridx->keylen;
  233. flags = 0;
  234. keyname = ridx->keyname;
  235. }
  236. else if (bfd_libdata (abfd)->ver == LBR_ELFMAJORID
  237. && offsetof (struct vms_elfidx, keyname) <= (size_t) (endp - p))
  238. {
  239. struct vms_elfidx *ridx = (struct vms_elfidx *)p;
  240. idx_vbn = bfd_getl32 (ridx->rfa.vbn);
  241. idx_off = bfd_getl16 (ridx->rfa.offset);
  242. keylen = bfd_getl16 (ridx->keylen);
  243. flags = ridx->flags;
  244. keyname = ridx->keyname;
  245. }
  246. else
  247. return false;
  248. /* Illegal value. */
  249. if (idx_vbn == 0)
  250. return false;
  251. /* Point to the next index entry. */
  252. p = keyname + keylen;
  253. if (p > endp)
  254. return false;
  255. if (idx_off == RFADEF__C_INDEX)
  256. {
  257. /* Indirect entry. Recurse. */
  258. if (!vms_traverse_index (abfd, idx_vbn, cs, recur_count + 1))
  259. return false;
  260. }
  261. else
  262. {
  263. /* Add a new entry. */
  264. char *name;
  265. if (flags & ELFIDX__SYMESC)
  266. {
  267. /* Extended key name. */
  268. unsigned int noff = 0;
  269. unsigned int koff;
  270. unsigned int kvbn;
  271. struct vms_kbn *kbn;
  272. unsigned char kblk[VMS_BLOCK_SIZE];
  273. /* Sanity check. */
  274. if (keylen != sizeof (struct vms_kbn))
  275. return false;
  276. kbn = (struct vms_kbn *)keyname;
  277. keylen = bfd_getl16 (kbn->keylen);
  278. name = bfd_alloc (abfd, keylen + 1);
  279. if (name == NULL)
  280. return false;
  281. kvbn = bfd_getl32 (kbn->rfa.vbn);
  282. koff = bfd_getl16 (kbn->rfa.offset);
  283. /* Read the key, chunk by chunk. */
  284. do
  285. {
  286. unsigned int klen;
  287. if (!vms_read_block (abfd, kvbn, kblk))
  288. return false;
  289. if (koff > sizeof (kblk) - sizeof (struct vms_kbn))
  290. return false;
  291. kbn = (struct vms_kbn *)(kblk + koff);
  292. klen = bfd_getl16 (kbn->keylen);
  293. if (klen > sizeof (kblk) - sizeof (struct vms_kbn) - koff)
  294. return false;
  295. kvbn = bfd_getl32 (kbn->rfa.vbn);
  296. koff = bfd_getl16 (kbn->rfa.offset);
  297. if (noff + klen > keylen)
  298. return false;
  299. memcpy (name + noff, kbn + 1, klen);
  300. noff += klen;
  301. }
  302. while (kvbn != 0);
  303. /* Sanity check. */
  304. if (noff != keylen)
  305. return false;
  306. }
  307. else
  308. {
  309. /* Usual key name. */
  310. name = bfd_alloc (abfd, keylen + 1);
  311. if (name == NULL)
  312. return false;
  313. memcpy (name, keyname, keylen);
  314. }
  315. name[keylen] = 0;
  316. if (flags & ELFIDX__LISTRFA)
  317. {
  318. struct vms_lhs lhs;
  319. /* Read the LHS. */
  320. off = (idx_vbn - 1) * VMS_BLOCK_SIZE + idx_off;
  321. if (bfd_seek (abfd, off, SEEK_SET) != 0
  322. || bfd_bread (&lhs, sizeof (lhs), abfd) != sizeof (lhs))
  323. return false;
  324. /* These extra entries may cause reallocation of CS. */
  325. if (!vms_add_indexes_from_list (abfd, cs, name, &lhs.ng_g_rfa))
  326. return false;
  327. if (!vms_add_indexes_from_list (abfd, cs, name, &lhs.ng_wk_rfa))
  328. return false;
  329. if (!vms_add_indexes_from_list (abfd, cs, name, &lhs.g_g_rfa))
  330. return false;
  331. if (!vms_add_indexes_from_list (abfd, cs, name, &lhs.g_wk_rfa))
  332. return false;
  333. }
  334. else
  335. {
  336. if (!vms_add_index (cs, name, idx_vbn, idx_off))
  337. return false;
  338. }
  339. }
  340. }
  341. return true;
  342. }
  343. /* Read index #IDX, which must have NBREL entries. */
  344. static struct carsym *
  345. vms_lib_read_index (bfd *abfd, int idx, unsigned int *nbrel)
  346. {
  347. struct vms_idd idd;
  348. unsigned int flags;
  349. unsigned int vbn;
  350. ufile_ptr filesize;
  351. size_t amt;
  352. struct carsym *csbuf;
  353. struct carsym_mem csm;
  354. /* Read index desription. */
  355. if (bfd_seek (abfd, LHD_IDXDESC + idx * IDD_LENGTH, SEEK_SET) != 0
  356. || bfd_bread (&idd, sizeof (idd), abfd) != sizeof (idd))
  357. return NULL;
  358. /* Sanity checks. */
  359. flags = bfd_getl16 (idd.flags);
  360. if (!(flags & IDD__FLAGS_ASCII)
  361. || !(flags & IDD__FLAGS_VARLENIDX))
  362. return NULL;
  363. filesize = bfd_get_file_size (abfd);
  364. csm.nbr = 0;
  365. csm.max = *nbrel;
  366. csm.limit = -1u;
  367. csm.realloced = false;
  368. if (filesize != 0)
  369. {
  370. /* Put an upper bound based on a file full of single char keys.
  371. This is to prevent fuzzed binary silliness. It is easily
  372. possible to set up loops over file blocks that add syms
  373. without end. */
  374. if (filesize / (sizeof (struct vms_rfa) + 2) <= -1u)
  375. csm.limit = filesize / (sizeof (struct vms_rfa) + 2);
  376. }
  377. if (csm.max > csm.limit)
  378. csm.max = csm.limit;
  379. if (_bfd_mul_overflow (csm.max, sizeof (struct carsym), &amt))
  380. return NULL;
  381. csm.idx = csbuf = bfd_alloc (abfd, amt);
  382. if (csm.idx == NULL)
  383. return NULL;
  384. /* Note: if the index is empty, there is no block to traverse. */
  385. vbn = bfd_getl32 (idd.vbn);
  386. if (vbn != 0 && !vms_traverse_index (abfd, vbn, &csm, 0))
  387. {
  388. if (csm.realloced)
  389. free (csm.idx);
  390. /* Note: in case of error, we can free what was allocated on the
  391. BFD's objalloc. */
  392. bfd_release (abfd, csbuf);
  393. return NULL;
  394. }
  395. if (csm.realloced)
  396. {
  397. /* There are more entries than the first estimate. Allocate on
  398. the BFD's objalloc. */
  399. csbuf = bfd_alloc (abfd, csm.nbr * sizeof (struct carsym));
  400. if (csbuf == NULL)
  401. return NULL;
  402. memcpy (csbuf, csm.idx, csm.nbr * sizeof (struct carsym));
  403. free (csm.idx);
  404. csm.idx = csbuf;
  405. }
  406. *nbrel = csm.nbr;
  407. return csm.idx;
  408. }
  409. /* Standard function. */
  410. static bfd_cleanup
  411. _bfd_vms_lib_archive_p (bfd *abfd, enum vms_lib_kind kind)
  412. {
  413. struct vms_lhd lhd;
  414. unsigned int sanity;
  415. unsigned int majorid;
  416. struct lib_tdata *tdata_hold;
  417. struct lib_tdata *tdata;
  418. unsigned int dcxvbn;
  419. unsigned int nbr_ent;
  420. /* Read header. */
  421. if (bfd_bread (&lhd, sizeof (lhd), abfd) != sizeof (lhd))
  422. {
  423. if (bfd_get_error () != bfd_error_system_call)
  424. bfd_set_error (bfd_error_wrong_format);
  425. return NULL;
  426. }
  427. /* Check sanity (= magic) number. */
  428. sanity = bfd_getl32 (lhd.sanity);
  429. if (!(sanity == LHD_SANEID3
  430. || sanity == LHD_SANEID6
  431. || sanity == LHD_SANEID_DCX))
  432. {
  433. bfd_set_error (bfd_error_wrong_format);
  434. return NULL;
  435. }
  436. majorid = bfd_getl32 (lhd.majorid);
  437. /* Check archive kind. */
  438. switch (kind)
  439. {
  440. case vms_lib_alpha:
  441. if ((lhd.type != LBR__C_TYP_EOBJ && lhd.type != LBR__C_TYP_ESHSTB)
  442. || majorid != LBR_MAJORID
  443. || lhd.nindex != 2)
  444. {
  445. bfd_set_error (bfd_error_wrong_format);
  446. return NULL;
  447. }
  448. break;
  449. case vms_lib_ia64:
  450. if ((lhd.type != LBR__C_TYP_IOBJ && lhd.type != LBR__C_TYP_ISHSTB)
  451. || majorid != LBR_ELFMAJORID
  452. || lhd.nindex != 2)
  453. {
  454. bfd_set_error (bfd_error_wrong_format);
  455. return NULL;
  456. }
  457. break;
  458. case vms_lib_txt:
  459. if ((lhd.type != LBR__C_TYP_TXT
  460. && lhd.type != LBR__C_TYP_MLB
  461. && lhd.type != LBR__C_TYP_HLP)
  462. || majorid != LBR_MAJORID
  463. || lhd.nindex != 1)
  464. {
  465. bfd_set_error (bfd_error_wrong_format);
  466. return NULL;
  467. }
  468. break;
  469. default:
  470. abort ();
  471. }
  472. /* Allocate and initialize private data. */
  473. tdata_hold = bfd_libdata (abfd);
  474. tdata = (struct lib_tdata *) bfd_zalloc (abfd, sizeof (struct lib_tdata));
  475. if (tdata == NULL)
  476. return NULL;
  477. abfd->tdata.any = (void *)tdata;
  478. tdata->ver = majorid;
  479. tdata->mhd_size = MHD__C_USRDAT + lhd.mhdusz;
  480. tdata->type = lhd.type;
  481. tdata->kind = kind;
  482. tdata->credat_lo = bfd_getl32 (lhd.credat + 0);
  483. tdata->credat_hi = bfd_getl32 (lhd.credat + 4);
  484. /* Read indexes. */
  485. tdata->nbr_modules = bfd_getl32 (lhd.modcnt);
  486. tdata->artdata.symdef_count = bfd_getl32 (lhd.idxcnt) - tdata->nbr_modules;
  487. nbr_ent = tdata->nbr_modules;
  488. tdata->modules = vms_lib_read_index (abfd, 0, &nbr_ent);
  489. if (tdata->modules == NULL || nbr_ent != tdata->nbr_modules)
  490. goto err;
  491. if (lhd.nindex == 2)
  492. {
  493. nbr_ent = tdata->artdata.symdef_count;
  494. tdata->artdata.symdefs = vms_lib_read_index (abfd, 1, &nbr_ent);
  495. if (tdata->artdata.symdefs == NULL)
  496. goto err;
  497. /* Only IA64 archives may have more entries in the index that what
  498. was declared. */
  499. if (nbr_ent != tdata->artdata.symdef_count
  500. && kind != vms_lib_ia64)
  501. goto err;
  502. tdata->artdata.symdef_count = nbr_ent;
  503. }
  504. tdata->cache = bfd_zalloc (abfd, sizeof (bfd *) * tdata->nbr_modules);
  505. if (tdata->cache == NULL)
  506. goto err;
  507. /* Read DCX submaps. */
  508. dcxvbn = bfd_getl32 (lhd.dcxmapvbn);
  509. if (dcxvbn != 0)
  510. {
  511. unsigned char buf_reclen[4];
  512. unsigned int reclen;
  513. unsigned char *buf;
  514. struct vms_dcxmap *map;
  515. unsigned int sbm_off;
  516. unsigned int i;
  517. if (bfd_seek (abfd, (dcxvbn - 1) * VMS_BLOCK_SIZE, SEEK_SET) != 0
  518. || bfd_bread (buf_reclen, sizeof (buf_reclen), abfd)
  519. != sizeof (buf_reclen))
  520. goto err;
  521. reclen = bfd_getl32 (buf_reclen);
  522. if (reclen < sizeof (struct vms_dcxmap))
  523. goto err;
  524. buf = _bfd_malloc_and_read (abfd, reclen, reclen);
  525. if (buf == NULL)
  526. goto err;
  527. map = (struct vms_dcxmap *)buf;
  528. tdata->nbr_dcxsbm = bfd_getl16 (map->nsubs);
  529. sbm_off = bfd_getl16 (map->sub0);
  530. tdata->dcxsbm = (struct dcxsbm_desc *)bfd_alloc
  531. (abfd, tdata->nbr_dcxsbm * sizeof (struct dcxsbm_desc));
  532. for (i = 0; i < tdata->nbr_dcxsbm; i++)
  533. {
  534. struct vms_dcxsbm *sbm;
  535. struct dcxsbm_desc *sbmdesc = &tdata->dcxsbm[i];
  536. unsigned int sbm_len;
  537. unsigned int sbm_sz;
  538. unsigned int off;
  539. unsigned char *buf1;
  540. unsigned int l, j;
  541. if (sbm_off > reclen
  542. || reclen - sbm_off < sizeof (struct vms_dcxsbm))
  543. {
  544. err_free_buf:
  545. free (buf);
  546. goto err;
  547. }
  548. sbm = (struct vms_dcxsbm *) (buf + sbm_off);
  549. sbm_sz = bfd_getl16 (sbm->size);
  550. sbm_off += sbm_sz;
  551. if (sbm_off > reclen)
  552. goto err_free_buf;
  553. sbmdesc->min_char = sbm->min_char;
  554. BFD_ASSERT (sbmdesc->min_char == 0);
  555. sbmdesc->max_char = sbm->max_char;
  556. sbm_len = sbmdesc->max_char - sbmdesc->min_char + 1;
  557. l = (2 * sbm_len + 7) / 8;
  558. if (sbm_sz < sizeof (struct vms_dcxsbm) + l + sbm_len
  559. || (tdata->nbr_dcxsbm > 1
  560. && sbm_sz < sizeof (struct vms_dcxsbm) + l + 3 * sbm_len))
  561. goto err_free_buf;
  562. sbmdesc->flags = (unsigned char *)bfd_alloc (abfd, l);
  563. off = bfd_getl16 (sbm->flags);
  564. if (off > sbm_sz
  565. || sbm_sz - off < l)
  566. goto err_free_buf;
  567. memcpy (sbmdesc->flags, (bfd_byte *) sbm + off, l);
  568. sbmdesc->nodes = (unsigned char *)bfd_alloc (abfd, 2 * sbm_len);
  569. off = bfd_getl16 (sbm->nodes);
  570. if (off > sbm_sz
  571. || sbm_sz - off < 2 * sbm_len)
  572. goto err_free_buf;
  573. memcpy (sbmdesc->nodes, (bfd_byte *) sbm + off, 2 * sbm_len);
  574. off = bfd_getl16 (sbm->next);
  575. if (off != 0)
  576. {
  577. if (off > sbm_sz
  578. || sbm_sz - off < 2 * sbm_len)
  579. goto err_free_buf;
  580. /* Read the 'next' array. */
  581. sbmdesc->next = (unsigned short *) bfd_alloc (abfd, 2 * sbm_len);
  582. buf1 = (bfd_byte *) sbm + off;
  583. for (j = 0; j < sbm_len; j++)
  584. sbmdesc->next[j] = bfd_getl16 (buf1 + j * 2);
  585. }
  586. else
  587. {
  588. /* There is no next array if there is only one submap. */
  589. BFD_ASSERT (tdata->nbr_dcxsbm == 1);
  590. sbmdesc->next = NULL;
  591. }
  592. }
  593. free (buf);
  594. }
  595. else
  596. {
  597. tdata->nbr_dcxsbm = 0;
  598. }
  599. /* The map is always present. Also mark shared image library. */
  600. abfd->has_armap = true;
  601. if (tdata->type == LBR__C_TYP_ESHSTB || tdata->type == LBR__C_TYP_ISHSTB)
  602. abfd->is_thin_archive = true;
  603. return _bfd_no_cleanup;
  604. err:
  605. bfd_release (abfd, tdata);
  606. abfd->tdata.any = (void *)tdata_hold;
  607. return NULL;
  608. }
  609. /* Standard function for alpha libraries. */
  610. bfd_cleanup
  611. _bfd_vms_lib_alpha_archive_p (bfd *abfd)
  612. {
  613. return _bfd_vms_lib_archive_p (abfd, vms_lib_alpha);
  614. }
  615. /* Standard function for ia64 libraries. */
  616. bfd_cleanup
  617. _bfd_vms_lib_ia64_archive_p (bfd *abfd)
  618. {
  619. return _bfd_vms_lib_archive_p (abfd, vms_lib_ia64);
  620. }
  621. /* Standard function for text libraries. */
  622. static bfd_cleanup
  623. _bfd_vms_lib_txt_archive_p (bfd *abfd)
  624. {
  625. return _bfd_vms_lib_archive_p (abfd, vms_lib_txt);
  626. }
  627. /* Standard bfd function. */
  628. static bool
  629. _bfd_vms_lib_mkarchive (bfd *abfd, enum vms_lib_kind kind)
  630. {
  631. struct lib_tdata *tdata;
  632. tdata = (struct lib_tdata *) bfd_zalloc (abfd, sizeof (struct lib_tdata));
  633. if (tdata == NULL)
  634. return false;
  635. abfd->tdata.any = (void *)tdata;
  636. vms_get_time (&tdata->credat_hi, &tdata->credat_lo);
  637. tdata->kind = kind;
  638. switch (kind)
  639. {
  640. case vms_lib_alpha:
  641. tdata->ver = LBR_MAJORID;
  642. tdata->mhd_size = offsetof (struct vms_mhd, pad1);
  643. tdata->type = LBR__C_TYP_EOBJ;
  644. break;
  645. case vms_lib_ia64:
  646. tdata->ver = LBR_ELFMAJORID;
  647. tdata->mhd_size = sizeof (struct vms_mhd);
  648. tdata->type = LBR__C_TYP_IOBJ;
  649. break;
  650. default:
  651. abort ();
  652. }
  653. tdata->nbr_modules = 0;
  654. tdata->artdata.symdef_count = 0;
  655. tdata->modules = NULL;
  656. tdata->artdata.symdefs = NULL;
  657. tdata->cache = NULL;
  658. return true;
  659. }
  660. bool
  661. _bfd_vms_lib_alpha_mkarchive (bfd *abfd)
  662. {
  663. return _bfd_vms_lib_mkarchive (abfd, vms_lib_alpha);
  664. }
  665. bool
  666. _bfd_vms_lib_ia64_mkarchive (bfd *abfd)
  667. {
  668. return _bfd_vms_lib_mkarchive (abfd, vms_lib_ia64);
  669. }
  670. /* Find NAME in the symbol index. Return the index. */
  671. symindex
  672. _bfd_vms_lib_find_symbol (bfd *abfd, const char *name)
  673. {
  674. struct lib_tdata *tdata = bfd_libdata (abfd);
  675. carsym *syms = tdata->artdata.symdefs;
  676. int lo, hi;
  677. /* Open-coded binary search for speed. */
  678. lo = 0;
  679. hi = tdata->artdata.symdef_count - 1;
  680. while (lo <= hi)
  681. {
  682. int mid = lo + (hi - lo) / 2;
  683. int diff;
  684. diff = (char)(name[0] - syms[mid].name[0]);
  685. if (diff == 0)
  686. diff = strcmp (name, syms[mid].name);
  687. if (diff == 0)
  688. return mid;
  689. else if (diff < 0)
  690. hi = mid - 1;
  691. else
  692. lo = mid + 1;
  693. }
  694. return BFD_NO_MORE_SYMBOLS;
  695. }
  696. /* IO vector for archive member. Need that because members are not linearly
  697. stored in archives. */
  698. struct vms_lib_iovec
  699. {
  700. /* Current offset. */
  701. ufile_ptr where;
  702. /* Length of the module, when known. */
  703. ufile_ptr file_len;
  704. /* Current position in the record from bfd_bread point of view (ie, after
  705. decompression). 0 means that no data byte have been read, -2 and -1
  706. are reserved for the length word. */
  707. int rec_pos;
  708. #define REC_POS_NL -4
  709. #define REC_POS_PAD -3
  710. #define REC_POS_LEN0 -2
  711. #define REC_POS_LEN1 -1
  712. /* Record length. */
  713. unsigned short rec_len;
  714. /* Number of bytes to read in the current record. */
  715. unsigned short rec_rem;
  716. /* Offset of the next block. */
  717. file_ptr next_block;
  718. /* Current *data* offset in the data block. */
  719. unsigned short blk_off;
  720. /* Offset of the first block. Extracted from the index. */
  721. file_ptr first_block;
  722. /* Initial next_block. Extracted when the MHD is read. */
  723. file_ptr init_next_block;
  724. /* Initial blk_off, once the MHD is read. */
  725. unsigned short init_blk_off;
  726. /* Used to store any 3 byte record, which could be the EOF pattern. */
  727. unsigned char pattern[4];
  728. /* DCX. */
  729. struct dcxsbm_desc *dcxsbms;
  730. /* Current submap. */
  731. struct dcxsbm_desc *dcx_sbm;
  732. /* Current offset in the submap. */
  733. unsigned int dcx_offset;
  734. int dcx_pos;
  735. /* Compressed buffer. */
  736. unsigned char *dcx_buf;
  737. /* Size of the buffer. Used to resize. */
  738. unsigned int dcx_max;
  739. /* Number of valid bytes in the buffer. */
  740. unsigned int dcx_rlen;
  741. };
  742. /* Return the current position. */
  743. static file_ptr
  744. vms_lib_btell (struct bfd *abfd)
  745. {
  746. struct vms_lib_iovec *vec = (struct vms_lib_iovec *) abfd->iostream;
  747. return vec->where;
  748. }
  749. /* Read the header of the next data block if all bytes of the current block
  750. have been read. */
  751. static bool
  752. vms_lib_read_block (struct bfd *abfd)
  753. {
  754. struct vms_lib_iovec *vec = (struct vms_lib_iovec *) abfd->iostream;
  755. if (vec->blk_off == DATA__LENGTH)
  756. {
  757. unsigned char hdr[DATA__DATA];
  758. /* Read next block. */
  759. if (bfd_seek (abfd->my_archive, vec->next_block, SEEK_SET) != 0)
  760. return false;
  761. if (bfd_bread (hdr, sizeof (hdr), abfd->my_archive) != sizeof (hdr))
  762. return false;
  763. vec->next_block = (bfd_getl32 (hdr + 2) - 1) * VMS_BLOCK_SIZE;
  764. vec->blk_off = sizeof (hdr);
  765. }
  766. return true;
  767. }
  768. /* Read NBYTES from ABFD into BUF if not NULL. If BUF is NULL, bytes are
  769. not stored. Read linearly from the library, but handle blocks. This
  770. function does not handle records nor EOF. */
  771. static file_ptr
  772. vms_lib_bread_raw (struct bfd *abfd, unsigned char *buf, file_ptr nbytes)
  773. {
  774. struct vms_lib_iovec *vec = (struct vms_lib_iovec *) abfd->iostream;
  775. file_ptr res;
  776. res = 0;
  777. while (nbytes > 0)
  778. {
  779. unsigned int l;
  780. /* Be sure the current data block is read. */
  781. if (!vms_lib_read_block (abfd))
  782. return -1;
  783. /* Do not read past the data block, do not read more than requested. */
  784. l = DATA__LENGTH - vec->blk_off;
  785. if (l > nbytes)
  786. l = nbytes;
  787. if (l == 0)
  788. return 0;
  789. if (buf != NULL)
  790. {
  791. /* Really read into BUF. */
  792. if (bfd_bread (buf, l, abfd->my_archive) != l)
  793. return -1;
  794. }
  795. else
  796. {
  797. /* Make as if we are reading. */
  798. if (bfd_seek (abfd->my_archive, l, SEEK_CUR) != 0)
  799. return -1;
  800. }
  801. if (buf != NULL)
  802. buf += l;
  803. vec->blk_off += l;
  804. nbytes -= l;
  805. res += l;
  806. }
  807. return res;
  808. }
  809. /* Decompress NBYTES from VEC. Store the bytes into BUF if not NULL. */
  810. static file_ptr
  811. vms_lib_dcx (struct vms_lib_iovec *vec, unsigned char *buf, file_ptr nbytes)
  812. {
  813. struct dcxsbm_desc *sbm;
  814. unsigned int i;
  815. unsigned int offset;
  816. unsigned int j;
  817. file_ptr res = 0;
  818. /* The loop below expect to deliver at least one byte. */
  819. if (nbytes == 0)
  820. return 0;
  821. /* Get the current state. */
  822. sbm = vec->dcx_sbm;
  823. offset = vec->dcx_offset;
  824. j = vec->dcx_pos & 7;
  825. for (i = vec->dcx_pos >> 3; i < vec->dcx_rlen; i++)
  826. {
  827. unsigned char b = vec->dcx_buf[i];
  828. for (; j < 8; j++)
  829. {
  830. if (b & (1 << j))
  831. offset++;
  832. if (!(sbm->flags[offset >> 3] & (1 << (offset & 7))))
  833. {
  834. unsigned int n_offset = sbm->nodes[offset];
  835. if (n_offset == 0)
  836. {
  837. /* End of buffer. Stay where we are. */
  838. vec->dcx_pos = (i << 3) + j;
  839. if (b & (1 << j))
  840. offset--;
  841. vec->dcx_offset = offset;
  842. vec->dcx_sbm = sbm;
  843. return res;
  844. }
  845. offset = 2 * n_offset;
  846. }
  847. else
  848. {
  849. unsigned char v = sbm->nodes[offset];
  850. if (sbm->next != NULL)
  851. sbm = vec->dcxsbms + sbm->next[v];
  852. offset = 0;
  853. res++;
  854. if (buf)
  855. {
  856. *buf++ = v;
  857. nbytes--;
  858. if (nbytes == 0)
  859. {
  860. vec->dcx_pos = (i << 3) + j + 1;
  861. vec->dcx_offset = offset;
  862. vec->dcx_sbm = sbm;
  863. return res;
  864. }
  865. }
  866. }
  867. }
  868. j = 0;
  869. }
  870. return -1;
  871. }
  872. /* Standard IOVEC function. */
  873. static file_ptr
  874. vms_lib_bread (struct bfd *abfd, void *vbuf, file_ptr nbytes)
  875. {
  876. struct vms_lib_iovec *vec = (struct vms_lib_iovec *) abfd->iostream;
  877. file_ptr res;
  878. file_ptr chunk;
  879. unsigned char *buf = (unsigned char *)vbuf;
  880. /* Do not read past the end. */
  881. if (vec->where >= vec->file_len)
  882. return 0;
  883. res = 0;
  884. while (nbytes > 0)
  885. {
  886. if (vec->rec_rem == 0)
  887. {
  888. unsigned char blen[2];
  889. /* Read record length. */
  890. if (vms_lib_bread_raw (abfd, blen, sizeof (blen)) != sizeof (blen))
  891. return -1;
  892. vec->rec_len = bfd_getl16 (blen);
  893. if (bfd_libdata (abfd->my_archive)->kind == vms_lib_txt)
  894. {
  895. /* Discard record size and align byte. */
  896. vec->rec_pos = 0;
  897. vec->rec_rem = vec->rec_len;
  898. }
  899. else
  900. {
  901. /* Prepend record size. */
  902. vec->rec_pos = REC_POS_LEN0;
  903. vec->rec_rem = (vec->rec_len + 1) & ~1; /* With align byte. */
  904. }
  905. if (vec->rec_len == 3)
  906. {
  907. /* Possibly end of file. Check the pattern. */
  908. if (vms_lib_bread_raw (abfd, vec->pattern, 4) != 4)
  909. return -1;
  910. if (!memcmp (vec->pattern, eotdesc + 2, 3))
  911. {
  912. /* This is really an EOF. */
  913. vec->where += res;
  914. vec->file_len = vec->where;
  915. return res;
  916. }
  917. }
  918. if (vec->dcxsbms != NULL)
  919. {
  920. /* This is a compressed member. */
  921. unsigned int len;
  922. file_ptr elen;
  923. /* Be sure there is enough room for the expansion. */
  924. len = (vec->rec_len + 1) & ~1;
  925. if (len > vec->dcx_max)
  926. {
  927. while (len > vec->dcx_max)
  928. vec->dcx_max *= 2;
  929. vec->dcx_buf = bfd_alloc (abfd, vec->dcx_max);
  930. if (vec->dcx_buf == NULL)
  931. return -1;
  932. }
  933. /* Read the compressed record. */
  934. vec->dcx_rlen = len;
  935. if (vec->rec_len == 3)
  936. {
  937. /* Already read. */
  938. memcpy (vec->dcx_buf, vec->pattern, 3);
  939. }
  940. else
  941. {
  942. elen = vms_lib_bread_raw (abfd, vec->dcx_buf, len);
  943. if (elen != len)
  944. return -1;
  945. }
  946. /* Dummy expansion to get the expanded length. */
  947. vec->dcx_offset = 0;
  948. vec->dcx_sbm = vec->dcxsbms;
  949. vec->dcx_pos = 0;
  950. elen = vms_lib_dcx (vec, NULL, 0x10000);
  951. if (elen < 0)
  952. return -1;
  953. vec->rec_len = elen;
  954. vec->rec_rem = elen;
  955. /* Reset the state. */
  956. vec->dcx_offset = 0;
  957. vec->dcx_sbm = vec->dcxsbms;
  958. vec->dcx_pos = 0;
  959. }
  960. }
  961. if (vec->rec_pos < 0)
  962. {
  963. unsigned char c;
  964. switch (vec->rec_pos)
  965. {
  966. case REC_POS_LEN0:
  967. c = vec->rec_len & 0xff;
  968. vec->rec_pos = REC_POS_LEN1;
  969. break;
  970. case REC_POS_LEN1:
  971. c = (vec->rec_len >> 8) & 0xff;
  972. vec->rec_pos = 0;
  973. break;
  974. case REC_POS_PAD:
  975. c = 0;
  976. vec->rec_rem = 0;
  977. break;
  978. case REC_POS_NL:
  979. c = '\n';
  980. vec->rec_rem = 0;
  981. break;
  982. default:
  983. abort ();
  984. }
  985. if (buf != NULL)
  986. {
  987. *buf = c;
  988. buf++;
  989. }
  990. nbytes--;
  991. res++;
  992. continue;
  993. }
  994. if (nbytes > vec->rec_rem)
  995. chunk = vec->rec_rem;
  996. else
  997. chunk = nbytes;
  998. if (vec->dcxsbms != NULL)
  999. {
  1000. /* Optimize the stat() case: no need to decompress again as we
  1001. know the length. */
  1002. if (!(buf == NULL && chunk == vec->rec_rem))
  1003. chunk = vms_lib_dcx (vec, buf, chunk);
  1004. }
  1005. else
  1006. {
  1007. if (vec->rec_len == 3)
  1008. {
  1009. if (buf != NULL)
  1010. memcpy (buf, vec->pattern + vec->rec_pos, chunk);
  1011. }
  1012. else
  1013. chunk = vms_lib_bread_raw (abfd, buf, chunk);
  1014. }
  1015. if (chunk < 0)
  1016. return -1;
  1017. res += chunk;
  1018. if (buf != NULL)
  1019. buf += chunk;
  1020. nbytes -= chunk;
  1021. vec->rec_pos += chunk;
  1022. vec->rec_rem -= chunk;
  1023. if (vec->rec_rem == 0)
  1024. {
  1025. /* End of record reached. */
  1026. if (bfd_libdata (abfd->my_archive)->kind == vms_lib_txt)
  1027. {
  1028. if ((vec->rec_len & 1) == 1
  1029. && vec->rec_len != 3
  1030. && vec->dcxsbms == NULL)
  1031. {
  1032. /* Eat the pad byte. */
  1033. unsigned char pad;
  1034. if (vms_lib_bread_raw (abfd, &pad, 1) != 1)
  1035. return -1;
  1036. }
  1037. vec->rec_pos = REC_POS_NL;
  1038. vec->rec_rem = 1;
  1039. }
  1040. else
  1041. {
  1042. if ((vec->rec_len & 1) == 1 && vec->dcxsbms != NULL)
  1043. {
  1044. vec->rec_pos = REC_POS_PAD;
  1045. vec->rec_rem = 1;
  1046. }
  1047. }
  1048. }
  1049. }
  1050. vec->where += res;
  1051. return res;
  1052. }
  1053. /* Standard function, but we currently only handle the rewind case. */
  1054. static int
  1055. vms_lib_bseek (struct bfd *abfd, file_ptr offset, int whence)
  1056. {
  1057. struct vms_lib_iovec *vec = (struct vms_lib_iovec *) abfd->iostream;
  1058. if (whence == SEEK_SET && offset == 0)
  1059. {
  1060. vec->where = 0;
  1061. vec->rec_rem = 0;
  1062. vec->dcx_pos = -1;
  1063. vec->blk_off = vec->init_blk_off;
  1064. vec->next_block = vec->init_next_block;
  1065. if (bfd_seek (abfd->my_archive, vec->first_block, SEEK_SET) != 0)
  1066. return -1;
  1067. }
  1068. else
  1069. abort ();
  1070. return 0;
  1071. }
  1072. static file_ptr
  1073. vms_lib_bwrite (struct bfd *abfd ATTRIBUTE_UNUSED,
  1074. const void *where ATTRIBUTE_UNUSED,
  1075. file_ptr nbytes ATTRIBUTE_UNUSED)
  1076. {
  1077. return -1;
  1078. }
  1079. static int
  1080. vms_lib_bclose (struct bfd *abfd)
  1081. {
  1082. abfd->iostream = NULL;
  1083. return 0;
  1084. }
  1085. static int
  1086. vms_lib_bflush (struct bfd *abfd ATTRIBUTE_UNUSED)
  1087. {
  1088. return 0;
  1089. }
  1090. static int
  1091. vms_lib_bstat (struct bfd *abfd ATTRIBUTE_UNUSED,
  1092. struct stat *sb ATTRIBUTE_UNUSED)
  1093. {
  1094. /* Not supported. */
  1095. return 0;
  1096. }
  1097. static void *
  1098. vms_lib_bmmap (struct bfd *abfd ATTRIBUTE_UNUSED,
  1099. void *addr ATTRIBUTE_UNUSED,
  1100. bfd_size_type len ATTRIBUTE_UNUSED,
  1101. int prot ATTRIBUTE_UNUSED,
  1102. int flags ATTRIBUTE_UNUSED,
  1103. file_ptr offset ATTRIBUTE_UNUSED,
  1104. void **map_addr ATTRIBUTE_UNUSED,
  1105. bfd_size_type *map_len ATTRIBUTE_UNUSED)
  1106. {
  1107. return (void *) -1;
  1108. }
  1109. static const struct bfd_iovec vms_lib_iovec = {
  1110. &vms_lib_bread, &vms_lib_bwrite, &vms_lib_btell, &vms_lib_bseek,
  1111. &vms_lib_bclose, &vms_lib_bflush, &vms_lib_bstat, &vms_lib_bmmap
  1112. };
  1113. /* Open a library module. FILEPOS is the position of the module header. */
  1114. static bool
  1115. vms_lib_bopen (bfd *el, file_ptr filepos)
  1116. {
  1117. struct vms_lib_iovec *vec;
  1118. unsigned char buf[256];
  1119. struct vms_mhd *mhd;
  1120. struct lib_tdata *tdata = bfd_libdata (el->my_archive);
  1121. unsigned int len;
  1122. /* Allocate and initialized the iovec. */
  1123. vec = bfd_zalloc (el, sizeof (*vec));
  1124. if (vec == NULL)
  1125. return false;
  1126. el->iostream = vec;
  1127. el->iovec = &vms_lib_iovec;
  1128. /* File length is not known. */
  1129. vec->file_len = -1;
  1130. /* Read the first data block. */
  1131. vec->next_block = filepos & ~(VMS_BLOCK_SIZE - 1);
  1132. vec->blk_off = DATA__LENGTH;
  1133. if (!vms_lib_read_block (el))
  1134. return false;
  1135. /* Prepare to read the first record. */
  1136. vec->blk_off = filepos & (VMS_BLOCK_SIZE - 1);
  1137. vec->rec_rem = 0;
  1138. if (bfd_seek (el->my_archive, filepos, SEEK_SET) != 0)
  1139. return false;
  1140. /* Read Record length + MHD + align byte. */
  1141. len = tdata->mhd_size;
  1142. if (vms_lib_bread_raw (el, buf, 2) != 2)
  1143. return false;
  1144. if (bfd_getl16 (buf) != len)
  1145. return false;
  1146. len = (len + 1) & ~1;
  1147. BFD_ASSERT (len <= sizeof (buf));
  1148. if (vms_lib_bread_raw (el, buf, len) != len)
  1149. return false;
  1150. /* Get info from mhd. */
  1151. mhd = (struct vms_mhd *)buf;
  1152. /* Check id. */
  1153. if (mhd->id != MHD__C_MHDID)
  1154. return false;
  1155. if (len >= MHD__C_MHDLEN + 1)
  1156. el->selective_search = (mhd->objstat & MHD__M_SELSRC) ? 1 : 0;
  1157. el->mtime = vms_rawtime_to_time_t (mhd->datim);
  1158. el->mtime_set = true;
  1159. /* Reinit the iovec so that seek() will point to the first record after
  1160. the mhd. */
  1161. vec->where = 0;
  1162. vec->init_blk_off = vec->blk_off;
  1163. vec->init_next_block = vec->next_block;
  1164. vec->first_block = bfd_tell (el->my_archive);
  1165. vec->dcxsbms = bfd_libdata (el->my_archive)->dcxsbm;
  1166. if (vec->dcxsbms != NULL)
  1167. {
  1168. /* Handle DCX. */
  1169. vec->dcx_max = 10 * 1024;
  1170. vec->dcx_buf = bfd_alloc (el, vec->dcx_max);
  1171. vec->dcx_pos = -1;
  1172. if (vec->dcx_buf == NULL)
  1173. return -1;
  1174. }
  1175. return true;
  1176. }
  1177. /* Get member MODIDX. Return NULL in case of error. */
  1178. static bfd *
  1179. _bfd_vms_lib_get_module (bfd *abfd, unsigned int modidx)
  1180. {
  1181. struct lib_tdata *tdata = bfd_libdata (abfd);
  1182. bfd *res;
  1183. file_ptr file_off;
  1184. const char *name;
  1185. char *newname;
  1186. size_t namelen;
  1187. /* Sanity check. */
  1188. if (modidx >= tdata->nbr_modules)
  1189. return NULL;
  1190. /* Already loaded. */
  1191. if (tdata->cache[modidx])
  1192. return tdata->cache[modidx];
  1193. /* Build it. */
  1194. file_off = tdata->modules[modidx].file_offset;
  1195. if (tdata->type != LBR__C_TYP_IOBJ)
  1196. {
  1197. res = _bfd_create_empty_archive_element_shell (abfd);
  1198. if (res == NULL)
  1199. return NULL;
  1200. /* Special reader to deal with data blocks. */
  1201. if (!vms_lib_bopen (res, file_off))
  1202. return NULL;
  1203. }
  1204. else
  1205. {
  1206. char buf[256];
  1207. struct vms_mhd *mhd;
  1208. struct areltdata *arelt;
  1209. /* Sanity check. The MHD must be big enough to contain module size. */
  1210. if (tdata->mhd_size < offsetof (struct vms_mhd, modsize) + 4)
  1211. return NULL;
  1212. /* Read the MHD now. */
  1213. if (bfd_seek (abfd, file_off, SEEK_SET) != 0)
  1214. return NULL;
  1215. if (bfd_bread (buf, tdata->mhd_size, abfd) != tdata->mhd_size)
  1216. return NULL;
  1217. mhd = (struct vms_mhd *) buf;
  1218. if (mhd->id != MHD__C_MHDID)
  1219. return NULL;
  1220. res = _bfd_create_empty_archive_element_shell (abfd);
  1221. if (res == NULL)
  1222. return NULL;
  1223. arelt = bfd_zmalloc (sizeof (*arelt));
  1224. if (arelt == NULL)
  1225. {
  1226. bfd_close (res);
  1227. return NULL;
  1228. }
  1229. res->arelt_data = arelt;
  1230. /* Get info from mhd. */
  1231. if (tdata->mhd_size >= offsetof (struct vms_mhd, objstat) + 1)
  1232. res->selective_search = (mhd->objstat & MHD__M_SELSRC) ? 1 : 0;
  1233. res->mtime = vms_rawtime_to_time_t (mhd->datim);
  1234. res->mtime_set = true;
  1235. arelt->parsed_size = bfd_getl32 (mhd->modsize);
  1236. /* No need for a special reader as members are stored linearly.
  1237. Just skip the MHD. */
  1238. res->origin = file_off + tdata->mhd_size;
  1239. }
  1240. /* Set filename. */
  1241. name = tdata->modules[modidx].name;
  1242. namelen = strlen (name);
  1243. newname = bfd_malloc (namelen + 4 + 1);
  1244. if (newname == NULL)
  1245. {
  1246. bfd_close (res);
  1247. return NULL;
  1248. }
  1249. strcpy (newname, name);
  1250. switch (tdata->type)
  1251. {
  1252. case LBR__C_TYP_IOBJ:
  1253. case LBR__C_TYP_EOBJ:
  1254. /* For object archives, append .obj to mimic standard behaviour. */
  1255. strcpy (newname + namelen, ".obj");
  1256. break;
  1257. default:
  1258. break;
  1259. }
  1260. bfd_set_filename (res, newname);
  1261. free (newname);
  1262. if (bfd_get_filename (res) == NULL)
  1263. {
  1264. bfd_close (res);
  1265. return NULL;
  1266. }
  1267. tdata->cache[modidx] = res;
  1268. return res;
  1269. }
  1270. /* Standard function: get member at IDX. */
  1271. bfd *
  1272. _bfd_vms_lib_get_elt_at_index (bfd *abfd, symindex symidx)
  1273. {
  1274. struct lib_tdata *tdata = bfd_libdata (abfd);
  1275. file_ptr file_off;
  1276. unsigned int modidx;
  1277. /* Check symidx. */
  1278. if (symidx > tdata->artdata.symdef_count)
  1279. return NULL;
  1280. file_off = tdata->artdata.symdefs[symidx].file_offset;
  1281. /* Linear-scan. */
  1282. for (modidx = 0; modidx < tdata->nbr_modules; modidx++)
  1283. {
  1284. if (tdata->modules[modidx].file_offset == file_off)
  1285. break;
  1286. }
  1287. if (modidx >= tdata->nbr_modules)
  1288. return NULL;
  1289. return _bfd_vms_lib_get_module (abfd, modidx);
  1290. }
  1291. /* Elements of an imagelib are stubs. You can get the real image with this
  1292. function. */
  1293. bfd *
  1294. _bfd_vms_lib_get_imagelib_file (bfd *el)
  1295. {
  1296. bfd *archive = el->my_archive;
  1297. const char *modname = bfd_get_filename (el);
  1298. int modlen = strlen (modname);
  1299. char *filename;
  1300. int j;
  1301. bfd *res;
  1302. /* Convert module name to lower case and append '.exe'. */
  1303. filename = bfd_alloc (el, modlen + 5);
  1304. if (filename == NULL)
  1305. return NULL;
  1306. for (j = 0; j < modlen; j++)
  1307. if (ISALPHA (modname[j]))
  1308. filename[j] = TOLOWER (modname[j]);
  1309. else
  1310. filename[j] = modname[j];
  1311. memcpy (filename + modlen, ".exe", 5);
  1312. filename = _bfd_append_relative_path (archive, filename);
  1313. if (filename == NULL)
  1314. return NULL;
  1315. res = bfd_openr (filename, NULL);
  1316. if (res == NULL)
  1317. {
  1318. /* xgettext:c-format */
  1319. _bfd_error_handler(_("could not open shared image '%s' from '%s'"),
  1320. filename, bfd_get_filename (archive));
  1321. bfd_release (archive, filename);
  1322. return NULL;
  1323. }
  1324. /* FIXME: put it in a cache ? */
  1325. return res;
  1326. }
  1327. /* Standard function. */
  1328. bfd *
  1329. _bfd_vms_lib_openr_next_archived_file (bfd *archive,
  1330. bfd *last_file)
  1331. {
  1332. unsigned int idx;
  1333. bfd *res;
  1334. if (!last_file)
  1335. idx = 0;
  1336. else
  1337. idx = last_file->proxy_origin + 1;
  1338. if (idx >= bfd_libdata (archive)->nbr_modules)
  1339. {
  1340. bfd_set_error (bfd_error_no_more_archived_files);
  1341. return NULL;
  1342. }
  1343. res = _bfd_vms_lib_get_module (archive, idx);
  1344. if (res == NULL)
  1345. return res;
  1346. res->proxy_origin = idx;
  1347. return res;
  1348. }
  1349. /* Standard function. Just compute the length. */
  1350. int
  1351. _bfd_vms_lib_generic_stat_arch_elt (bfd *abfd, struct stat *st)
  1352. {
  1353. struct lib_tdata *tdata;
  1354. /* Sanity check. */
  1355. if (abfd->my_archive == NULL)
  1356. {
  1357. bfd_set_error (bfd_error_invalid_operation);
  1358. return -1;
  1359. }
  1360. tdata = bfd_libdata (abfd->my_archive);
  1361. if (tdata->type != LBR__C_TYP_IOBJ)
  1362. {
  1363. struct vms_lib_iovec *vec = (struct vms_lib_iovec *) abfd->iostream;
  1364. if (vec->file_len == (ufile_ptr)-1)
  1365. {
  1366. if (vms_lib_bseek (abfd, 0, SEEK_SET) != 0)
  1367. return -1;
  1368. /* Compute length. */
  1369. while (vms_lib_bread (abfd, NULL, 1 << 20) > 0)
  1370. ;
  1371. }
  1372. st->st_size = vec->file_len;
  1373. }
  1374. else
  1375. {
  1376. st->st_size = ((struct areltdata *)abfd->arelt_data)->parsed_size;
  1377. }
  1378. if (abfd->mtime_set)
  1379. st->st_mtime = abfd->mtime;
  1380. else
  1381. st->st_mtime = 0;
  1382. st->st_uid = 0;
  1383. st->st_gid = 0;
  1384. st->st_mode = 0644;
  1385. return 0;
  1386. }
  1387. /* Internal representation of an index entry. */
  1388. struct lib_index
  1389. {
  1390. /* Corresponding archive member. */
  1391. bfd *abfd;
  1392. /* Number of reference to this entry. */
  1393. unsigned int ref;
  1394. /* Length of the key. */
  1395. unsigned short namlen;
  1396. /* Key. */
  1397. const char *name;
  1398. };
  1399. /* Used to sort index entries. */
  1400. static int
  1401. lib_index_cmp (const void *lv, const void *rv)
  1402. {
  1403. const struct lib_index *l = lv;
  1404. const struct lib_index *r = rv;
  1405. return strcmp (l->name, r->name);
  1406. }
  1407. /* Maximum number of index blocks level. */
  1408. #define MAX_LEVEL 10
  1409. /* Get the size of an index entry. */
  1410. static unsigned int
  1411. get_idxlen (struct lib_index *idx, bool is_elfidx)
  1412. {
  1413. if (is_elfidx)
  1414. {
  1415. /* 9 is the size of struct vms_elfidx without keyname. */
  1416. if (idx->namlen > MAX_KEYLEN)
  1417. return 9 + sizeof (struct vms_kbn);
  1418. else
  1419. return 9 + idx->namlen;
  1420. }
  1421. else
  1422. {
  1423. /* 7 is the size of struct vms_idx without keyname. */
  1424. return 7 + idx->namlen;
  1425. }
  1426. }
  1427. /* Write the index composed by NBR symbols contained in IDX.
  1428. VBN is the first vbn to be used, and will contain on return the last vbn.
  1429. Can be called with ABFD set to NULL just to size the index.
  1430. If not null, TOPVBN will be assigned to the vbn of the root index tree.
  1431. IS_ELFIDX is true for elfidx (ie ia64) indexes layout.
  1432. Return TRUE on success. */
  1433. static bool
  1434. vms_write_index (bfd *abfd,
  1435. struct lib_index *idx, unsigned int nbr, unsigned int *vbn,
  1436. unsigned int *topvbn, bool is_elfidx)
  1437. {
  1438. /* The index is organized as a tree. This function implements a naive
  1439. algorithm to balance the tree: it fills the leaves, and create a new
  1440. branch when all upper leaves and branches are full. We only keep in
  1441. memory a path to the current leaf. */
  1442. unsigned int i;
  1443. int j;
  1444. int level;
  1445. /* Disk blocks for the current path. */
  1446. struct vms_indexdef *rblk[MAX_LEVEL];
  1447. /* Info on the current blocks. */
  1448. struct idxblk
  1449. {
  1450. unsigned int vbn; /* VBN of the block. */
  1451. /* The last entry is identified so that it could be copied to the
  1452. parent block. */
  1453. unsigned short len; /* Length up to the last entry. */
  1454. unsigned short lastlen; /* Length of the last entry. */
  1455. } blk[MAX_LEVEL];
  1456. /* The kbn blocks are used to store long symbol names. */
  1457. unsigned int kbn_sz = 0; /* Number of bytes available in the kbn block. */
  1458. unsigned int kbn_vbn = 0; /* VBN of the kbn block. */
  1459. unsigned char *kbn_blk = NULL; /* Contents of the kbn block. */
  1460. if (nbr == 0)
  1461. {
  1462. /* No entries. Very easy to handle. */
  1463. if (topvbn != NULL)
  1464. *topvbn = 0;
  1465. return true;
  1466. }
  1467. if (abfd == NULL)
  1468. {
  1469. /* Sort the index the first time this function is called. */
  1470. qsort (idx, nbr, sizeof (struct lib_index), lib_index_cmp);
  1471. }
  1472. /* Allocate first index block. */
  1473. level = 1;
  1474. if (abfd != NULL)
  1475. rblk[0] = bfd_zmalloc (sizeof (struct vms_indexdef));
  1476. blk[0].vbn = (*vbn)++;
  1477. blk[0].len = 0;
  1478. blk[0].lastlen = 0;
  1479. for (i = 0; i < nbr; i++, idx++)
  1480. {
  1481. unsigned int idxlen;
  1482. int flush = 0;
  1483. unsigned int key_vbn = 0;
  1484. unsigned int key_off = 0;
  1485. idxlen = get_idxlen (idx, is_elfidx);
  1486. if (is_elfidx && idx->namlen > MAX_KEYLEN)
  1487. {
  1488. /* If the key (ie name) is too long, write it in the kbn block. */
  1489. unsigned int kl = idx->namlen;
  1490. unsigned int kl_chunk;
  1491. const char *key = idx->name;
  1492. /* Write the key in the kbn, chunk after chunk. */
  1493. do
  1494. {
  1495. if (kbn_sz < sizeof (struct vms_kbn))
  1496. {
  1497. /* Not enough room in the kbn block. */
  1498. if (abfd != NULL)
  1499. {
  1500. /* Write it to the disk (if there is one). */
  1501. if (kbn_vbn != 0)
  1502. {
  1503. if (!vms_write_block (abfd, kbn_vbn, kbn_blk))
  1504. return false;
  1505. }
  1506. else
  1507. {
  1508. kbn_blk = bfd_malloc (VMS_BLOCK_SIZE);
  1509. if (kbn_blk == NULL)
  1510. return false;
  1511. }
  1512. *(unsigned short *)kbn_blk = 0;
  1513. }
  1514. /* Allocate a new block for the keys. */
  1515. kbn_vbn = (*vbn)++;
  1516. kbn_sz = VMS_BLOCK_SIZE - 2;
  1517. }
  1518. /* Size of the chunk written to the current key block. */
  1519. if (kl + sizeof (struct vms_kbn) > kbn_sz)
  1520. kl_chunk = kbn_sz - sizeof (struct vms_kbn);
  1521. else
  1522. kl_chunk = kl;
  1523. if (kbn_blk != NULL)
  1524. {
  1525. struct vms_kbn *kbn;
  1526. kbn = (struct vms_kbn *)(kbn_blk + VMS_BLOCK_SIZE - kbn_sz);
  1527. if (key_vbn == 0)
  1528. {
  1529. /* Save the rfa of the first chunk. */
  1530. key_vbn = kbn_vbn;
  1531. key_off = VMS_BLOCK_SIZE - kbn_sz;
  1532. }
  1533. bfd_putl16 (kl_chunk, kbn->keylen);
  1534. if (kl_chunk == kl)
  1535. {
  1536. /* No next chunk. */
  1537. bfd_putl32 (0, kbn->rfa.vbn);
  1538. bfd_putl16 (0, kbn->rfa.offset);
  1539. }
  1540. else
  1541. {
  1542. /* Next chunk will be at the start of the next block. */
  1543. bfd_putl32 (*vbn, kbn->rfa.vbn);
  1544. bfd_putl16 (2, kbn->rfa.offset);
  1545. }
  1546. memcpy ((char *)(kbn + 1), key, kl_chunk);
  1547. key += kl_chunk;
  1548. }
  1549. kl -= kl_chunk;
  1550. kl_chunk = (kl_chunk + 1) & ~1; /* Always align. */
  1551. kbn_sz -= kl_chunk + sizeof (struct vms_kbn);
  1552. }
  1553. while (kl > 0);
  1554. }
  1555. /* Check if a block might overflow. In this case we will flush this
  1556. block and all the blocks below it. */
  1557. for (j = 0; j < level; j++)
  1558. if (blk[j].len + blk[j].lastlen + idxlen > INDEXDEF__BLKSIZ)
  1559. flush = j + 1;
  1560. for (j = 0; j < level; j++)
  1561. {
  1562. if (j < flush)
  1563. {
  1564. /* There is not enough room to write the new entry in this
  1565. block or in a parent block. */
  1566. if (j + 1 == level)
  1567. {
  1568. BFD_ASSERT (level < MAX_LEVEL);
  1569. /* Need to create a parent. */
  1570. if (abfd != NULL)
  1571. {
  1572. rblk[level] = bfd_zmalloc (sizeof (struct vms_indexdef));
  1573. bfd_putl32 (*vbn, rblk[j]->parent);
  1574. }
  1575. blk[level].vbn = (*vbn)++;
  1576. blk[level].len = 0;
  1577. blk[level].lastlen = blk[j].lastlen;
  1578. level++;
  1579. }
  1580. /* Update parent block: write the last entry from the current
  1581. block. */
  1582. if (abfd != NULL)
  1583. {
  1584. struct vms_rfa *rfa;
  1585. /* Pointer to the last entry in parent block. */
  1586. rfa = (struct vms_rfa *)(rblk[j + 1]->keys + blk[j + 1].len);
  1587. /* Copy the whole entry. */
  1588. BFD_ASSERT (blk[j + 1].lastlen == blk[j].lastlen);
  1589. memcpy (rfa, rblk[j]->keys + blk[j].len, blk[j].lastlen);
  1590. /* Fix the entry (which in always the first field of an
  1591. entry. */
  1592. bfd_putl32 (blk[j].vbn, rfa->vbn);
  1593. bfd_putl16 (RFADEF__C_INDEX, rfa->offset);
  1594. }
  1595. if (j + 1 == flush)
  1596. {
  1597. /* And allocate it. Do it only on the block that won't be
  1598. flushed (so that the parent of the parent can be
  1599. updated too). */
  1600. blk[j + 1].len += blk[j + 1].lastlen;
  1601. blk[j + 1].lastlen = 0;
  1602. }
  1603. /* Write this block on the disk. */
  1604. if (abfd != NULL)
  1605. {
  1606. bfd_putl16 (blk[j].len + blk[j].lastlen, rblk[j]->used);
  1607. if (!vms_write_block (abfd, blk[j].vbn, rblk[j]))
  1608. return false;
  1609. }
  1610. /* Reset this block. */
  1611. blk[j].len = 0;
  1612. blk[j].lastlen = 0;
  1613. blk[j].vbn = (*vbn)++;
  1614. }
  1615. /* Append it to the block. */
  1616. if (j == 0)
  1617. {
  1618. /* Keep the previous last entry. */
  1619. blk[j].len += blk[j].lastlen;
  1620. if (abfd != NULL)
  1621. {
  1622. struct vms_rfa *rfa;
  1623. rfa = (struct vms_rfa *)(rblk[j]->keys + blk[j].len);
  1624. bfd_putl32 ((idx->abfd->proxy_origin / VMS_BLOCK_SIZE) + 1,
  1625. rfa->vbn);
  1626. bfd_putl16
  1627. ((idx->abfd->proxy_origin % VMS_BLOCK_SIZE)
  1628. + (is_elfidx ? 0 : DATA__DATA),
  1629. rfa->offset);
  1630. if (is_elfidx)
  1631. {
  1632. /* Use elfidx format. */
  1633. struct vms_elfidx *en = (struct vms_elfidx *)rfa;
  1634. en->flags = 0;
  1635. if (key_vbn != 0)
  1636. {
  1637. /* Long symbol name. */
  1638. struct vms_kbn *k = (struct vms_kbn *)(en->keyname);
  1639. bfd_putl16 (sizeof (struct vms_kbn), en->keylen);
  1640. bfd_putl16 (idx->namlen, k->keylen);
  1641. bfd_putl32 (key_vbn, k->rfa.vbn);
  1642. bfd_putl16 (key_off, k->rfa.offset);
  1643. en->flags |= ELFIDX__SYMESC;
  1644. }
  1645. else
  1646. {
  1647. bfd_putl16 (idx->namlen, en->keylen);
  1648. memcpy (en->keyname, idx->name, idx->namlen);
  1649. }
  1650. }
  1651. else
  1652. {
  1653. /* Use idx format. */
  1654. struct vms_idx *en = (struct vms_idx *)rfa;
  1655. en->keylen = idx->namlen;
  1656. memcpy (en->keyname, idx->name, idx->namlen);
  1657. }
  1658. }
  1659. }
  1660. /* The last added key can now be the last one all blocks in the
  1661. path. */
  1662. blk[j].lastlen = idxlen;
  1663. }
  1664. }
  1665. /* Save VBN of the root. */
  1666. if (topvbn != NULL)
  1667. *topvbn = blk[level - 1].vbn;
  1668. if (abfd == NULL)
  1669. return true;
  1670. /* Flush. */
  1671. for (j = 1; j < level; j++)
  1672. {
  1673. /* Update parent block: write the new entry. */
  1674. unsigned char *en;
  1675. unsigned char *par;
  1676. struct vms_rfa *rfa;
  1677. en = rblk[j - 1]->keys + blk[j - 1].len;
  1678. par = rblk[j]->keys + blk[j].len;
  1679. BFD_ASSERT (blk[j].lastlen == blk[j - 1].lastlen);
  1680. memcpy (par, en, blk[j - 1].lastlen);
  1681. rfa = (struct vms_rfa *)par;
  1682. bfd_putl32 (blk[j - 1].vbn, rfa->vbn);
  1683. bfd_putl16 (RFADEF__C_INDEX, rfa->offset);
  1684. }
  1685. for (j = 0; j < level; j++)
  1686. {
  1687. /* Write this block on the disk. */
  1688. bfd_putl16 (blk[j].len + blk[j].lastlen, rblk[j]->used);
  1689. if (!vms_write_block (abfd, blk[j].vbn, rblk[j]))
  1690. return false;
  1691. free (rblk[j]);
  1692. }
  1693. /* Write the last kbn (if any). */
  1694. if (kbn_vbn != 0)
  1695. {
  1696. if (!vms_write_block (abfd, kbn_vbn, kbn_blk))
  1697. return false;
  1698. free (kbn_blk);
  1699. }
  1700. return true;
  1701. }
  1702. /* Append data to the data block DATA. Force write if PAD is true. */
  1703. static bool
  1704. vms_write_data_block (bfd *arch, struct vms_datadef *data, file_ptr *off,
  1705. const unsigned char *buf, unsigned int len, int pad)
  1706. {
  1707. while (len > 0 || pad)
  1708. {
  1709. unsigned int doff = *off & (VMS_BLOCK_SIZE - 1);
  1710. unsigned int remlen = (DATA__LENGTH - DATA__DATA) - doff;
  1711. unsigned int l;
  1712. l = (len > remlen) ? remlen : len;
  1713. memcpy (data->data + doff, buf, l);
  1714. buf += l;
  1715. len -= l;
  1716. doff += l;
  1717. *off += l;
  1718. if (doff == (DATA__LENGTH - DATA__DATA) || (len == 0 && pad))
  1719. {
  1720. data->recs = 0;
  1721. data->fill_1 = 0;
  1722. bfd_putl32 ((*off / VMS_BLOCK_SIZE) + 2, data->link);
  1723. if (bfd_bwrite (data, sizeof (*data), arch) != sizeof (*data))
  1724. return false;
  1725. *off += DATA__LENGTH - doff;
  1726. if (len == 0)
  1727. break;
  1728. }
  1729. }
  1730. return true;
  1731. }
  1732. /* Build the symbols index. */
  1733. static bool
  1734. _bfd_vms_lib_build_map (unsigned int nbr_modules,
  1735. struct lib_index *modules,
  1736. unsigned int *res_cnt,
  1737. struct lib_index **res)
  1738. {
  1739. unsigned int i;
  1740. asymbol **syms = NULL;
  1741. long syms_max = 0;
  1742. struct lib_index *map = NULL;
  1743. unsigned int map_max = 1024; /* Fine initial default. */
  1744. unsigned int map_count = 0;
  1745. map = (struct lib_index *) bfd_malloc (map_max * sizeof (struct lib_index));
  1746. if (map == NULL)
  1747. goto error_return;
  1748. /* Gather symbols. */
  1749. for (i = 0; i < nbr_modules; i++)
  1750. {
  1751. long storage;
  1752. long symcount;
  1753. long src_count;
  1754. bfd *current = modules[i].abfd;
  1755. if ((bfd_get_file_flags (current) & HAS_SYMS) == 0)
  1756. continue;
  1757. storage = bfd_get_symtab_upper_bound (current);
  1758. if (storage < 0)
  1759. goto error_return;
  1760. if (storage != 0)
  1761. {
  1762. if (storage > syms_max)
  1763. {
  1764. free (syms);
  1765. syms_max = storage;
  1766. syms = (asymbol **) bfd_malloc (syms_max);
  1767. if (syms == NULL)
  1768. goto error_return;
  1769. }
  1770. symcount = bfd_canonicalize_symtab (current, syms);
  1771. if (symcount < 0)
  1772. goto error_return;
  1773. /* Now map over all the symbols, picking out the ones we
  1774. want. */
  1775. for (src_count = 0; src_count < symcount; src_count++)
  1776. {
  1777. flagword flags = (syms[src_count])->flags;
  1778. asection *sec = syms[src_count]->section;
  1779. if ((flags & BSF_GLOBAL
  1780. || flags & BSF_WEAK
  1781. || flags & BSF_INDIRECT
  1782. || bfd_is_com_section (sec))
  1783. && ! bfd_is_und_section (sec))
  1784. {
  1785. struct lib_index *new_map;
  1786. /* This symbol will go into the archive header. */
  1787. if (map_count == map_max)
  1788. {
  1789. map_max *= 2;
  1790. new_map = (struct lib_index *)
  1791. bfd_realloc (map, map_max * sizeof (struct lib_index));
  1792. if (new_map == NULL)
  1793. goto error_return;
  1794. map = new_map;
  1795. }
  1796. map[map_count].abfd = current;
  1797. map[map_count].namlen = strlen (syms[src_count]->name);
  1798. map[map_count].name = syms[src_count]->name;
  1799. map_count++;
  1800. modules[i].ref++;
  1801. }
  1802. }
  1803. }
  1804. }
  1805. *res_cnt = map_count;
  1806. *res = map;
  1807. return true;
  1808. error_return:
  1809. free (syms);
  1810. free (map);
  1811. return false;
  1812. }
  1813. /* Do the hard work: write an archive on the disk. */
  1814. bool
  1815. _bfd_vms_lib_write_archive_contents (bfd *arch)
  1816. {
  1817. bfd *current;
  1818. unsigned int nbr_modules;
  1819. struct lib_index *modules;
  1820. unsigned int nbr_symbols;
  1821. struct lib_index *symbols;
  1822. struct lib_tdata *tdata = bfd_libdata (arch);
  1823. unsigned int i;
  1824. file_ptr off;
  1825. unsigned int nbr_mod_iblk;
  1826. unsigned int nbr_sym_iblk;
  1827. unsigned int vbn;
  1828. unsigned int mod_idx_vbn;
  1829. unsigned int sym_idx_vbn;
  1830. bool is_elfidx = tdata->kind == vms_lib_ia64;
  1831. unsigned int max_keylen = is_elfidx ? MAX_EKEYLEN : MAX_KEYLEN;
  1832. /* Count the number of modules (and do a first sanity check). */
  1833. nbr_modules = 0;
  1834. for (current = arch->archive_head;
  1835. current != NULL;
  1836. current = current->archive_next)
  1837. {
  1838. /* This check is checking the bfds for the objects we're reading
  1839. from (which are usually either an object file or archive on
  1840. disk), not the archive entries we're writing to. We don't
  1841. actually create bfds for the archive members, we just copy
  1842. them byte-wise when we write out the archive. */
  1843. if (bfd_write_p (current) || !bfd_check_format (current, bfd_object))
  1844. {
  1845. bfd_set_error (bfd_error_invalid_operation);
  1846. goto input_err;
  1847. }
  1848. nbr_modules++;
  1849. }
  1850. /* Build the modules list. */
  1851. BFD_ASSERT (tdata->modules == NULL);
  1852. modules = bfd_alloc (arch, nbr_modules * sizeof (struct lib_index));
  1853. if (modules == NULL)
  1854. return false;
  1855. for (current = arch->archive_head, i = 0;
  1856. current != NULL;
  1857. current = current->archive_next, i++)
  1858. {
  1859. unsigned int nl;
  1860. modules[i].abfd = current;
  1861. modules[i].name = vms_get_module_name (bfd_get_filename (current), false);
  1862. modules[i].ref = 1;
  1863. /* FIXME: silently truncate long names ? */
  1864. nl = strlen (modules[i].name);
  1865. modules[i].namlen = (nl > max_keylen ? max_keylen : nl);
  1866. }
  1867. /* Create the module index. */
  1868. vbn = 0;
  1869. if (!vms_write_index (NULL, modules, nbr_modules, &vbn, NULL, is_elfidx))
  1870. return false;
  1871. nbr_mod_iblk = vbn;
  1872. /* Create symbol index. */
  1873. if (!_bfd_vms_lib_build_map (nbr_modules, modules, &nbr_symbols, &symbols))
  1874. return false;
  1875. vbn = 0;
  1876. if (!vms_write_index (NULL, symbols, nbr_symbols, &vbn, NULL, is_elfidx))
  1877. return false;
  1878. nbr_sym_iblk = vbn;
  1879. /* Write modules and remember their position. */
  1880. off = (1 + nbr_mod_iblk + nbr_sym_iblk) * VMS_BLOCK_SIZE;
  1881. if (bfd_seek (arch, off, SEEK_SET) != 0)
  1882. return false;
  1883. for (i = 0; i < nbr_modules; i++)
  1884. {
  1885. struct vms_datadef data;
  1886. unsigned char blk[VMS_BLOCK_SIZE];
  1887. struct vms_mhd *mhd;
  1888. unsigned int sz;
  1889. current = modules[i].abfd;
  1890. current->proxy_origin = off;
  1891. if (is_elfidx)
  1892. sz = 0;
  1893. else
  1894. {
  1895. /* Write the MHD as a record (ie, size first). */
  1896. sz = 2;
  1897. bfd_putl16 (tdata->mhd_size, blk);
  1898. }
  1899. mhd = (struct vms_mhd *)(blk + sz);
  1900. memset (mhd, 0, sizeof (struct vms_mhd));
  1901. mhd->lbrflag = 0;
  1902. mhd->id = MHD__C_MHDID;
  1903. mhd->objidlng = 4;
  1904. memcpy (mhd->objid, "V1.0", 4);
  1905. bfd_putl32 (modules[i].ref, mhd->refcnt);
  1906. /* FIXME: datim. */
  1907. sz += tdata->mhd_size;
  1908. sz = (sz + 1) & ~1;
  1909. /* Rewind the member to be put into the archive. */
  1910. if (bfd_seek (current, 0, SEEK_SET) != 0)
  1911. goto input_err;
  1912. /* Copy the member into the archive. */
  1913. if (is_elfidx)
  1914. {
  1915. unsigned int modsize = 0;
  1916. bfd_size_type amt;
  1917. file_ptr off_hdr = off;
  1918. /* Read to complete the first block. */
  1919. amt = bfd_bread (blk + sz, VMS_BLOCK_SIZE - sz, current);
  1920. if (amt == (bfd_size_type)-1)
  1921. goto input_err;
  1922. modsize = amt;
  1923. if (amt < VMS_BLOCK_SIZE - sz)
  1924. {
  1925. /* The member size is less than a block. Pad the block. */
  1926. memset (blk + sz + amt, 0, VMS_BLOCK_SIZE - sz - amt);
  1927. }
  1928. bfd_putl32 (modsize, mhd->modsize);
  1929. /* Write the first block (which contains an mhd). */
  1930. if (bfd_bwrite (blk, VMS_BLOCK_SIZE, arch) != VMS_BLOCK_SIZE)
  1931. goto input_err;
  1932. off += VMS_BLOCK_SIZE;
  1933. if (amt == VMS_BLOCK_SIZE - sz)
  1934. {
  1935. /* Copy the remaining. */
  1936. char buffer[DEFAULT_BUFFERSIZE];
  1937. while (1)
  1938. {
  1939. amt = bfd_bread (buffer, sizeof (buffer), current);
  1940. if (amt == (bfd_size_type)-1)
  1941. goto input_err;
  1942. if (amt == 0)
  1943. break;
  1944. modsize += amt;
  1945. if (amt != sizeof (buffer))
  1946. {
  1947. /* Clear the padding. */
  1948. memset (buffer + amt, 0, sizeof (buffer) - amt);
  1949. amt = (amt + VMS_BLOCK_SIZE) & ~(VMS_BLOCK_SIZE - 1);
  1950. }
  1951. if (bfd_bwrite (buffer, amt, arch) != amt)
  1952. goto input_err;
  1953. off += amt;
  1954. }
  1955. /* Now that the size is known, write the first block (again). */
  1956. bfd_putl32 (modsize, mhd->modsize);
  1957. if (bfd_seek (arch, off_hdr, SEEK_SET) != 0
  1958. || bfd_bwrite (blk, VMS_BLOCK_SIZE, arch) != VMS_BLOCK_SIZE)
  1959. goto input_err;
  1960. if (bfd_seek (arch, off, SEEK_SET) != 0)
  1961. goto input_err;
  1962. }
  1963. }
  1964. else
  1965. {
  1966. /* Write the MHD. */
  1967. if (!vms_write_data_block (arch, &data, &off, blk, sz, 0))
  1968. goto input_err;
  1969. /* Write the member. */
  1970. while (1)
  1971. {
  1972. sz = bfd_bread (blk, sizeof (blk), current);
  1973. if (sz == 0)
  1974. break;
  1975. if (!vms_write_data_block (arch, &data, &off, blk, sz, 0))
  1976. goto input_err;
  1977. }
  1978. /* Write the end of module marker. */
  1979. if (!vms_write_data_block (arch, &data, &off,
  1980. eotdesc, sizeof (eotdesc), 1))
  1981. goto input_err;
  1982. }
  1983. }
  1984. /* Write the indexes. */
  1985. vbn = 2;
  1986. if (!vms_write_index (arch, modules, nbr_modules, &vbn, &mod_idx_vbn,
  1987. is_elfidx))
  1988. return false;
  1989. if (!vms_write_index (arch, symbols, nbr_symbols, &vbn, &sym_idx_vbn,
  1990. is_elfidx))
  1991. return false;
  1992. /* Write libary header. */
  1993. {
  1994. unsigned char blk[VMS_BLOCK_SIZE];
  1995. struct vms_lhd *lhd = (struct vms_lhd *)blk;
  1996. struct vms_idd *idd = (struct vms_idd *)(blk + sizeof (*lhd));
  1997. unsigned int idd_flags;
  1998. unsigned int saneid;
  1999. memset (blk, 0, sizeof (blk));
  2000. lhd->type = tdata->type;
  2001. lhd->nindex = 2;
  2002. switch (tdata->kind)
  2003. {
  2004. case vms_lib_alpha:
  2005. saneid = LHD_SANEID3;
  2006. break;
  2007. case vms_lib_ia64:
  2008. saneid = LHD_SANEID6;
  2009. break;
  2010. default:
  2011. abort ();
  2012. }
  2013. bfd_putl32 (saneid, lhd->sanity);
  2014. bfd_putl16 (tdata->ver, lhd->majorid);
  2015. bfd_putl16 (0, lhd->minorid);
  2016. snprintf ((char *)lhd->lbrver + 1, sizeof (lhd->lbrver) - 1,
  2017. "GNU ar %u.%u.%u",
  2018. (unsigned)(BFD_VERSION / 100000000UL),
  2019. (unsigned)(BFD_VERSION / 1000000UL) % 100,
  2020. (unsigned)(BFD_VERSION / 10000UL) % 100);
  2021. lhd->lbrver[sizeof (lhd->lbrver) - 1] = 0;
  2022. lhd->lbrver[0] = strlen ((char *)lhd->lbrver + 1);
  2023. bfd_putl32 (tdata->credat_lo, lhd->credat + 0);
  2024. bfd_putl32 (tdata->credat_hi, lhd->credat + 4);
  2025. vms_raw_get_time (lhd->updtim);
  2026. lhd->mhdusz = tdata->mhd_size - MHD__C_USRDAT;
  2027. bfd_putl32 (nbr_modules + nbr_symbols, lhd->idxcnt);
  2028. bfd_putl32 (nbr_modules, lhd->modcnt);
  2029. bfd_putl32 (nbr_modules, lhd->modhdrs);
  2030. /* Number of blocks for index. */
  2031. bfd_putl32 (nbr_mod_iblk + nbr_sym_iblk, lhd->idxblks);
  2032. bfd_putl32 (vbn - 1, lhd->hipreal);
  2033. bfd_putl32 (vbn - 1, lhd->hiprusd);
  2034. /* VBN of the next free block. */
  2035. bfd_putl32 ((off / VMS_BLOCK_SIZE) + 1, lhd->nextvbn);
  2036. bfd_putl32 ((off / VMS_BLOCK_SIZE) + 1, lhd->nextrfa + 0);
  2037. bfd_putl16 (0, lhd->nextrfa + 4);
  2038. /* First index (modules name). */
  2039. idd_flags = IDD__FLAGS_ASCII | IDD__FLAGS_VARLENIDX
  2040. | IDD__FLAGS_NOCASECMP | IDD__FLAGS_NOCASENTR;
  2041. bfd_putl16 (idd_flags, idd->flags);
  2042. bfd_putl16 (max_keylen + 1, idd->keylen);
  2043. bfd_putl16 (mod_idx_vbn, idd->vbn);
  2044. idd++;
  2045. /* Second index (symbols name). */
  2046. bfd_putl16 (idd_flags, idd->flags);
  2047. bfd_putl16 (max_keylen + 1, idd->keylen);
  2048. bfd_putl16 (sym_idx_vbn, idd->vbn);
  2049. idd++;
  2050. if (!vms_write_block (arch, 1, blk))
  2051. return false;
  2052. }
  2053. return true;
  2054. input_err:
  2055. bfd_set_input_error (current, bfd_get_error ());
  2056. return false;
  2057. }
  2058. /* Add a target for text library. This costs almost nothing and is useful to
  2059. read VMS library on the host. */
  2060. const bfd_target alpha_vms_lib_txt_vec =
  2061. {
  2062. "vms-libtxt", /* Name. */
  2063. bfd_target_unknown_flavour,
  2064. BFD_ENDIAN_UNKNOWN, /* byteorder */
  2065. BFD_ENDIAN_UNKNOWN, /* header_byteorder */
  2066. 0, /* Object flags. */
  2067. 0, /* Sect flags. */
  2068. 0, /* symbol_leading_char. */
  2069. ' ', /* ar_pad_char. */
  2070. 15, /* ar_max_namelen. */
  2071. 0, /* match priority. */
  2072. TARGET_KEEP_UNUSED_SECTION_SYMBOLS, /* keep unused section symbols. */
  2073. bfd_getl64, bfd_getl_signed_64, bfd_putl64,
  2074. bfd_getl32, bfd_getl_signed_32, bfd_putl32,
  2075. bfd_getl16, bfd_getl_signed_16, bfd_putl16,
  2076. bfd_getl64, bfd_getl_signed_64, bfd_putl64,
  2077. bfd_getl32, bfd_getl_signed_32, bfd_putl32,
  2078. bfd_getl16, bfd_getl_signed_16, bfd_putl16,
  2079. { /* bfd_check_format. */
  2080. _bfd_dummy_target,
  2081. _bfd_dummy_target,
  2082. _bfd_vms_lib_txt_archive_p,
  2083. _bfd_dummy_target
  2084. },
  2085. { /* bfd_set_format. */
  2086. _bfd_bool_bfd_false_error,
  2087. _bfd_bool_bfd_false_error,
  2088. _bfd_bool_bfd_false_error,
  2089. _bfd_bool_bfd_false_error
  2090. },
  2091. { /* bfd_write_contents. */
  2092. _bfd_bool_bfd_false_error,
  2093. _bfd_bool_bfd_false_error,
  2094. _bfd_bool_bfd_false_error,
  2095. _bfd_bool_bfd_false_error
  2096. },
  2097. BFD_JUMP_TABLE_GENERIC (_bfd_generic),
  2098. BFD_JUMP_TABLE_COPY (_bfd_generic),
  2099. BFD_JUMP_TABLE_CORE (_bfd_nocore),
  2100. BFD_JUMP_TABLE_ARCHIVE (_bfd_vms_lib),
  2101. BFD_JUMP_TABLE_SYMBOLS (_bfd_nosymbols),
  2102. BFD_JUMP_TABLE_RELOCS (_bfd_norelocs),
  2103. BFD_JUMP_TABLE_WRITE (_bfd_nowrite),
  2104. BFD_JUMP_TABLE_LINK (_bfd_nolink),
  2105. BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),
  2106. NULL,
  2107. NULL
  2108. };