0

I have some symbols collision in a C program, some previous search leads to this objcopy the problem is my workflow is a golang cgo one so I don't deal myself with the .o and .a (I know I could be the goal of my lib is to be used by other people so I can't have a custom golang workflow.).


More info of what I need: I have a bunch of functions doing various things in my go lib, this code is autogenerated and can't be predicted, they sometimes collide (have the same name) with other function later in the build pipeline, so I would like all the C function in my go lib to be renamed, this can either happend at build time using the standart cgo process (basicaly build each file first with gcc and then link them all up) or after the autogeneration of the code (I guess I could run a preprocessor renaming all the functions and there calls in the source but I weren't able to find one).


What I've tried already : #pragma extern_prefix

This and this seems very promising but whatever I try I can't get it to works :

// test.c
#include <stdio.h>

#pragma extern_prefix "TestPrefix"

int test() {
    printf("Hello, World!\n");
    return 0;
}

#pragma extern_prefix ""

int main() {
   return test();
}

Shell output :

$ gcc test.c -o test && ./test && nm -an test | grep test
Hello, World!
0000000000000000 a test.c
0000000000001149 T test

Unlike what I expected the test symbol isn't prefixed like I expect (with my understanding the symbol should be TestPrefixtest).

Jorropo
  • 348
  • 3
  • 12
  • 2
    The GCC documentation that you link to says "currently only on Tru64 UNIX". If you compile with `-Wunknown-pragma` or with `-Wall`, you should get a warning if the pragma is not understood. – M Oehm Oct 27 '20 at 17:38
  • Won't a compination of directly calling [`go tool cgo -objdir `](https://golang.org/cmd/cgo/#hdr-Using_cgo_directly) plus [`objcopy`](https://stackoverflow.com/a/10157939/720999) work for you? – kostix Oct 27 '20 at 18:29
  • Alternatively, Go has full format Go parser in its standard library, so you could post-process that auto-generated code to add a common prefix to all the symbols exported to C. – kostix Oct 27 '20 at 18:31
  • Also, what happens when you `go build -buildmode=c-archive` your `cgo` code? Is the resulting library actionable upon with `objdump`? – kostix Oct 27 '20 at 18:32
  • > The GCC documentation that you link to says "currently only on Tru64 UNIX". If you compile with -Wunknown-pragma or with -Wall, you should get a warning if the pragma is not understood. Yes thx, that does explain why this wouldn't work. – Jorropo Oct 28 '20 at 09:58
  • > Won't a compination of directly calling go tool cgo -objdir plus objcopy work for you? I know I could but I don't want, the idea is a bot autogenerate some C code and wraps it in go then people could just `go build .` and that would just magicaly work, I don't want to bother people with a custom build process. > Alternatively, Go has full format Go parser in its standard library, so you could post-process that auto-generated code to add a common prefix to all the symbols exported to C. Could you pls give me more Links ? I would love to do that. – Jorropo Oct 28 '20 at 10:00
  • I've does have found `go/parser` but that for Go code, not C one. – Jorropo Oct 28 '20 at 10:08
  • I fail to understand your requirements then: I've got an impression you're autogenerating Go code which you want to export to C (hence `cgo`), no? (On a side note: please mention the user whose comment you're replying to using `@username`—otherwise they won't get notified about your response since there's no way to "subscribe to a comment thread" on SO). (And while we're on it, it looks like SO is unfit for your question; consider posting to the [mailing list](https://groups.google.com/forum/#!forum/golang-nuts) instead.) – kostix Oct 28 '20 at 12:49

0 Answers0