Recent

Author Topic: [SOLVED] How to add resource strings to an ELF under Linux?  (Read 839 times)

MMarie

  • New Member
  • *
  • Posts: 29
  • Right, lets bodge this pisspot
    • Homepage
[SOLVED] How to add resource strings to an ELF under Linux?
« on: April 18, 2023, 08:30:55 pm »
Hi,
im trying to load a string table from an elf binary using .rc files and windres. When compiling it creates the file "deash.ro" with the strings ("Hello" and "Goodbye"), however these do not appear in the binary. I found that resources on how to create resources in an elf binary using fpc are quite lacking also.

My Resource File looks like this:
Code: Text  [Select][+][-]
  1. #define IDS_HELLO    1
  2. #define IDS_GOODBYE  2
  3.  
  4. STRINGTABLE
  5. {
  6.     IDS_HELLO,   "Hello"
  7.     IDS_GOODBYE, "Goodbye"
  8. }
  9.  

Some stackoverflow also seemed to suggest that embedding resources is not supported by elf, which would suprise because of the existance of the TElfResourceReader class.

IMPORTANT EDIT:
The strings are embedded into the ELF file just in UTF-16, what the issue is now is that I cannot read anything from the string table it results in an access violation.

Heres my code for reading the string table (its cobbled together to test it before i actually making it pretty, so please pardon me :))
Code: Pascal  [Select][+][-]
  1. function GetResourceString(const AId: String): String;
  2.   var
  3.     ress: TResources;
  4.     res: TAbstractResource;
  5.     res_reader: TElfResourceReader;
  6.     str_stream: TStringStream;
  7.     _str: String;
  8.   begin
  9.     result := '';
  10.  
  11.     res_reader := TElfResourceReader.Create;
  12.     ress := TResources.Create;
  13.     ress.loadfromfile(ParamStr(0),res_reader);
  14.     res_reader.Free;
  15.     res := ress.find(RT_STRING, 1);
  16.     ress.Free;
  17.  
  18.       writeln(TStringTableResource(res).strings[0]);
  19.   end;
  20.  
« Last Edit: April 23, 2023, 04:25:21 pm by MMarie »
i use arch btw

PeterBB

  • New Member
  • *
  • Posts: 40
Re: How to add resource strings to an ELF under Linux?
« Reply #1 on: April 21, 2023, 12:17:13 pm »
A few suggestions;

TAbstractResource is not intended to be used directly. Maybe instead define res as a TStringTableResource.

res is pointed at ress in line 15, but ress is free-ed before the Writeln on res.
Try putting the ress.Free, after the Writeln.

If you compile with -gl, should get a backtrace, and it will help to see exaclty which line is causing the access violation.

TRon

  • Hero Member
  • *****
  • Posts: 2515
Re: How to add resource strings to an ELF under Linux?
« Reply #2 on: April 21, 2023, 01:25:23 pm »
PeterBB is correct there.

Your GetResourceString function should look more like:
Code: Pascal  [Select][+][-]
  1. function GetResourceString(const stringindex: integer): String;
  2. var
  3.   res  : TResources;
  4.   stres: TStringTableResource;
  5. begin
  6.   result := '';
  7.  
  8.   res := TResources.Create;
  9.   res.loadfromfile(ParamStr(0));
  10.   stres := res.find(RT_STRING, 1) as TStringTableResource;
  11.   result := stres.strings[stringindex];
  12.   res.Free;
  13. end;
  14.  

There is no need to use the reader yourself if you include the correct units, e.g. resreader, coffreader, elfreader and/or winpeimagereader

Besides that, the stringtable resource is not working the way you think it is working. Add a new string to your table with for example the index 200 and try to read that using the same code. The resource tables are split up in different (smaller) id-sections. see also https://forum.lazarus.freepascal.org/index.php/topic,62274.msg470928.html#msg470928 and https://www.freepascal.org/daily/doc/fclres/stringtableresource/tstringtableresource.html
Quote
A string table is a resource containing strings, identified by an integer id in the range 0-65535. A string table contains exactly 16 strings, and its name is an ID in the range 1-4096, determined by the highest 12 bits of the strings ID it contains, plus one. That is, a string table with 1 as name holds strings with IDs from 0 to 15, string table 2 contains strings with IDs from 16 to 31 and so on. There is no difference between an empty string and a non-existant string.

An improved version for that function would then look like:
Code: Pascal  [Select][+][-]
  1. function GetResourceString(const stringindex: integer): String;
  2. var
  3.   res: TResources;
  4.   stres: TStringTableResource;
  5. begin
  6.   result:= '';
  7.   res:= TResources.Create;
  8.   try
  9.     res.loadfromfile(ParamStr(0));
  10.     stres:= res.find(RT_STRING, succ(stringindex shr 4)) as TStringTableResource;
  11.     result:= stres.strings[stringindex];
  12.   finally
  13.     res.Free
  14.   end;
  15. end;
  16.  
« Last Edit: April 21, 2023, 02:04:16 pm by TRon »

MMarie

  • New Member
  • *
  • Posts: 29
  • Right, lets bodge this pisspot
    • Homepage
Re: How to add resource strings to an ELF under Linux?
« Reply #3 on: April 23, 2023, 04:06:37 pm »
Thanks for the help and sorry for the late response! This works perfectly, thank you so much :)
i use arch btw

TRon

  • Hero Member
  • *****
  • Posts: 2515
Re: [SOLVED] How to add resource strings to an ELF under Linux?
« Reply #4 on: April 23, 2023, 04:51:28 pm »
No problem and you are welcome. Better late then never  :D Thank you for the feedback.

 

TinyPortal © 2005-2018