gdb-lines.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. /* gdb-lines.c -- Deal with source lines for GDB format
  2. Copyright (C) 1989, Free Software Foundation.
  3. This file is part of GAS, the GNU Assembler.
  4. GAS is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 1, or (at your option)
  7. any later version.
  8. GAS is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GAS; see the file COPYING. If not, write to
  14. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
  15. #include "as.h"
  16. #include "obstack.h"
  17. #include "frags.h"
  18. /* This is a souce file that we're storing .gdbline information about */
  19. /* .gdbline refers to files by numbers. We keep a linked list of them
  20. We store a list of vectors for each file. Each element of the vector
  21. contains a line-number, a frag, and an offset within the frag. */
  22. struct g_line_file {
  23. int gdb_line_file_file_number; /* fnum */
  24. int gdb_line_file_number_of_vectors; /* nv */
  25. long gdb_line_table_offset; /* taboff */
  26. struct g_line_vector *gdb_line_file_vectors; /* vec */
  27. struct g_line_file *gdb_line_file_next_file; /* nfile */
  28. };
  29. /* In order to save on space (We expect there to be LOTS of lines), we
  30. store line-number/address pairs in bunches of MAX_LINES_PER_VECTOR
  31. (originally fifty). Each vector descriptor contains
  32. gdb_line_number_of_lines the number of line-number/address pairs
  33. actually in this vector.
  34. gdb_line_lines The actual vector.
  35. gdb_line_next_vector The next vector descriptor in the linked list.
  36. */
  37. struct g_line_vector {
  38. int gdb_line_number_of_lines; /* nlines */
  39. struct g_line *gdb_line_lines; /* lines */
  40. struct g_line_vector *gdb_line_next_vector; /* nvec */
  41. };
  42. /* A .gdbline wants to store a line-number/address pair. Unfortunatly, we
  43. don't know addresses yet, so we store frag/offset which we can use to
  44. generate the address at write-out time. */
  45. struct g_line {
  46. int gdb_line_line_number; /* lno */
  47. fragS *gdb_line_frag; /* lfrag */
  48. int gdb_line_offset; /* loff */
  49. };
  50. /* The following is stolen from (gdb's? (or is it gcc's?) symseg.h file.
  51. These structures describe the format for the line# symbolic info in
  52. the gdb symbolic info file. This info is not particularly useful,
  53. except to show what we're writing into. . . */
  54. /* Source-file information.
  55. This describes the relation between source files and line numbers
  56. and addresses in the program text. */
  57. struct sourcevector
  58. {
  59. int length; /* Number of source files described */
  60. struct source *source[1]; /* Descriptions of the files */
  61. };
  62. /* Line number and address of one line. */
  63. struct line
  64. {
  65. int linenum;
  66. int address;
  67. };
  68. /* All the information on one source file. */
  69. struct source
  70. {
  71. char *name; /* Name of file */
  72. int nlines; /* Number of lines that follow */
  73. struct line lines[1]; /* Information on each line */
  74. };
  75. /* End of text from symseg.h */
  76. struct g_line_file *first_file;
  77. struct g_line_file *add_file();
  78. struct g_line_vector *add_vector();
  79. #define MAX_LINES_PER_VECTOR 50 /* lpv */
  80. /* We've been told that the current address corresponds to line LINENO in
  81. file FILE_NUMBER */
  82. void
  83. gdb_line(file_number,lineno)
  84. int file_number;
  85. int lineno;
  86. {
  87. struct g_line_file *f;
  88. struct g_line_vector *v;
  89. struct g_line *line;
  90. for(f=first_file;f;f=f->gdb_line_file_next_file)
  91. if(f->gdb_line_file_file_number==file_number)
  92. break;
  93. if(!f) f=add_file(file_number);
  94. v=f->gdb_line_file_vectors;
  95. if(!v || v->gdb_line_number_of_lines==MAX_LINES_PER_VECTOR)
  96. v=add_vector(f);
  97. line= &(v->gdb_line_lines)[v->gdb_line_number_of_lines];
  98. v->gdb_line_number_of_lines++;
  99. line->gdb_line_line_number=lineno;
  100. line->gdb_line_frag= frag_now;
  101. line->gdb_line_offset=obstack_next_free(&frags)-frag_now->fr_literal;
  102. }
  103. /* We've been told where to store the .line table for file FILE_NUMBER */
  104. void
  105. gdb_line_tab(file_number,offset)
  106. int file_number;
  107. int offset;
  108. {
  109. struct g_line_file *f;
  110. for(f=first_file;f;f=f->gdb_line_file_next_file)
  111. if(f->gdb_line_file_file_number==file_number)
  112. break;
  113. if(!f) f=add_file(file_number);
  114. if(f->gdb_line_table_offset)
  115. as_warn("Ignoring duplicate .linetab for file %d",file_number);
  116. else
  117. f->gdb_line_table_offset=offset;
  118. }
  119. /* We've got a file (FILE_NUMBER) that we haven't heard about before. Create
  120. an entry for it, etc. . . */
  121. struct g_line_file *
  122. add_file(file_number)
  123. {
  124. struct g_line_file *f;
  125. f=(struct g_line_file *)xmalloc(sizeof(struct g_line_file));
  126. f->gdb_line_file_file_number=file_number;
  127. f->gdb_line_table_offset = 0;
  128. f->gdb_line_file_number_of_vectors=0;
  129. f->gdb_line_file_vectors=(struct g_line_vector *)0;
  130. f->gdb_line_file_next_file=first_file;
  131. first_file=f;
  132. return f;
  133. }
  134. /* The last vector for file F is full. Allocate a new one. */
  135. struct g_line_vector *
  136. add_vector(f)
  137. struct g_line_file *f;
  138. {
  139. struct g_line_vector *tmp_vec;
  140. f->gdb_line_file_number_of_vectors++;
  141. tmp_vec=(struct g_line_vector *)xmalloc(sizeof(struct g_line_vector));
  142. tmp_vec->gdb_line_number_of_lines=0;
  143. tmp_vec->gdb_line_lines=(struct g_line *)xmalloc(MAX_LINES_PER_VECTOR*sizeof(struct g_line));
  144. tmp_vec->gdb_line_next_vector=f->gdb_line_file_vectors;
  145. f->gdb_line_file_vectors=tmp_vec;
  146. return tmp_vec;
  147. }
  148. /* All done. Time to write the stuff out. This should be fun. */
  149. void
  150. gdb_lines_emit()
  151. {
  152. struct g_line_file *f;
  153. struct g_line_vector *v,*old_v,*v_tmp;
  154. struct g_line *current_line_pointer; /* lp */
  155. int n;
  156. int previous_line_number;
  157. long int current_gdb_segment_pos;
  158. unsigned int number_of_things_in_table;
  159. for(f=first_file;f;f=f->gdb_line_file_next_file) {
  160. if(!f->gdb_line_table_offset) {
  161. as_warn("No .gdblinetab given for file %d. Ignoring .gdbline(s) for it.");
  162. continue;
  163. }
  164. /* Reverse the linked list of vectors. Since we built it
  165. last entry first, this puts the first entry at the start
  166. of the list. Thus we can manage to put out low line #s
  167. at the start of the table. . .*/
  168. v_tmp=0;
  169. old_v=0;
  170. for(v=f->gdb_line_file_vectors;v;v=v_tmp) {
  171. v_tmp=v->gdb_line_next_vector;
  172. v->gdb_line_next_vector=old_v;
  173. old_v=v;
  174. }
  175. f->gdb_line_file_vectors=old_v;
  176. /* Start putting stuff at the beginning of the table */
  177. current_gdb_segment_pos=f->gdb_line_table_offset+sizeof(long int);
  178. previous_line_number = -2;
  179. number_of_things_in_table = 0;
  180. /* For every vector in the table: */
  181. for(v=f->gdb_line_file_vectors;v;v=v->gdb_line_next_vector) {
  182. current_line_pointer=v->gdb_line_lines;
  183. /* For every element of every vector */
  184. for(n=v->gdb_line_number_of_lines;n;n--) {
  185. if(current_line_pointer->gdb_line_line_number != previous_line_number + 1) {
  186. /* Write out the line number */
  187. gdb_alter(current_gdb_segment_pos, -(current_line_pointer->gdb_line_line_number));
  188. current_gdb_segment_pos+=sizeof(long int);
  189. number_of_things_in_table++;
  190. }
  191. previous_line_number = current_line_pointer->gdb_line_line_number;
  192. /* And write out the address */
  193. gdb_alter(current_gdb_segment_pos,current_line_pointer->gdb_line_frag->fr_address+current_line_pointer->gdb_line_offset);
  194. current_gdb_segment_pos+=sizeof(long int);
  195. number_of_things_in_table++;
  196. current_line_pointer++;
  197. }
  198. }
  199. gdb_alter(f->gdb_line_table_offset,number_of_things_in_table);
  200. }
  201. }