obj_dump.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /*
  2. * BURG - Brand-new Universal loadeR from GRUB
  3. * Copyright 2009 Bean Lee - All Rights Reserved
  4. *
  5. * BURG 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. *
  10. * BURG 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. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with BURG. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include <config.h>
  19. #include <grub/types.h>
  20. #include <grub/util/obj.h>
  21. #include <grub/util/misc.h>
  22. #include <stdio.h>
  23. #include <stdlib.h>
  24. #include <string.h>
  25. static char name_buf[16];
  26. static char *
  27. get_segment_name (int type)
  28. {
  29. switch (type)
  30. {
  31. case GRUB_OBJ_SEG_TEXT:
  32. return ".text";
  33. case GRUB_OBJ_SEG_DATA:
  34. return ".data";
  35. case GRUB_OBJ_SEG_RDATA:
  36. return ".rdata";
  37. case GRUB_OBJ_SEG_BSS:
  38. return ".bss";
  39. case GRUB_OBJ_SEG_INFO:
  40. return ".info";
  41. }
  42. sprintf (name_buf, "%d", type);
  43. return name_buf;
  44. }
  45. static char *
  46. get_reloc_type (int type)
  47. {
  48. switch (type)
  49. {
  50. case GRUB_OBJ_REL_TYPE_32:
  51. return "dir32";
  52. case GRUB_OBJ_REL_TYPE_32 | GRUB_OBJ_REL_FLAG_REL:
  53. return "rel32";
  54. case GRUB_OBJ_REL_TYPE_64:
  55. return "dir64";
  56. case GRUB_OBJ_REL_TYPE_64 | GRUB_OBJ_REL_FLAG_REL:
  57. return "rel64";
  58. case GRUB_OBJ_REL_TYPE_16:
  59. return "dir16";
  60. case GRUB_OBJ_REL_TYPE_16 | GRUB_OBJ_REL_FLAG_REL:
  61. return "rel16";
  62. case GRUB_OBJ_REL_TYPE_24 | GRUB_OBJ_REL_FLAG_REL:
  63. return "rel24";
  64. case GRUB_OBJ_REL_TYPE_16HI:
  65. return "dir16hi";
  66. case GRUB_OBJ_REL_TYPE_16HI | GRUB_OBJ_REL_FLAG_REL:
  67. return "rel16hi";
  68. case GRUB_OBJ_REL_TYPE_16HA:
  69. return "dir16ha";
  70. case GRUB_OBJ_REL_TYPE_16HA | GRUB_OBJ_REL_FLAG_REL:
  71. return "rel16ha";
  72. case GRUB_OBJ_REL_TYPE_LO10:
  73. return "dirlo10";
  74. case GRUB_OBJ_REL_TYPE_HI22:
  75. return "dirhi22";
  76. case GRUB_OBJ_REL_TYPE_HM10:
  77. return "dirhm10";
  78. case GRUB_OBJ_REL_TYPE_HH22:
  79. return "dirhh22";
  80. case GRUB_OBJ_REL_TYPE_30 | GRUB_OBJ_REL_FLAG_REL:
  81. return "rel30";
  82. }
  83. sprintf (name_buf, "%x", type);
  84. return name_buf;
  85. }
  86. static int
  87. dump_segments_hook (struct grub_util_obj_segment *obj,
  88. void *closure __attribute__ ((unused)))
  89. {
  90. printf ("%-10s%08x %08x %08x %08x %d\n",
  91. get_segment_name (obj->segment.type),
  92. obj->segment.offset, obj->segment.size, obj->raw_size,
  93. obj->file_off, obj->segment.align);
  94. return 0;
  95. }
  96. void
  97. grub_obj_dump_segments (struct grub_util_obj *obj)
  98. {
  99. printf ("Segments:\n"
  100. "Segment Offset Size Raw Size File Off Align\n");
  101. grub_list_iterate (GRUB_AS_LIST (obj->segments),
  102. (grub_list_hook_t) dump_segments_hook, 0);
  103. }
  104. static int
  105. dump_symbols_hook (struct grub_util_obj_symbol *obj,
  106. void *closure __attribute__ ((unused)))
  107. {
  108. if (obj->segment)
  109. printf ("%-10s%08x %s\n",
  110. get_segment_name (obj->segment->segment.type),
  111. obj->symbol.offset + obj->segment->segment.offset, obj->name);
  112. return 0;
  113. }
  114. void
  115. grub_obj_dump_symbols (struct grub_util_obj *obj)
  116. {
  117. printf ("Symbols:\n"
  118. "Segment Offset Name\n");
  119. grub_list_iterate (GRUB_AS_LIST (obj->symbols),
  120. (grub_list_hook_t) dump_symbols_hook, 0);
  121. }
  122. static int
  123. dump_reloc_hook (struct grub_util_obj_reloc *obj,
  124. void *closure __attribute__ ((unused)))
  125. {
  126. if (obj->segment)
  127. {
  128. grub_uint32_t value;
  129. #ifdef GRUB_TARGET_USE_ADDEND
  130. value = obj->reloc.addend;
  131. #else
  132. if (obj->segment->data)
  133. value =
  134. grub_target_to_host32 (*((grub_uint32_t *) (obj->segment->data
  135. + obj->reloc.offset)));
  136. else
  137. value = 0;
  138. #endif
  139. printf ("%-10s%08x %08x %-10s%s\n",
  140. get_segment_name (obj->segment->segment.type),
  141. obj->reloc.offset + obj->segment->segment.offset, value,
  142. get_reloc_type (obj->reloc.type),
  143. ((! obj->symbol_segment) ? obj->symbol_name :
  144. get_segment_name (obj->symbol_segment->segment.type)));
  145. }
  146. return 0;
  147. }
  148. void
  149. grub_obj_dump_relocs (struct grub_util_obj *obj)
  150. {
  151. printf ("Relocs:\n"
  152. "Segment Offset Value Type Name\n");
  153. grub_list_iterate (GRUB_AS_LIST (obj->relocs),
  154. (grub_list_hook_t) dump_reloc_hook, 0);
  155. }