openhwgroup / corev-binutils-gdb

GNU General Public License v2.0
9 stars 26 forks source link

Zcmt relaxation of single *.S-source bare metal code #88

Open silabs-hfegran opened 1 year ago

silabs-hfegran commented 1 year ago

There are cases where compilation of a single, bare-metal .S-file with the zcmt-flag march-flag enabled causes the linker to replace jumps inside the assembly with table jump instructions - this creates several issues and does not really make sense to me:

  1. The program and linker has no way of knowing the initial value of the jvt-register (jvt is not necessarily read-only), thus the address of the table is unknown. To make sense, the code also needs to initialize this register with the address of the table it wants to use.
  2. The table itself is not created (I believe this is the case - in a previous version of binutils, the content at address 0 would be overwritten)
  3. Will break manually crafted assembly files that uses manually written table(s) and cm.jalt/cm.jt instructions.
  4. Will break assembly tests that do not explicitly use zcmt (but leaves the march flag enabled), as the program jumps will get corrupted due to lack of a functional jump table.
  5. Will break assembly tests that use multiple jump tables whose locations (jvt) are updated based on the state of the program
  6. If the jvt CSR is updated, as per bullet 1, then it would cause additional new instructions (e.g. csrrw) to be inserted into a manually crafted asm file, which is usually not what one would want.

Technically, this can be avoided with -mno-relax-flag to the linker, but I would not expect this to be necessary, as that also leads to an impact of potentially blocking other relaxations. That leads me to the question of what kind of scenario with tablejump-relaxation of a manually crafted assembly file would actually be useful?

I do not currently have an example on hand to reproduce this, but I will try to add that here as soon as possible. Posting this now to raise awareness of this issue.

fanghuaqi commented 1 year ago

Hi @silabs-hfegran , I met similar issues with you, hope there will some way to not enable table jump for certain code

such as

  1. using the asm .option arch, -zcmt to disable cmt option
  2. allow a gcc option to not replace jump instruction to certain symbols not be replaced by table jump

Maybe @pz9115 @linsinan1995 can help with this issue.

Thanks

linsinan1995 commented 1 year ago

Hi, @fanghuaqi

Hi @silabs-hfegran , I met similar issues with you, hope there will some way to not enable table jump for certain code

such as

  1. using the asm .option arch, -zcmt to disable cmt option

Not sure if this works or not. IIRC, the current relaxation phrase treat symbols from different object files equally, so this won't work unless we record the arch info of each object file and skip relaxing symbols from object files without zcmt arch option.

  1. allow a gcc option to not replace jump instruction to certain symbols not be replaced by table jump

I could imagine this option will be useful in some other scenarios, and better to have it. CC @pz9115

fanghuaqi commented 1 year ago

Hi, @fanghuaqi

Hi @silabs-hfegran , I met similar issues with you, hope there will some way to not enable table jump for certain code such as

  1. using the asm .option arch, -zcmt to disable cmt option

Not sure if this works or not. IIRC, the current relaxation phrase treat symbols from different object files equally, so this won't work unless we record the arch info of each object file and skip relaxing symbols from object files without zcmt arch option.

  1. allow a gcc option to not replace jump instruction to certain symbols not be replaced by table jump

I could imagine this option will be useful in some other scenarios, and better to have it. CC @pz9115

Thanks @linsinan1995 , option 1 is not working, I have tried it, currently didn't find a good way to achive it.