Advertisement
Guest User

Untitled

a guest
Jun 27th, 2017
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 31.65 KB | None | 0 0
  1. /*
  2.  * File: symbols.h
  3.  * ----------------
  4.  * This is where you declare any exported features of the symbols.c
  5.  * module.  As is good header file practice, you should take care to
  6.  * set the header up to work correctly even if included more than once.
  7.  */
  8.  
  9. #ifndef _symbols_h
  10. #define _symbols_h
  11.  
  12. #include <stdbool.h>    //  this header defines the C99 bool type
  13.  
  14. /*
  15.  * The <elf.h> header already declares structs for the file header, section header,
  16.  * symbols, etc.  but the naming conventions are cryptic and multi-layered, which
  17.  * makes it a hassle to try to figure out what a particular field/type boils down
  18.  * to, so we declare our own version of these structures with simpler names
  19.  * to keep things cleaner for you.  We do directly use the #define-d ELF constants
  20.  * such as SHT_SYMTAB, SHT_STRTAB, STT_FUNC which come from <elf.h>
  21.  */
  22.  
  23. /* Type: Elf_File_Header
  24.  * ---------------------
  25.  * A struct that describes the data found in first few bytes of Elf file.
  26.  */
  27. typedef struct {
  28.    char identity[16];        // ELF specification information
  29.    int  other_ints[4];           // you can ignore these fields
  30.    int  offset_to_section_header_table; // offset in bytes from start of file to section headers
  31.    short other_shorts[6];        // you can ignore these fields
  32.    short number_of_section_headers; // count of section headers in table
  33. } Elf_File_Header;
  34.  
  35.  
  36. /* Type: Elf_Section_Header
  37.  * ------------------------
  38.  * A struct that describes the data for each section header. The section
  39.  * header table is a contiguous array of section header structs.
  40.  */
  41. typedef struct {
  42.    int  name;
  43.    int  type;       // type of section: SHT_SYMTAB, SHT_STRTAB, SHT_REL, etc.
  44.    int  flags;
  45.    int  addr;
  46.    int  offset;     // offset in bytes from file begin to where sectn data starts
  47.    int  size;       // number of bytes of data in the section
  48.    int  strtab_index;   // index into section header table for associated string table section
  49.    int  other_ints[3];
  50. } Elf_Section_Header;
  51.  
  52.  
  53. /* Type: Elf_Symbol
  54.  * ----------------
  55.  * A struct that describes the data for each symbol. The symtab
  56.  * section is a contiguous array of symbol structs.
  57.  */
  58. typedef struct {
  59.    int   name;          // offset into string table for symbol name
  60.    int   address;       // symbol address
  61.    int   size;          // symbol size in bytes
  62.    char  type_and_binding;      // low-order 4 bits are type (STT_FUNC, STT_OBJECT)
  63.                                 // high-order 4 bits are binding (STB_LOCAL, STB_GLOBAL)
  64.    char reserved;
  65.    char section_tag;            // will be SHN_UNDEF if symbol is undefined
  66. } Elf_Symbol;
  67.  
  68. /* Type: Symbol
  69.  * ----------------
  70.  * A struct that describes the data for each symbol, and is what is returned to the client.
  71.  */
  72.  
  73. typedef struct {
  74.  
  75.    char* name;
  76.    int address;
  77.    int size;
  78.  
  79. } Func_Symbol;
  80.  
  81. /* Type: Function_Table
  82.  * --------------------
  83.  * A struct that manages the table of symbols
  84.  */
  85.  
  86. typedef struct {
  87.  
  88.    Func_Symbol* table;
  89.    int count;
  90.  
  91. } Client_Symbol_Table;
  92.  
  93.  
  94. /* Function: GetFunctionSymbolsTable
  95.  * ---------------------------------
  96.  * The main function exported by symbol.c. This function, given a string filename, checks for formatting mistakes,
  97.  * parses the ELF file, builds a symbol table, and returns a pointer to the table.
  98.  */
  99. void *GetFunctionTable(const char *filename, int* count);
  100.  
  101.  
  102. /* Function: FreeFunctionSymbolsTable
  103.  * ----------------------------------
  104.  * Frees memory associated with a table of function symbols
  105.  */
  106. void FreeFunctionSymbolsTable(char *func_table);
  107.  
  108.  
  109. #endif
  110.  
  111.  
  112. /*
  113.  * File: symbols.h
  114.  * ----------------
  115.  * This is where you declare any exported features of the symbols.c
  116.  * module.  As is good header file practice, you should take care to
  117.  * set the header up to work correctly even if included more than once.
  118.  */
  119.  
  120. #ifndef _symbols_h
  121. #define _symbols_h
  122.  
  123. #include <stdbool.h>    //  this header defines the C99 bool type
  124.  
  125. /*
  126.  * The <elf.h> header already declares structs for the file header, section header,
  127.  * symbols, etc.  but the naming conventions are cryptic and multi-layered, which
  128.  * makes it a hassle to try to figure out what a particular field/type boils down
  129.  * to, so we declare our own version of these structures with simpler names
  130.  * to keep things cleaner for you.  We do directly use the #define-d ELF constants
  131.  * such as SHT_SYMTAB, SHT_STRTAB, STT_FUNC which come from <elf.h>
  132.  */
  133.  
  134. /* Type: Elf_File_Header
  135.  * ---------------------
  136.  * A struct that describes the data found in first few bytes of Elf file.
  137.  */
  138. typedef struct {
  139.    char identity[16];        // ELF specification information
  140.    int  other_ints[4];           // you can ignore these fields
  141.    int  offset_to_section_header_table; // offset in bytes from start of file to section headers
  142.    short other_shorts[6];        // you can ignore these fields
  143.    short number_of_section_headers; // count of section headers in table
  144. } Elf_File_Header;
  145.  
  146.  
  147. /* Type: Elf_Section_Header
  148.  * ------------------------
  149.  * A struct that describes the data for each section header. The section
  150.  * header table is a contiguous array of section header structs.
  151.  */
  152. typedef struct {
  153.    int  name;
  154.    int  type;       // type of section: SHT_SYMTAB, SHT_STRTAB, SHT_REL, etc.
  155.    int  flags;
  156.    int  addr;
  157.    int  offset;     // offset in bytes from file begin to where sectn data starts
  158.    int  size;       // number of bytes of data in the section
  159.    int  strtab_index;   // index into section header table for associated string table section
  160.    int  other_ints[3];
  161. } Elf_Section_Header;
  162.  
  163.  
  164. /* Type: Elf_Symbol
  165.  * ----------------
  166.  * A struct that describes the data for each symbol. The symtab
  167.  * section is a contiguous array of symbol structs.
  168.  */
  169. typedef struct {
  170.    int   name;          // offset into string table for symbol name
  171.    int   address;       // symbol address
  172.    int   size;          // symbol size in bytes
  173.    char  type_and_binding;      // low-order 4 bits are type (STT_FUNC, STT_OBJECT)
  174.                                 // high-order 4 bits are binding (STB_LOCAL, STB_GLOBAL)
  175.    char reserved;
  176.    char section_tag;            // will be SHN_UNDEF if symbol is undefined
  177. } Elf_Symbol;
  178.  
  179. /* Type: Symbol
  180.  * ----------------
  181.  * A struct that describes the data for each symbol, and is what is returned to the client.
  182.  */
  183.  
  184. typedef struct {
  185.  
  186.    char* name;
  187.    int address;
  188.    int size;
  189.  
  190. } Func_Symbol;
  191.  
  192. /* Type: Function_Table
  193.  * --------------------
  194.  * A struct that manages the table of symbols
  195.  */
  196.  
  197. typedef struct {
  198.  
  199.    Func_Symbol* table;
  200.    int count;
  201.  
  202. } Client_Symbol_Table;
  203.  
  204.  
  205. /* Function: GetFunctionSymbolsTable
  206.  * ---------------------------------
  207.  * The main function exported by symbol.c. This function, given a string filename, checks for formatting mistakes,
  208.  * parses the ELF file, builds a symbol table, and returns a pointer to the table.
  209.  */
  210. void *GetFunctionTable(const char *filename, int* count);
  211.  
  212.  
  213. /* Function: FreeFunctionSymbolsTable
  214.  * ----------------------------------
  215.  * Frees memory associated with a table of function symbols
  216.  */
  217. void FreeFunctionSymbolsTable(char *func_table);
  218.  
  219.  
  220. #endif
  221.  
  222.  
  223. /*
  224.  * File: symbols.h
  225.  * ----------------
  226.  * This is where you declare any exported features of the symbols.c
  227.  * module.  As is good header file practice, you should take care to
  228.  * set the header up to work correctly even if included more than once.
  229.  */
  230.  
  231. #ifndef _symbols_h
  232. #define _symbols_h
  233.  
  234. #include <stdbool.h>    //  this header defines the C99 bool type
  235.  
  236. /*
  237.  * The <elf.h> header already declares structs for the file header, section header,
  238.  * symbols, etc.  but the naming conventions are cryptic and multi-layered, which
  239.  * makes it a hassle to try to figure out what a particular field/type boils down
  240.  * to, so we declare our own version of these structures with simpler names
  241.  * to keep things cleaner for you.  We do directly use the #define-d ELF constants
  242.  * such as SHT_SYMTAB, SHT_STRTAB, STT_FUNC which come from <elf.h>
  243.  */
  244.  
  245. /* Type: Elf_File_Header
  246.  * ---------------------
  247.  * A struct that describes the data found in first few bytes of Elf file.
  248.  */
  249. typedef struct {
  250.    char identity[16];        // ELF specification information
  251.    int  other_ints[4];           // you can ignore these fields
  252.    int  offset_to_section_header_table; // offset in bytes from start of file to section headers
  253.    short other_shorts[6];        // you can ignore these fields
  254.    short number_of_section_headers; // count of section headers in table
  255. } Elf_File_Header;
  256.  
  257.  
  258. /* Type: Elf_Section_Header
  259.  * ------------------------
  260.  * A struct that describes the data for each section header. The section
  261.  * header table is a contiguous array of section header structs.
  262.  */
  263. typedef struct {
  264.    int  name;
  265.    int  type;       // type of section: SHT_SYMTAB, SHT_STRTAB, SHT_REL, etc.
  266.    int  flags;
  267.    int  addr;
  268.    int  offset;     // offset in bytes from file begin to where sectn data starts
  269.    int  size;       // number of bytes of data in the section
  270.    int  strtab_index;   // index into section header table for associated string table section
  271.    int  other_ints[3];
  272. } Elf_Section_Header;
  273.  
  274.  
  275. /* Type: Elf_Symbol
  276.  * ----------------
  277.  * A struct that describes the data for each symbol. The symtab
  278.  * section is a contiguous array of symbol structs.
  279.  */
  280. typedef struct {
  281.    int   name;          // offset into string table for symbol name
  282.    int   address;       // symbol address
  283.    int   size;          // symbol size in bytes
  284.    char  type_and_binding;      // low-order 4 bits are type (STT_FUNC, STT_OBJECT)
  285.                                 // high-order 4 bits are binding (STB_LOCAL, STB_GLOBAL)
  286.    char reserved;
  287.    char section_tag;            // will be SHN_UNDEF if symbol is undefined
  288. } Elf_Symbol;
  289.  
  290. /* Type: Symbol
  291.  * ----------------
  292.  * A struct that describes the data for each symbol, and is what is returned to the client.
  293.  */
  294.  
  295. typedef struct {
  296.  
  297.    char* name;
  298.    int address;
  299.    int size;
  300.  
  301. } Func_Symbol;
  302.  
  303. /* Type: Function_Table
  304.  * --------------------
  305.  * A struct that manages the table of symbols
  306.  */
  307.  
  308. typedef struct {
  309.  
  310.    Func_Symbol* table;
  311.    int count;
  312.  
  313. } Client_Symbol_Table;
  314.  
  315.  
  316. /* Function: GetFunctionSymbolsTable
  317.  * ---------------------------------
  318.  * The main function exported by symbol.c. This function, given a string filename, checks for formatting mistakes,
  319.  * parses the ELF file, builds a symbol table, and returns a pointer to the table.
  320.  */
  321. void *GetFunctionTable(const char *filename, int* count);
  322.  
  323.  
  324. /* Function: FreeFunctionSymbolsTable
  325.  * ----------------------------------
  326.  * Frees memory associated with a table of function symbols
  327.  */
  328. void FreeFunctionSymbolsTable(char *func_table);
  329.  
  330.  
  331. #endif
  332.  
  333.  
  334. /*
  335.  * File: symbols.h
  336.  * ----------------
  337.  * This is where you declare any exported features of the symbols.c
  338.  * module.  As is good header file practice, you should take care to
  339.  * set the header up to work correctly even if included more than once.
  340.  */
  341.  
  342. #ifndef _symbols_h
  343. #define _symbols_h
  344.  
  345. #include <stdbool.h>    //  this header defines the C99 bool type
  346.  
  347. /*
  348.  * The <elf.h> header already declares structs for the file header, section header,
  349.  * symbols, etc.  but the naming conventions are cryptic and multi-layered, which
  350.  * makes it a hassle to try to figure out what a particular field/type boils down
  351.  * to, so we declare our own version of these structures with simpler names
  352.  * to keep things cleaner for you.  We do directly use the #define-d ELF constants
  353.  * such as SHT_SYMTAB, SHT_STRTAB, STT_FUNC which come from <elf.h>
  354.  */
  355.  
  356. /* Type: Elf_File_Header
  357.  * ---------------------
  358.  * A struct that describes the data found in first few bytes of Elf file.
  359.  */
  360. typedef struct {
  361.    char identity[16];        // ELF specification information
  362.    int  other_ints[4];           // you can ignore these fields
  363.    int  offset_to_section_header_table; // offset in bytes from start of file to section headers
  364.    short other_shorts[6];        // you can ignore these fields
  365.    short number_of_section_headers; // count of section headers in table
  366. } Elf_File_Header;
  367.  
  368.  
  369. /* Type: Elf_Section_Header
  370.  * ------------------------
  371.  * A struct that describes the data for each section header. The section
  372.  * header table is a contiguous array of section header structs.
  373.  */
  374. typedef struct {
  375.    int  name;
  376.    int  type;       // type of section: SHT_SYMTAB, SHT_STRTAB, SHT_REL, etc.
  377.    int  flags;
  378.    int  addr;
  379.    int  offset;     // offset in bytes from file begin to where sectn data starts
  380.    int  size;       // number of bytes of data in the section
  381.    int  strtab_index;   // index into section header table for associated string table section
  382.    int  other_ints[3];
  383. } Elf_Section_Header;
  384.  
  385.  
  386. /* Type: Elf_Symbol
  387.  * ----------------
  388.  * A struct that describes the data for each symbol. The symtab
  389.  * section is a contiguous array of symbol structs.
  390.  */
  391. typedef struct {
  392.    int   name;          // offset into string table for symbol name
  393.    int   address;       // symbol address
  394.    int   size;          // symbol size in bytes
  395.    char  type_and_binding;      // low-order 4 bits are type (STT_FUNC, STT_OBJECT)
  396.                                 // high-order 4 bits are binding (STB_LOCAL, STB_GLOBAL)
  397.    char reserved;
  398.    char section_tag;            // will be SHN_UNDEF if symbol is undefined
  399. } Elf_Symbol;
  400.  
  401. /* Type: Symbol
  402.  * ----------------
  403.  * A struct that describes the data for each symbol, and is what is returned to the client.
  404.  */
  405.  
  406. typedef struct {
  407.  
  408.    char* name;
  409.    int address;
  410.    int size;
  411.  
  412. } Func_Symbol;
  413.  
  414. /* Type: Function_Table
  415.  * --------------------
  416.  * A struct that manages the table of symbols
  417.  */
  418.  
  419. typedef struct {
  420.  
  421.    Func_Symbol* table;
  422.    int count;
  423.  
  424. } Client_Symbol_Table;
  425.  
  426.  
  427. /* Function: GetFunctionSymbolsTable
  428.  * ---------------------------------
  429.  * The main function exported by symbol.c. This function, given a string filename, checks for formatting mistakes,
  430.  * parses the ELF file, builds a symbol table, and returns a pointer to the table.
  431.  */
  432. void *GetFunctionTable(const char *filename, int* count);
  433.  
  434.  
  435. /* Function: FreeFunctionSymbolsTable
  436.  * ----------------------------------
  437.  * Frees memory associated with a table of function symbols
  438.  */
  439. void FreeFunctionSymbolsTable(char *func_table);
  440.  
  441.  
  442. #endif
  443.  
  444.  
  445. /*
  446.  * File: symbols.h
  447.  * ----------------
  448.  * This is where you declare any exported features of the symbols.c
  449.  * module.  As is good header file practice, you should take care to
  450.  * set the header up to work correctly even if included more than once.
  451.  */
  452.  
  453. #ifndef _symbols_h
  454. #define _symbols_h
  455.  
  456. #include <stdbool.h>    //  this header defines the C99 bool type
  457.  
  458. /*
  459.  * The <elf.h> header already declares structs for the file header, section header,
  460.  * symbols, etc.  but the naming conventions are cryptic and multi-layered, which
  461.  * makes it a hassle to try to figure out what a particular field/type boils down
  462.  * to, so we declare our own version of these structures with simpler names
  463.  * to keep things cleaner for you.  We do directly use the #define-d ELF constants
  464.  * such as SHT_SYMTAB, SHT_STRTAB, STT_FUNC which come from <elf.h>
  465.  */
  466.  
  467. /* Type: Elf_File_Header
  468.  * ---------------------
  469.  * A struct that describes the data found in first few bytes of Elf file.
  470.  */
  471. typedef struct {
  472.    char identity[16];        // ELF specification information
  473.    int  other_ints[4];           // you can ignore these fields
  474.    int  offset_to_section_header_table; // offset in bytes from start of file to section headers
  475.    short other_shorts[6];        // you can ignore these fields
  476.    short number_of_section_headers; // count of section headers in table
  477. } Elf_File_Header;
  478.  
  479.  
  480. /* Type: Elf_Section_Header
  481.  * ------------------------
  482.  * A struct that describes the data for each section header. The section
  483.  * header table is a contiguous array of section header structs.
  484.  */
  485. typedef struct {
  486.    int  name;
  487.    int  type;       // type of section: SHT_SYMTAB, SHT_STRTAB, SHT_REL, etc.
  488.    int  flags;
  489.    int  addr;
  490.    int  offset;     // offset in bytes from file begin to where sectn data starts
  491.    int  size;       // number of bytes of data in the section
  492.    int  strtab_index;   // index into section header table for associated string table section
  493.    int  other_ints[3];
  494. } Elf_Section_Header;
  495.  
  496.  
  497. /* Type: Elf_Symbol
  498.  * ----------------
  499.  * A struct that describes the data for each symbol. The symtab
  500.  * section is a contiguous array of symbol structs.
  501.  */
  502. typedef struct {
  503.    int   name;          // offset into string table for symbol name
  504.    int   address;       // symbol address
  505.    int   size;          // symbol size in bytes
  506.    char  type_and_binding;      // low-order 4 bits are type (STT_FUNC, STT_OBJECT)
  507.                                 // high-order 4 bits are binding (STB_LOCAL, STB_GLOBAL)
  508.    char reserved;
  509.    char section_tag;            // will be SHN_UNDEF if symbol is undefined
  510. } Elf_Symbol;
  511.  
  512. /* Type: Symbol
  513.  * ----------------
  514.  * A struct that describes the data for each symbol, and is what is returned to the client.
  515.  */
  516.  
  517. typedef struct {
  518.  
  519.    char* name;
  520.    int address;
  521.    int size;
  522.  
  523. } Func_Symbol;
  524.  
  525. /* Type: Function_Table
  526.  * --------------------
  527.  * A struct that manages the table of symbols
  528.  */
  529.  
  530. typedef struct {
  531.  
  532.    Func_Symbol* table;
  533.    int count;
  534.  
  535. } Client_Symbol_Table;
  536.  
  537.  
  538. /* Function: GetFunctionSymbolsTable
  539.  * ---------------------------------
  540.  * The main function exported by symbol.c. This function, given a string filename, checks for formatting mistakes,
  541.  * parses the ELF file, builds a symbol table, and returns a pointer to the table.
  542.  */
  543. void *GetFunctionTable(const char *filename, int* count);
  544.  
  545.  
  546. /* Function: FreeFunctionSymbolsTable
  547.  * ----------------------------------
  548.  * Frees memory associated with a table of function symbols
  549.  */
  550. void FreeFunctionSymbolsTable(char *func_table);
  551.  
  552.  
  553. #endif
  554.  
  555.  
  556. /*
  557.  * File: symbols.h
  558.  * ----------------
  559.  * This is where you declare any exported features of the symbols.c
  560.  * module.  As is good header file practice, you should take care to
  561.  * set the header up to work correctly even if included more than once.
  562.  */
  563.  
  564. #ifndef _symbols_h
  565. #define _symbols_h
  566.  
  567. #include <stdbool.h>    //  this header defines the C99 bool type
  568.  
  569. /*
  570.  * The <elf.h> header already declares structs for the file header, section header,
  571.  * symbols, etc.  but the naming conventions are cryptic and multi-layered, which
  572.  * makes it a hassle to try to figure out what a particular field/type boils down
  573.  * to, so we declare our own version of these structures with simpler names
  574.  * to keep things cleaner for you.  We do directly use the #define-d ELF constants
  575.  * such as SHT_SYMTAB, SHT_STRTAB, STT_FUNC which come from <elf.h>
  576.  */
  577.  
  578. /* Type: Elf_File_Header
  579.  * ---------------------
  580.  * A struct that describes the data found in first few bytes of Elf file.
  581.  */
  582. typedef struct {
  583.    char identity[16];        // ELF specification information
  584.    int  other_ints[4];           // you can ignore these fields
  585.    int  offset_to_section_header_table; // offset in bytes from start of file to section headers
  586.    short other_shorts[6];        // you can ignore these fields
  587.    short number_of_section_headers; // count of section headers in table
  588. } Elf_File_Header;
  589.  
  590.  
  591. /* Type: Elf_Section_Header
  592.  * ------------------------
  593.  * A struct that describes the data for each section header. The section
  594.  * header table is a contiguous array of section header structs.
  595.  */
  596. typedef struct {
  597.    int  name;
  598.    int  type;       // type of section: SHT_SYMTAB, SHT_STRTAB, SHT_REL, etc.
  599.    int  flags;
  600.    int  addr;
  601.    int  offset;     // offset in bytes from file begin to where sectn data starts
  602.    int  size;       // number of bytes of data in the section
  603.    int  strtab_index;   // index into section header table for associated string table section
  604.    int  other_ints[3];
  605. } Elf_Section_Header;
  606.  
  607.  
  608. /* Type: Elf_Symbol
  609.  * ----------------
  610.  * A struct that describes the data for each symbol. The symtab
  611.  * section is a contiguous array of symbol structs.
  612.  */
  613. typedef struct {
  614.    int   name;          // offset into string table for symbol name
  615.    int   address;       // symbol address
  616.    int   size;          // symbol size in bytes
  617.    char  type_and_binding;      // low-order 4 bits are type (STT_FUNC, STT_OBJECT)
  618.                                 // high-order 4 bits are binding (STB_LOCAL, STB_GLOBAL)
  619.    char reserved;
  620.    char section_tag;            // will be SHN_UNDEF if symbol is undefined
  621. } Elf_Symbol;
  622.  
  623. /* Type: Symbol
  624.  * ----------------
  625.  * A struct that describes the data for each symbol, and is what is returned to the client.
  626.  */
  627.  
  628. typedef struct {
  629.  
  630.    char* name;
  631.    int address;
  632.    int size;
  633.  
  634. } Func_Symbol;
  635.  
  636. /* Type: Function_Table
  637.  * --------------------
  638.  * A struct that manages the table of symbols
  639.  */
  640.  
  641. typedef struct {
  642.  
  643.    Func_Symbol* table;
  644.    int count;
  645.  
  646. } Client_Symbol_Table;
  647.  
  648.  
  649. /* Function: GetFunctionSymbolsTable
  650.  * ---------------------------------
  651.  * The main function exported by symbol.c. This function, given a string filename, checks for formatting mistakes,
  652.  * parses the ELF file, builds a symbol table, and returns a pointer to the table.
  653.  */
  654. void *GetFunctionTable(const char *filename, int* count);
  655.  
  656.  
  657. /* Function: FreeFunctionSymbolsTable
  658.  * ----------------------------------
  659.  * Frees memory associated with a table of function symbols
  660.  */
  661. void FreeFunctionSymbolsTable(char *func_table);
  662.  
  663.  
  664. #endif
  665.  
  666.  
  667. /*
  668.  * File: symbols.h
  669.  * ----------------
  670.  * This is where you declare any exported features of the symbols.c
  671.  * module.  As is good header file practice, you should take care to
  672.  * set the header up to work correctly even if included more than once.
  673.  */
  674.  
  675. #ifndef _symbols_h
  676. #define _symbols_h
  677.  
  678. #include <stdbool.h>    //  this header defines the C99 bool type
  679.  
  680. /*
  681.  * The <elf.h> header already declares structs for the file header, section header,
  682.  * symbols, etc.  but the naming conventions are cryptic and multi-layered, which
  683.  * makes it a hassle to try to figure out what a particular field/type boils down
  684.  * to, so we declare our own version of these structures with simpler names
  685.  * to keep things cleaner for you.  We do directly use the #define-d ELF constants
  686.  * such as SHT_SYMTAB, SHT_STRTAB, STT_FUNC which come from <elf.h>
  687.  */
  688.  
  689. /* Type: Elf_File_Header
  690.  * ---------------------
  691.  * A struct that describes the data found in first few bytes of Elf file.
  692.  */
  693. typedef struct {
  694.    char identity[16];        // ELF specification information
  695.    int  other_ints[4];           // you can ignore these fields
  696.    int  offset_to_section_header_table; // offset in bytes from start of file to section headers
  697.    short other_shorts[6];        // you can ignore these fields
  698.    short number_of_section_headers; // count of section headers in table
  699. } Elf_File_Header;
  700.  
  701.  
  702. /* Type: Elf_Section_Header
  703.  * ------------------------
  704.  * A struct that describes the data for each section header. The section
  705.  * header table is a contiguous array of section header structs.
  706.  */
  707. typedef struct {
  708.    int  name;
  709.    int  type;       // type of section: SHT_SYMTAB, SHT_STRTAB, SHT_REL, etc.
  710.    int  flags;
  711.    int  addr;
  712.    int  offset;     // offset in bytes from file begin to where sectn data starts
  713.    int  size;       // number of bytes of data in the section
  714.    int  strtab_index;   // index into section header table for associated string table section
  715.    int  other_ints[3];
  716. } Elf_Section_Header;
  717.  
  718.  
  719. /* Type: Elf_Symbol
  720.  * ----------------
  721.  * A struct that describes the data for each symbol. The symtab
  722.  * section is a contiguous array of symbol structs.
  723.  */
  724. typedef struct {
  725.    int   name;          // offset into string table for symbol name
  726.    int   address;       // symbol address
  727.    int   size;          // symbol size in bytes
  728.    char  type_and_binding;      // low-order 4 bits are type (STT_FUNC, STT_OBJECT)
  729.                                 // high-order 4 bits are binding (STB_LOCAL, STB_GLOBAL)
  730.    char reserved;
  731.    char section_tag;            // will be SHN_UNDEF if symbol is undefined
  732. } Elf_Symbol;
  733.  
  734. /* Type: Symbol
  735.  * ----------------
  736.  * A struct that describes the data for each symbol, and is what is returned to the client.
  737.  */
  738.  
  739. typedef struct {
  740.  
  741.    char* name;
  742.    int address;
  743.    int size;
  744.  
  745. } Func_Symbol;
  746.  
  747. /* Type: Function_Table
  748.  * --------------------
  749.  * A struct that manages the table of symbols
  750.  */
  751.  
  752. typedef struct {
  753.  
  754.    Func_Symbol* table;
  755.    int count;
  756.  
  757. } Client_Symbol_Table;
  758.  
  759.  
  760. /* Function: GetFunctionSymbolsTable
  761.  * ---------------------------------
  762.  * The main function exported by symbol.c. This function, given a string filename, checks for formatting mistakes,
  763.  * parses the ELF file, builds a symbol table, and returns a pointer to the table.
  764.  */
  765. void *GetFunctionTable(const char *filename, int* count);
  766.  
  767.  
  768. /* Function: FreeFunctionSymbolsTable
  769.  * ----------------------------------
  770.  * Frees memory associated with a table of function symbols
  771.  */
  772. void FreeFunctionSymbolsTable(char *func_table);
  773.  
  774.  
  775. #endif
  776.  
  777.  
  778. /*
  779.  * File: symbols.h
  780.  * ----------------
  781.  * This is where you declare any exported features of the symbols.c
  782.  * module.  As is good header file practice, you should take care to
  783.  * set the header up to work correctly even if included more than once.
  784.  */
  785.  
  786. #ifndef _symbols_h
  787. #define _symbols_h
  788.  
  789. #include <stdbool.h>    //  this header defines the C99 bool type
  790.  
  791. /*
  792.  * The <elf.h> header already declares structs for the file header, section header,
  793.  * symbols, etc.  but the naming conventions are cryptic and multi-layered, which
  794.  * makes it a hassle to try to figure out what a particular field/type boils down
  795.  * to, so we declare our own version of these structures with simpler names
  796.  * to keep things cleaner for you.  We do directly use the #define-d ELF constants
  797.  * such as SHT_SYMTAB, SHT_STRTAB, STT_FUNC which come from <elf.h>
  798.  */
  799.  
  800. /* Type: Elf_File_Header
  801.  * ---------------------
  802.  * A struct that describes the data found in first few bytes of Elf file.
  803.  */
  804. typedef struct {
  805.    char identity[16];        // ELF specification information
  806.    int  other_ints[4];           // you can ignore these fields
  807.    int  offset_to_section_header_table; // offset in bytes from start of file to section headers
  808.    short other_shorts[6];        // you can ignore these fields
  809.    short number_of_section_headers; // count of section headers in table
  810. } Elf_File_Header;
  811.  
  812.  
  813. /* Type: Elf_Section_Header
  814.  * ------------------------
  815.  * A struct that describes the data for each section header. The section
  816.  * header table is a contiguous array of section header structs.
  817.  */
  818. typedef struct {
  819.    int  name;
  820.    int  type;       // type of section: SHT_SYMTAB, SHT_STRTAB, SHT_REL, etc.
  821.    int  flags;
  822.    int  addr;
  823.    int  offset;     // offset in bytes from file begin to where sectn data starts
  824.    int  size;       // number of bytes of data in the section
  825.    int  strtab_index;   // index into section header table for associated string table section
  826.    int  other_ints[3];
  827. } Elf_Section_Header;
  828.  
  829.  
  830. /* Type: Elf_Symbol
  831.  * ----------------
  832.  * A struct that describes the data for each symbol. The symtab
  833.  * section is a contiguous array of symbol structs.
  834.  */
  835. typedef struct {
  836.    int   name;          // offset into string table for symbol name
  837.    int   address;       // symbol address
  838.    int   size;          // symbol size in bytes
  839.    char  type_and_binding;      // low-order 4 bits are type (STT_FUNC, STT_OBJECT)
  840.                                 // high-order 4 bits are binding (STB_LOCAL, STB_GLOBAL)
  841.    char reserved;
  842.    char section_tag;            // will be SHN_UNDEF if symbol is undefined
  843. } Elf_Symbol;
  844.  
  845. /* Type: Symbol
  846.  * ----------------
  847.  * A struct that describes the data for each symbol, and is what is returned to the client.
  848.  */
  849.  
  850. typedef struct {
  851.  
  852.    char* name;
  853.    int address;
  854.    int size;
  855.  
  856. } Func_Symbol;
  857.  
  858. /* Type: Function_Table
  859.  * --------------------
  860.  * A struct that manages the table of symbols
  861.  */
  862.  
  863. typedef struct {
  864.  
  865.    Func_Symbol* table;
  866.    int count;
  867.  
  868. } Client_Symbol_Table;
  869.  
  870.  
  871. /* Function: GetFunctionSymbolsTable
  872.  * ---------------------------------
  873.  * The main function exported by symbol.c. This function, given a string filename, checks for formatting mistakes,
  874.  * parses the ELF file, builds a symbol table, and returns a pointer to the table.
  875.  */
  876. void *GetFunctionTable(const char *filename, int* count);
  877.  
  878.  
  879. /* Function: FreeFunctionSymbolsTable
  880.  * ----------------------------------
  881.  * Frees memory associated with a table of function symbols
  882.  */
  883. void FreeFunctionSymbolsTable(char *func_table);
  884.  
  885.  
  886. #endif
  887.  
  888.  
  889. /*
  890.  * File: symbols.h
  891.  * ----------------
  892.  * This is where you declare any exported features of the symbols.c
  893.  * module.  As is good header file practice, you should take care to
  894.  * set the header up to work correctly even if included more than once.
  895.  */
  896.  
  897. #ifndef _symbols_h
  898. #define _symbols_h
  899.  
  900. #include <stdbool.h>    //  this header defines the C99 bool type
  901.  
  902. /*
  903.  * The <elf.h> header already declares structs for the file header, section header,
  904.  * symbols, etc.  but the naming conventions are cryptic and multi-layered, which
  905.  * makes it a hassle to try to figure out what a particular field/type boils down
  906.  * to, so we declare our own version of these structures with simpler names
  907.  * to keep things cleaner for you.  We do directly use the #define-d ELF constants
  908.  * such as SHT_SYMTAB, SHT_STRTAB, STT_FUNC which come from <elf.h>
  909.  */
  910.  
  911. /* Type: Elf_File_Header
  912.  * ---------------------
  913.  * A struct that describes the data found in first few bytes of Elf file.
  914.  */
  915. typedef struct {
  916.    char identity[16];        // ELF specification information
  917.    int  other_ints[4];           // you can ignore these fields
  918.    int  offset_to_section_header_table; // offset in bytes from start of file to section headers
  919.    short other_shorts[6];        // you can ignore these fields
  920.    short number_of_section_headers; // count of section headers in table
  921. } Elf_File_Header;
  922.  
  923.  
  924. /* Type: Elf_Section_Header
  925.  * ------------------------
  926.  * A struct that describes the data for each section header. The section
  927.  * header table is a contiguous array of section header structs.
  928.  */
  929. typedef struct {
  930.    int  name;
  931.    int  type;       // type of section: SHT_SYMTAB, SHT_STRTAB, SHT_REL, etc.
  932.    int  flags;
  933.    int  addr;
  934.    int  offset;     // offset in bytes from file begin to where sectn data starts
  935.    int  size;       // number of bytes of data in the section
  936.    int  strtab_index;   // index into section header table for associated string table section
  937.    int  other_ints[3];
  938. } Elf_Section_Header;
  939.  
  940.  
  941. /* Type: Elf_Symbol
  942.  * ----------------
  943.  * A struct that describes the data for each symbol. The symtab
  944.  * section is a contiguous array of symbol structs.
  945.  */
  946. typedef struct {
  947.    int   name;          // offset into string table for symbol name
  948.    int   address;       // symbol address
  949.    int   size;          // symbol size in bytes
  950.    char  type_and_binding;      // low-order 4 bits are type (STT_FUNC, STT_OBJECT)
  951.                                 // high-order 4 bits are binding (STB_LOCAL, STB_GLOBAL)
  952.    char reserved;
  953.    char section_tag;            // will be SHN_UNDEF if symbol is undefined
  954. } Elf_Symbol;
  955.  
  956. /* Type: Symbol
  957.  * ----------------
  958.  * A struct that describes the data for each symbol, and is what is returned to the client.
  959.  */
  960.  
  961. typedef struct {
  962.  
  963.    char* name;
  964.    int address;
  965.    int size;
  966.  
  967. } Func_Symbol;
  968.  
  969. /* Type: Function_Table
  970.  * --------------------
  971.  * A struct that manages the table of symbols
  972.  */
  973.  
  974. typedef struct {
  975.  
  976.    Func_Symbol* table;
  977.    int count;
  978.  
  979. } Client_Symbol_Table;
  980.  
  981.  
  982. /* Function: GetFunctionSymbolsTable
  983.  * ---------------------------------
  984.  * The main function exported by symbol.c. This function, given a string filename, checks for formatting mistakes,
  985.  * parses the ELF file, builds a symbol table, and returns a pointer to the table.
  986.  */
  987. void *GetFunctionTable(const char *filename, int* count);
  988.  
  989.  
  990. /* Function: FreeFunctionSymbolsTable
  991.  * ----------------------------------
  992.  * Frees memory associated with a table of function symbols
  993.  */
  994. void FreeFunctionSymbolsTable(char *func_table);
  995.  
  996.  
  997. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement