mipsread.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. /* Read a symbol table in MIPS' format (Third-Eye).
  2. Copyright (C) 1986-2022 Free Software Foundation, Inc.
  3. Contributed by Alessandro Forin (af@cs.cmu.edu) at CMU. Major work
  4. by Per Bothner, John Gilmore and Ian Lance Taylor at Cygnus Support.
  5. This file is part of GDB.
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 3 of the License, or
  9. (at your option) any later version.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  16. /* Read symbols from an ECOFF file. Most of the work is done in
  17. mdebugread.c. */
  18. #include "defs.h"
  19. #include "bfd.h"
  20. #include "symtab.h"
  21. #include "objfiles.h"
  22. #include "stabsread.h"
  23. #include "mdebugread.h"
  24. #include "coff/sym.h"
  25. #include "coff/internal.h"
  26. #include "coff/ecoff.h"
  27. #include "libcoff.h" /* Private BFD COFF information. */
  28. #include "libecoff.h" /* Private BFD ECOFF information. */
  29. #include "elf/common.h"
  30. #include "elf/internal.h"
  31. #include "elf/mips.h"
  32. #include "psymtab.h"
  33. static void
  34. read_alphacoff_dynamic_symtab (minimal_symbol_reader &,
  35. struct objfile *objfile);
  36. /* Initialize anything that needs initializing when a completely new
  37. symbol file is specified (not just adding some symbols from another
  38. file, e.g. a shared library). */
  39. static void
  40. mipscoff_new_init (struct objfile *ignore)
  41. {
  42. stabsread_new_init ();
  43. }
  44. /* Initialize to read a symbol file (nothing to do). */
  45. static void
  46. mipscoff_symfile_init (struct objfile *objfile)
  47. {
  48. }
  49. /* Read a symbol file from a file. */
  50. static void
  51. mipscoff_symfile_read (struct objfile *objfile, symfile_add_flags symfile_flags)
  52. {
  53. bfd *abfd = objfile->obfd;
  54. minimal_symbol_reader reader (objfile);
  55. /* Now that the executable file is positioned at symbol table,
  56. process it and define symbols accordingly. */
  57. if (!((*ecoff_backend (abfd)->debug_swap.read_debug_info)
  58. (abfd, NULL, &ecoff_data (abfd)->debug_info)))
  59. error (_("Error reading symbol table: %s"), bfd_errmsg (bfd_get_error ()));
  60. mdebug_build_psymtabs (reader, objfile, &ecoff_backend (abfd)->debug_swap,
  61. &ecoff_data (abfd)->debug_info);
  62. /* Add alpha coff dynamic symbols. */
  63. read_alphacoff_dynamic_symtab (reader, objfile);
  64. /* Install any minimal symbols that have been collected as the current
  65. minimal symbols for this objfile. */
  66. reader.install ();
  67. }
  68. /* Perform any local cleanups required when we are done with a
  69. particular objfile. */
  70. static void
  71. mipscoff_symfile_finish (struct objfile *objfile)
  72. {
  73. }
  74. /* Alpha OSF/1 encapsulates the dynamic symbols in ELF format in a
  75. standard COFF section. The ELF format for the symbols differs from
  76. the format defined in elf/external.h. It seems that a normal ELF
  77. 32-bit format is used, and the representation only changes because
  78. longs are 64-bit on the alpha. In addition, the handling of
  79. text/data section indices for symbols is different from the ELF
  80. ABI. As the BFD linker currently does not support dynamic linking
  81. on the alpha, there seems to be no reason to pollute BFD with
  82. another mixture of object file formats for now. */
  83. /* Format of an alpha external ELF symbol. */
  84. typedef struct
  85. {
  86. unsigned char st_name[4]; /* Symbol name, index in string table. */
  87. unsigned char st_pad[4]; /* Pad to long word boundary. */
  88. unsigned char st_value[8]; /* Value of the symbol. */
  89. unsigned char st_size[4]; /* Associated symbol size. */
  90. unsigned char st_info[1]; /* Type and binding attributes. */
  91. unsigned char st_other[1]; /* No defined meaning, 0. */
  92. unsigned char st_shndx[2]; /* Associated section index. */
  93. } Elfalpha_External_Sym;
  94. /* Format of an alpha external ELF dynamic info structure. */
  95. typedef struct
  96. {
  97. unsigned char d_tag[4]; /* Tag. */
  98. unsigned char d_pad[4]; /* Pad to long word boundary. */
  99. union
  100. {
  101. unsigned char d_ptr[8]; /* Pointer value. */
  102. unsigned char d_val[4]; /* Integer value. */
  103. }
  104. d_un;
  105. } Elfalpha_External_Dyn;
  106. /* Struct to obtain the section pointers for alpha dynamic symbol info. */
  107. struct alphacoff_dynsecinfo
  108. {
  109. asection *sym_sect; /* Section pointer for .dynsym section. */
  110. asection *str_sect; /* Section pointer for .dynstr section. */
  111. asection *dyninfo_sect; /* Section pointer for .dynamic section. */
  112. asection *got_sect; /* Section pointer for .got section. */
  113. };
  114. /* We are called once per section from read_alphacoff_dynamic_symtab.
  115. We need to examine each section we are passed, check to see if it
  116. is something we are interested in processing, and if so, stash away
  117. some access information for the section. */
  118. static void
  119. alphacoff_locate_sections (bfd *ignore_abfd, asection *sectp, void *sip)
  120. {
  121. struct alphacoff_dynsecinfo *si;
  122. si = (struct alphacoff_dynsecinfo *) sip;
  123. if (strcmp (sectp->name, ".dynsym") == 0)
  124. si->sym_sect = sectp;
  125. else if (strcmp (sectp->name, ".dynstr") == 0)
  126. si->str_sect = sectp;
  127. else if (strcmp (sectp->name, ".dynamic") == 0)
  128. si->dyninfo_sect = sectp;
  129. else if (strcmp (sectp->name, ".got") == 0)
  130. si->got_sect = sectp;
  131. }
  132. /* Scan an alpha dynamic symbol table for symbols of interest and add
  133. them to the minimal symbol table. */
  134. static void
  135. read_alphacoff_dynamic_symtab (minimal_symbol_reader &reader,
  136. struct objfile *objfile)
  137. {
  138. bfd *abfd = objfile->obfd;
  139. struct alphacoff_dynsecinfo si;
  140. int sym_count;
  141. int i;
  142. int stripped;
  143. Elfalpha_External_Sym *x_symp;
  144. gdb_byte *dyninfo_p;
  145. gdb_byte *dyninfo_end;
  146. int got_entry_size = 8;
  147. int dt_mips_local_gotno = -1;
  148. int dt_mips_gotsym = -1;
  149. /* We currently only know how to handle alpha dynamic symbols. */
  150. if (bfd_get_arch (abfd) != bfd_arch_alpha)
  151. return;
  152. /* Locate the dynamic symbols sections and read them in. */
  153. memset ((char *) &si, 0, sizeof (si));
  154. bfd_map_over_sections (abfd, alphacoff_locate_sections, (void *) & si);
  155. if (si.sym_sect == NULL || si.str_sect == NULL
  156. || si.dyninfo_sect == NULL || si.got_sect == NULL)
  157. return;
  158. gdb::byte_vector sym_sec (bfd_section_size (si.sym_sect));
  159. gdb::byte_vector str_sec (bfd_section_size (si.str_sect));
  160. gdb::byte_vector dyninfo_sec (bfd_section_size (si.dyninfo_sect));
  161. gdb::byte_vector got_sec (bfd_section_size (si.got_sect));
  162. if (!bfd_get_section_contents (abfd, si.sym_sect, sym_sec.data (),
  163. (file_ptr) 0, sym_sec.size ()))
  164. return;
  165. if (!bfd_get_section_contents (abfd, si.str_sect, str_sec.data (),
  166. (file_ptr) 0, str_sec.size ()))
  167. return;
  168. if (!bfd_get_section_contents (abfd, si.dyninfo_sect, dyninfo_sec.data (),
  169. (file_ptr) 0, dyninfo_sec.size ()))
  170. return;
  171. if (!bfd_get_section_contents (abfd, si.got_sect, got_sec.data (),
  172. (file_ptr) 0, got_sec.size ()))
  173. return;
  174. /* Find the number of local GOT entries and the index for the
  175. first dynamic symbol in the GOT. */
  176. for ((dyninfo_p = dyninfo_sec.data (),
  177. dyninfo_end = dyninfo_p + dyninfo_sec.size ());
  178. dyninfo_p < dyninfo_end;
  179. dyninfo_p += sizeof (Elfalpha_External_Dyn))
  180. {
  181. Elfalpha_External_Dyn *x_dynp = (Elfalpha_External_Dyn *) dyninfo_p;
  182. long dyn_tag;
  183. dyn_tag = bfd_h_get_32 (abfd, (bfd_byte *) x_dynp->d_tag);
  184. if (dyn_tag == DT_NULL)
  185. break;
  186. else if (dyn_tag == DT_MIPS_LOCAL_GOTNO)
  187. {
  188. if (dt_mips_local_gotno < 0)
  189. dt_mips_local_gotno
  190. = bfd_h_get_32 (abfd, (bfd_byte *) x_dynp->d_un.d_val);
  191. }
  192. else if (dyn_tag == DT_MIPS_GOTSYM)
  193. {
  194. if (dt_mips_gotsym < 0)
  195. dt_mips_gotsym
  196. = bfd_h_get_32 (abfd, (bfd_byte *) x_dynp->d_un.d_val);
  197. }
  198. }
  199. if (dt_mips_local_gotno < 0 || dt_mips_gotsym < 0)
  200. return;
  201. /* Scan all dynamic symbols and enter them into the minimal symbol
  202. table if appropriate. */
  203. sym_count = sym_sec.size () / sizeof (Elfalpha_External_Sym);
  204. stripped = (bfd_get_symcount (abfd) == 0);
  205. /* Skip first symbol, which is a null dummy. */
  206. for (i = 1, x_symp = (Elfalpha_External_Sym *) sym_sec.data () + 1;
  207. i < sym_count;
  208. i++, x_symp++)
  209. {
  210. unsigned long strx;
  211. char *name;
  212. bfd_vma sym_value;
  213. unsigned char sym_info;
  214. unsigned int sym_shndx;
  215. int isglobal;
  216. enum minimal_symbol_type ms_type;
  217. strx = bfd_h_get_32 (abfd, (bfd_byte *) x_symp->st_name);
  218. if (strx >= str_sec.size ())
  219. continue;
  220. name = (char *) (str_sec.data () + strx);
  221. if (*name == '\0' || *name == '.')
  222. continue;
  223. sym_value = bfd_h_get_64 (abfd, (bfd_byte *) x_symp->st_value);
  224. sym_info = bfd_h_get_8 (abfd, (bfd_byte *) x_symp->st_info);
  225. sym_shndx = bfd_h_get_16 (abfd, (bfd_byte *) x_symp->st_shndx);
  226. if (sym_shndx >= (SHN_LORESERVE & 0xffff))
  227. sym_shndx += SHN_LORESERVE - (SHN_LORESERVE & 0xffff);
  228. isglobal = (ELF_ST_BIND (sym_info) == STB_GLOBAL);
  229. if (sym_shndx == SHN_UNDEF)
  230. {
  231. /* Handle undefined functions which are defined in a shared
  232. library. */
  233. if (ELF_ST_TYPE (sym_info) != STT_FUNC
  234. || ELF_ST_BIND (sym_info) != STB_GLOBAL)
  235. continue;
  236. ms_type = mst_solib_trampoline;
  237. /* If sym_value is nonzero, it points to the shared library
  238. trampoline entry, which is what we are looking for.
  239. If sym_value is zero, then we have to get the GOT entry
  240. for the symbol.
  241. If the GOT entry is nonzero, it represents the quickstart
  242. address of the function and we use that as the symbol
  243. value.
  244. If the GOT entry is zero, the function address has to be
  245. resolved by the runtime loader before the executable is
  246. started. We are unable to find any meaningful address
  247. for these functions in the executable file, so we skip
  248. them. */
  249. if (sym_value == 0)
  250. {
  251. int got_entry_offset =
  252. (i - dt_mips_gotsym + dt_mips_local_gotno) * got_entry_size;
  253. if (got_entry_offset < 0
  254. || got_entry_offset >= got_sec.size ())
  255. continue;
  256. sym_value =
  257. bfd_h_get_64 (abfd,
  258. (bfd_byte *) (got_sec.data ()
  259. + got_entry_offset));
  260. if (sym_value == 0)
  261. continue;
  262. }
  263. }
  264. else
  265. {
  266. /* Symbols defined in the executable itself. We only care
  267. about them if this is a stripped executable, otherwise
  268. they have been retrieved from the normal symbol table
  269. already. */
  270. if (!stripped)
  271. continue;
  272. if (sym_shndx == SHN_MIPS_TEXT)
  273. {
  274. if (isglobal)
  275. ms_type = mst_text;
  276. else
  277. ms_type = mst_file_text;
  278. }
  279. else if (sym_shndx == SHN_MIPS_DATA)
  280. {
  281. if (isglobal)
  282. ms_type = mst_data;
  283. else
  284. ms_type = mst_file_data;
  285. }
  286. else if (sym_shndx == SHN_MIPS_ACOMMON)
  287. {
  288. if (isglobal)
  289. ms_type = mst_bss;
  290. else
  291. ms_type = mst_file_bss;
  292. }
  293. else if (sym_shndx == SHN_ABS)
  294. {
  295. ms_type = mst_abs;
  296. }
  297. else
  298. {
  299. continue;
  300. }
  301. }
  302. reader.record (name, sym_value, ms_type);
  303. }
  304. }
  305. /* Initialization. */
  306. static const struct sym_fns ecoff_sym_fns =
  307. {
  308. mipscoff_new_init, /* init anything gbl to entire symtab */
  309. mipscoff_symfile_init, /* read initial info, setup for sym_read() */
  310. mipscoff_symfile_read, /* read a symbol file into symtab */
  311. mipscoff_symfile_finish, /* finished with file, cleanup */
  312. default_symfile_offsets, /* dummy FIXME til implem sym reloc */
  313. default_symfile_segments, /* Get segment information from a file. */
  314. NULL,
  315. default_symfile_relocate, /* Relocate a debug section. */
  316. NULL, /* sym_probe_fns */
  317. };
  318. void _initialize_mipsread ();
  319. void
  320. _initialize_mipsread ()
  321. {
  322. add_symtab_fns (bfd_target_ecoff_flavour, &ecoff_sym_fns);
  323. }