1
0

elf.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833
  1. /*++
  2. Copyright (c) 2012 Minoca Corp. All Rights Reserved
  3. Module Name:
  4. elf.c
  5. Abstract:
  6. This module handles parsing ELF symbol tables for the debugger.
  7. Author:
  8. Evan Green 14-Sep-2012
  9. Environment:
  10. Debug Client
  11. --*/
  12. //
  13. // ------------------------------------------------------------------- Includes
  14. //
  15. #include "dbgrtl.h"
  16. #include <minoca/lib/im.h>
  17. #include <minoca/debug/dbgext.h>
  18. #include "elf.h"
  19. #include "symbols.h"
  20. #include "stabs.h"
  21. #include <assert.h>
  22. #include <errno.h>
  23. #include <stdio.h>
  24. #include <stdlib.h>
  25. #include <string.h>
  26. //
  27. // ---------------------------------------------------------------- Definitions
  28. //
  29. #define MALLOC(_x) malloc(_x)
  30. #define FREE(_x) free(_x)
  31. //
  32. // ------------------------------------------------------ Data Type Definitions
  33. //
  34. typedef struct _ELF_SECTION ELF_SECTION, *PELF_SECTION;
  35. struct _ELF_SECTION {
  36. PELF_SECTION Next;
  37. LONG SectionIndex;
  38. ULONG SectionAddress;
  39. };
  40. //
  41. // ----------------------------------------------- Internal Function Prototypes
  42. //
  43. LONG
  44. DbgpGetFileSize (
  45. FILE *File
  46. );
  47. VOID
  48. DbgpElfFreeSymbols (
  49. PDEBUG_SYMBOLS Symbols
  50. );
  51. BOOL
  52. DbgpLoadElfSymbolTable (
  53. PDEBUG_SYMBOLS Symbols,
  54. PSTR Filename,
  55. PELF_SECTION *SectionList
  56. );
  57. BOOL
  58. DbgpParseElfSymbolTable (
  59. PDEBUG_SYMBOLS Symbols,
  60. PELF_SECTION SectionList
  61. );
  62. //
  63. // -------------------------------------------------------------------- Globals
  64. //
  65. DEBUG_SYMBOL_INTERFACE DbgElfSymbolInterface = {
  66. DbgpElfLoadSymbols,
  67. DbgpElfFreeSymbols,
  68. NULL,
  69. NULL,
  70. NULL
  71. };
  72. //
  73. // ------------------------------------------------------------------ Functions
  74. //
  75. INT
  76. DbgpElfLoadSymbols (
  77. PSTR Filename,
  78. IMAGE_MACHINE_TYPE MachineType,
  79. ULONG Flags,
  80. PVOID HostContext,
  81. PDEBUG_SYMBOLS *Symbols
  82. )
  83. /*++
  84. Routine Description:
  85. This routine loads ELF debugging symbol information from the specified file.
  86. Arguments:
  87. Filename - Supplies the name of the binary to load symbols from.
  88. MachineType - Supplies the required machine type of the image. Set to
  89. unknown to allow the symbol library to load a file with any machine
  90. type.
  91. Flags - Supplies a bitfield of flags governing the behavior during load.
  92. These flags are specific to each symbol library.
  93. HostContext - Supplies the value to store in the host context field of the
  94. debug symbols.
  95. Symbols - Supplies an optional pointer where a pointer to the symbols will
  96. be returned on success.
  97. Return Value:
  98. 0 on success.
  99. Returns an error number on failure.
  100. --*/
  101. {
  102. UINTN AllocationSize;
  103. PDEBUG_SYMBOLS ElfSymbols;
  104. BOOL Result;
  105. INT Status;
  106. AllocationSize = sizeof(DEBUG_SYMBOLS) + sizeof(STAB_CONTEXT);
  107. ElfSymbols = MALLOC(AllocationSize);
  108. if (ElfSymbols == NULL) {
  109. Status = ENOMEM;
  110. goto ElfLoadSymbolsEnd;
  111. }
  112. memset(ElfSymbols, 0, AllocationSize);
  113. INITIALIZE_LIST_HEAD(&(ElfSymbols->SourcesHead));
  114. ElfSymbols->Interface = &DbgElfSymbolInterface;
  115. ElfSymbols->SymbolContext = ElfSymbols + 1;
  116. ElfSymbols->HostContext = HostContext;
  117. Result = DbgpLoadElfSymbols(ElfSymbols, Filename);
  118. if (Result == FALSE) {
  119. Status = EINVAL;
  120. goto ElfLoadSymbolsEnd;
  121. }
  122. Status = 0;
  123. ElfLoadSymbolsEnd:
  124. if (Status != 0) {
  125. if (ElfSymbols != NULL) {
  126. DbgpElfFreeSymbols(ElfSymbols);
  127. ElfSymbols = NULL;
  128. }
  129. }
  130. *Symbols = ElfSymbols;
  131. return 0;
  132. }
  133. BOOL
  134. DbgpLoadElfSymbols (
  135. PDEBUG_SYMBOLS Symbols,
  136. PSTR Filename
  137. )
  138. /*++
  139. Routine Description:
  140. This routine loads ELF symbols into a pre-existing set of ELF symbols.
  141. Arguments:
  142. Symbols - Supplies a pointer to debug symbols that are assumed to have
  143. already been allocated and initialized.
  144. Filename - Supplies the name of the file to load ELF symbols for.
  145. Return Value:
  146. TRUE on success.
  147. FALSE on failure.
  148. --*/
  149. {
  150. PELF_SECTION FirstSection;
  151. PELF_SECTION NextSection;
  152. BOOL Result;
  153. FirstSection = NULL;
  154. Result = DbgpLoadElfSymbolTable(Symbols, Filename, &FirstSection);
  155. if (Result == FALSE) {
  156. DbgOut("Error reading ELF symbol table.\n");
  157. goto LoadElfSymbolsEnd;
  158. }
  159. Result = DbgpParseElfSymbolTable(Symbols, FirstSection);
  160. if (Result == FALSE) {
  161. DbgOut("Error parsing ELF symbol table.\n");
  162. goto LoadElfSymbolsEnd;
  163. }
  164. LoadElfSymbolsEnd:
  165. while (FirstSection != NULL) {
  166. NextSection = FirstSection->Next;
  167. FREE(FirstSection);
  168. FirstSection = NextSection;
  169. }
  170. return Result;
  171. }
  172. //
  173. // --------------------------------------------------------- Internal Functions
  174. //
  175. VOID
  176. DbgpElfFreeSymbols (
  177. PDEBUG_SYMBOLS Symbols
  178. )
  179. /*++
  180. Routine Description:
  181. This routine frees all memory associated with an instance of debugging
  182. symbols. Once called, the pointer passed in should not be dereferenced
  183. again by the caller.
  184. Arguments:
  185. Symbols - Supplies a pointer to the debugging symbols.
  186. Return Value:
  187. None.
  188. --*/
  189. {
  190. PLIST_ENTRY CurrentFunctionEntry;
  191. PLIST_ENTRY CurrentGlobalEntry;
  192. PLIST_ENTRY CurrentSourceEntry;
  193. PFUNCTION_SYMBOL Function;
  194. PDATA_SYMBOL GlobalVariable;
  195. PLIST_ENTRY NextFunctionEntry;
  196. PSOURCE_FILE_SYMBOL SourceFile;
  197. PSTAB_CONTEXT StabContext;
  198. StabContext = Symbols->SymbolContext;
  199. if (Symbols->Filename != NULL) {
  200. FREE(Symbols->Filename);
  201. }
  202. if (StabContext->RawSymbolTable != NULL) {
  203. FREE(StabContext->RawSymbolTable);
  204. }
  205. if (StabContext->RawSymbolTableStrings != NULL) {
  206. FREE(StabContext->RawSymbolTableStrings);
  207. }
  208. //
  209. // Free Source files.
  210. //
  211. CurrentSourceEntry = Symbols->SourcesHead.Next;
  212. while ((CurrentSourceEntry != &(Symbols->SourcesHead)) &&
  213. (CurrentSourceEntry != NULL)) {
  214. SourceFile = LIST_VALUE(CurrentSourceEntry,
  215. SOURCE_FILE_SYMBOL,
  216. ListEntry);
  217. assert(LIST_EMPTY(&(SourceFile->TypesHead)));
  218. //
  219. // Free functions.
  220. //
  221. CurrentFunctionEntry = SourceFile->FunctionsHead.Next;
  222. while (CurrentFunctionEntry != &(SourceFile->FunctionsHead)) {
  223. Function = LIST_VALUE(CurrentFunctionEntry,
  224. FUNCTION_SYMBOL,
  225. ListEntry);
  226. assert(LIST_EMPTY(&(Function->ParametersHead)));
  227. assert(LIST_EMPTY(&(Function->LocalsHead)));
  228. if (Function->Name != NULL) {
  229. FREE(Function->Name);
  230. }
  231. NextFunctionEntry = CurrentFunctionEntry->Next;
  232. FREE(Function);
  233. CurrentFunctionEntry = NextFunctionEntry;
  234. }
  235. assert(LIST_EMPTY(&(SourceFile->SourceLinesHead)));
  236. //
  237. // Free global/static symbols.
  238. //
  239. CurrentGlobalEntry = SourceFile->DataSymbolsHead.Next;
  240. while (CurrentGlobalEntry != &(SourceFile->DataSymbolsHead)) {
  241. GlobalVariable = LIST_VALUE(CurrentGlobalEntry,
  242. DATA_SYMBOL,
  243. ListEntry);
  244. if (GlobalVariable->Name != NULL) {
  245. FREE(GlobalVariable->Name);
  246. }
  247. CurrentGlobalEntry = CurrentGlobalEntry->Next;
  248. FREE(GlobalVariable);
  249. }
  250. CurrentSourceEntry = CurrentSourceEntry->Next;
  251. FREE(SourceFile);
  252. }
  253. FREE(Symbols);
  254. return;
  255. }
  256. BOOL
  257. DbgpLoadElfSymbolTable (
  258. PDEBUG_SYMBOLS Symbols,
  259. PSTR Filename,
  260. PELF_SECTION *SectionList
  261. )
  262. /*++
  263. Routine Description:
  264. This routine loads the raw ELF symbol table out of the file.
  265. Arguments:
  266. Symbols - Supplies a pointer to debug symbols that are assumed to have
  267. already been allocated and initialized.
  268. Filename - Supplies the name of the file to load ELF symbols for.
  269. SectionList - Supplies a pointer that will receive a pointer to the list of
  270. sections in the ELF file. Most ELF symbol values are relative to a
  271. section like .text or .bss.
  272. Return Value:
  273. TRUE on success.
  274. FALSE on failure.
  275. --*/
  276. {
  277. ULONG BytesRead;
  278. PELF32_SECTION_HEADER CurrentSection;
  279. PELF32_HEADER ElfHeader;
  280. FILE *File;
  281. PVOID FileBuffer;
  282. ULONG FileSize;
  283. PELF_SECTION FirstSection;
  284. PELF32_SECTION_HEADER FirstSectionHeader;
  285. IMAGE_BUFFER ImageBuffer;
  286. PELF_SECTION NewSection;
  287. PELF_SECTION NextSection;
  288. BOOL Result;
  289. ULONG SectionIndex;
  290. PUCHAR Source;
  291. PSTAB_CONTEXT StabContext;
  292. PELF32_SECTION_HEADER StringSection;
  293. PELF32_SECTION_HEADER SymbolSection;
  294. CurrentSection = NULL;
  295. StabContext = Symbols->SymbolContext;
  296. FileBuffer = NULL;
  297. FirstSection = NULL;
  298. memset(&ImageBuffer, 0, sizeof(IMAGE_BUFFER));
  299. SymbolSection = NULL;
  300. StabContext->RawSymbolTable = NULL;
  301. StabContext->RawSymbolTableStrings = NULL;
  302. *SectionList = NULL;
  303. //
  304. // Determine the file size and load the file into memory.
  305. //
  306. File = fopen(Filename, "rb");
  307. if (File == NULL) {
  308. Result = FALSE;
  309. goto LoadElfSymbolTableEnd;
  310. }
  311. FileSize = DbgpGetFileSize(File);
  312. if (FileSize <= 0) {
  313. Result = FALSE;
  314. goto LoadElfSymbolTableEnd;
  315. }
  316. FileBuffer = MALLOC(FileSize);
  317. if (FileBuffer == NULL) {
  318. Result = FALSE;
  319. goto LoadElfSymbolTableEnd;
  320. }
  321. BytesRead = fread(FileBuffer, 1, FileSize, File);
  322. if (BytesRead != FileSize) {
  323. Result = FALSE;
  324. goto LoadElfSymbolTableEnd;
  325. }
  326. ImageBuffer.Data = FileBuffer;
  327. ImageBuffer.Size = FileSize;
  328. //
  329. // Get the ELF headers to determine the location of the sections.
  330. //
  331. Result = ImpElf32GetHeader(&ImageBuffer, &ElfHeader);
  332. if (Result == FALSE) {
  333. goto LoadElfSymbolTableEnd;
  334. }
  335. FirstSectionHeader =
  336. (PELF32_SECTION_HEADER)((PUCHAR)FileBuffer +
  337. ElfHeader->SectionHeaderOffset);
  338. CurrentSection = FirstSectionHeader;
  339. for (SectionIndex = 0;
  340. SectionIndex < ElfHeader->SectionHeaderCount;
  341. SectionIndex += 1) {
  342. //
  343. // Remember the symbols section.
  344. //
  345. if (CurrentSection->SectionType == ELF_SECTION_TYPE_SYMBOLS) {
  346. SymbolSection = CurrentSection;
  347. }
  348. //
  349. // Skip non-loadable sections.
  350. //
  351. if ((CurrentSection->Flags & ELF_SECTION_FLAG_LOAD) == 0) {
  352. CurrentSection += 1;
  353. continue;
  354. }
  355. //
  356. // Record this section and its address for easy access later.
  357. //
  358. NewSection = MALLOC(sizeof(ELF_SECTION));
  359. if (NewSection == NULL) {
  360. Result = FALSE;
  361. goto LoadElfSymbolTableEnd;
  362. }
  363. RtlZeroMemory(NewSection, sizeof(ELF_SECTION));
  364. NewSection->SectionIndex = SectionIndex;
  365. NewSection->SectionAddress = CurrentSection->VirtualAddress;
  366. NewSection->Next = FirstSection;
  367. FirstSection = NewSection;
  368. NewSection = NULL;
  369. CurrentSection += 1;
  370. }
  371. //
  372. // If no symbol section was found, error out.
  373. //
  374. if (SymbolSection == NULL) {
  375. Result = FALSE;
  376. goto LoadElfSymbolTableEnd;
  377. }
  378. //
  379. // Get the string table for this symbol table, stored in the Link field.
  380. //
  381. if ((SymbolSection->Link == 0) ||
  382. (SymbolSection->Link >= ElfHeader->SectionHeaderCount)) {
  383. Result = FALSE;
  384. goto LoadElfSymbolTableEnd;
  385. }
  386. StringSection = FirstSectionHeader + SymbolSection->Link;
  387. if (StringSection->SectionType != ELF_SECTION_TYPE_STRINGS) {
  388. Result = FALSE;
  389. goto LoadElfSymbolTableEnd;
  390. }
  391. StabContext->RawSymbolTableSize = SymbolSection->Size;
  392. StabContext->RawSymbolTableStringsSize = StringSection->Size;
  393. StabContext->RawSymbolTable = MALLOC(StabContext->RawSymbolTableSize);
  394. if (StabContext->RawSymbolTable == NULL) {
  395. Result = FALSE;
  396. goto LoadElfSymbolTableEnd;
  397. }
  398. StabContext->RawSymbolTableStrings =
  399. MALLOC(StabContext->RawSymbolTableStringsSize);
  400. if (StabContext->RawSymbolTableStrings == NULL) {
  401. Result = FALSE;
  402. goto LoadElfSymbolTableEnd;
  403. }
  404. Source = (PUCHAR)FileBuffer + SymbolSection->Offset;
  405. RtlCopyMemory(StabContext->RawSymbolTable,
  406. Source,
  407. StabContext->RawSymbolTableSize);
  408. Source = (PUCHAR)FileBuffer + StringSection->Offset;
  409. RtlCopyMemory(StabContext->RawSymbolTableStrings,
  410. Source,
  411. StabContext->RawSymbolTableStringsSize);
  412. Result = TRUE;
  413. LoadElfSymbolTableEnd:
  414. if (FileBuffer != NULL) {
  415. FREE(FileBuffer);
  416. }
  417. if (Result == FALSE) {
  418. if (StabContext->RawSymbolTable != NULL) {
  419. FREE(StabContext->RawSymbolTable);
  420. }
  421. if (StabContext->RawSymbolTableStrings != NULL) {
  422. FREE(StabContext->RawSymbolTableStrings);
  423. }
  424. //
  425. // Free all section entries.
  426. //
  427. while (FirstSection != NULL) {
  428. NextSection = FirstSection->Next;
  429. FREE(FirstSection);
  430. FirstSection = NextSection;
  431. }
  432. //
  433. // If successful, return the section list.
  434. //
  435. } else {
  436. *SectionList = FirstSection;
  437. }
  438. if (File != NULL) {
  439. fclose(File);
  440. }
  441. return Result;
  442. }
  443. BOOL
  444. DbgpParseElfSymbolTable (
  445. PDEBUG_SYMBOLS Symbols,
  446. PELF_SECTION SectionList
  447. )
  448. /*++
  449. Routine Description:
  450. This routine parses ELF symbol tables and combines them with existing
  451. debug symbols.
  452. Arguments:
  453. Symbols - Supplies a pointer to debug symbols that are assumed to have
  454. already been allocated and initialized. The raw symbol tables and
  455. string table are expected to be valid.
  456. SectionList - Supplies a list of all loadable section in the image. Most
  457. ELF symbols are relative to a section, so this is needed to determine
  458. the real address.
  459. Return Value:
  460. TRUE on success.
  461. FALSE on failure.
  462. --*/
  463. {
  464. PELF_SECTION CurrentSection;
  465. PSOURCE_FILE_SYMBOL CurrentSource;
  466. PLIST_ENTRY CurrentSourceEntry;
  467. PELF32_SYMBOL CurrentSymbol;
  468. PSOURCE_FILE_SYMBOL FunctionParent;
  469. PFUNCTION_SYMBOL NewFunction;
  470. SYMBOL_SEARCH_RESULT Result;
  471. PSYMBOL_SEARCH_RESULT ResultPointer;
  472. PSTAB_CONTEXT StabContext;
  473. BOOL Status;
  474. ULONG SymbolAddress;
  475. PELF32_SYMBOL SymbolEnd;
  476. PSTR SymbolName;
  477. PSTR SymbolNameCopy;
  478. ELF_SYMBOL_TYPE SymbolType;
  479. StabContext = Symbols->SymbolContext;
  480. Status = TRUE;
  481. NewFunction = NULL;
  482. SymbolNameCopy = NULL;
  483. SymbolEnd = (PELF32_SYMBOL)((PUCHAR)StabContext->RawSymbolTable +
  484. StabContext->RawSymbolTableSize);
  485. CurrentSymbol = (PELF32_SYMBOL)(StabContext->RawSymbolTable);
  486. while (CurrentSymbol + 1 <= SymbolEnd) {
  487. Result.Variety = SymbolResultInvalid;
  488. SymbolAddress = 0;
  489. SymbolType = ELF_GET_SYMBOL_TYPE(CurrentSymbol->Information);
  490. SymbolName = (PSTR)StabContext->RawSymbolTableStrings +
  491. CurrentSymbol->NameOffset;
  492. //
  493. // Create a copy of the symbol name.
  494. //
  495. SymbolNameCopy = MALLOC(strlen(SymbolName) + 1);
  496. if (SymbolNameCopy == NULL) {
  497. Status = FALSE;
  498. goto ParseElfSymbolTableEnd;
  499. }
  500. strcpy(SymbolNameCopy, SymbolName);
  501. //
  502. // Attempt to find the section address this symbol relates to.
  503. //
  504. if (CurrentSymbol->SectionIndex != 0) {
  505. CurrentSection = SectionList;
  506. while (CurrentSection != NULL) {
  507. if (CurrentSection->SectionIndex ==
  508. CurrentSymbol->SectionIndex) {
  509. SymbolAddress = CurrentSection->SectionAddress;
  510. break;
  511. }
  512. CurrentSection = CurrentSection->Next;
  513. }
  514. }
  515. switch (SymbolType) {
  516. case ElfSymbolFunction:
  517. SymbolAddress += CurrentSymbol->Value;
  518. //
  519. // Don't add symbols with no value.
  520. //
  521. if (SymbolAddress == 0) {
  522. break;
  523. }
  524. ResultPointer =
  525. DbgFindFunctionSymbol(Symbols, SymbolName, 0, &Result);
  526. //
  527. // If the function does not exist, create it. For now, only create
  528. // new functions, don't update existing ones.
  529. //
  530. if (ResultPointer == NULL) {
  531. //
  532. // Attempt to find the source file this belongs under. Loop
  533. // through all source files looking for one whose address range
  534. // contains this function.
  535. //
  536. CurrentSourceEntry = Symbols->SourcesHead.Next;
  537. FunctionParent = NULL;
  538. while (CurrentSourceEntry != &(Symbols->SourcesHead)) {
  539. CurrentSource = LIST_VALUE(CurrentSourceEntry,
  540. SOURCE_FILE_SYMBOL,
  541. ListEntry);
  542. if ((CurrentSource->StartAddress <= SymbolAddress) &&
  543. (CurrentSource->EndAddress > SymbolAddress)) {
  544. FunctionParent = CurrentSource;
  545. break;
  546. }
  547. CurrentSourceEntry = CurrentSourceEntry->Next;
  548. }
  549. //
  550. // If a parent source could not be found, there's nowhere to add
  551. // this function to.
  552. //
  553. if (FunctionParent == NULL) {
  554. break;
  555. }
  556. NewFunction = MALLOC(sizeof(FUNCTION_SYMBOL));
  557. if (NewFunction == NULL) {
  558. Status = FALSE;
  559. break;
  560. }
  561. RtlZeroMemory(NewFunction, sizeof(FUNCTION_SYMBOL));
  562. NewFunction->ParentSource = FunctionParent;
  563. NewFunction->Name = SymbolNameCopy;
  564. NewFunction->FunctionNumber = 1000;
  565. INITIALIZE_LIST_HEAD(&(NewFunction->ParametersHead));
  566. INITIALIZE_LIST_HEAD(&(NewFunction->LocalsHead));
  567. NewFunction->StartAddress = SymbolAddress;
  568. NewFunction->EndAddress = SymbolAddress + 0x20;
  569. NewFunction->ReturnTypeNumber = 0;
  570. NewFunction->ReturnTypeOwner = NULL;
  571. //
  572. // Insert the function into the current source file's list of
  573. // functions.
  574. //
  575. INSERT_BEFORE(&(NewFunction->ListEntry),
  576. &(FunctionParent->FunctionsHead));
  577. //
  578. // Null out the allocated variables so they don't get freed by
  579. // the end of this function.
  580. //
  581. NewFunction = NULL;
  582. SymbolNameCopy = NULL;
  583. }
  584. break;
  585. case ElfSymbolObject:
  586. SymbolAddress = CurrentSymbol->Value;
  587. ResultPointer = DbgFindDataSymbol(Symbols, SymbolName, 0, &Result);
  588. //
  589. // If it exists and it's current value is NULL, update it. For now,
  590. // only update, do not create globals.
  591. //
  592. if ((ResultPointer != NULL) &&
  593. (Result.U.DataResult->LocationType ==
  594. DataLocationAbsoluteAddress) &&
  595. (Result.U.DataResult->Location.Address == (UINTN)NULL)) {
  596. Result.U.DataResult->Location.Address = SymbolAddress;
  597. }
  598. break;
  599. //
  600. // Ignore other symbol types.
  601. //
  602. default:
  603. break;
  604. }
  605. //
  606. // Free the symbol name copy if it wasn't used.
  607. //
  608. if (SymbolNameCopy != NULL) {
  609. FREE(SymbolNameCopy);
  610. SymbolNameCopy = NULL;
  611. }
  612. //
  613. // Break on failure.
  614. //
  615. if (Status == FALSE) {
  616. break;
  617. }
  618. CurrentSymbol += 1;
  619. }
  620. ParseElfSymbolTableEnd:
  621. if (SymbolNameCopy != NULL) {
  622. FREE(SymbolNameCopy);
  623. }
  624. if (NewFunction != NULL) {
  625. FREE(NewFunction);
  626. }
  627. return Status;
  628. }