New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
cmd/go: freebsd/amd64 support for shared/archive build modes #14327
Comments
It might be as simple as changing cmd/go/build.go and cmd/dist/test.go to permit the new build modes. In any case, that is the first step. Do that and then see if any tests fail. |
Thanks! I was able to find the necessary places in cmd/go/build.go, cmd/link/internal/ld/lib.go such that a build passed the current tests. But I am having trouble finding what needs to be changed in cmd/dist/test.go Disregarding the lack of tests, and after the build succeeded, trying to compile with the shared buildmode resulted in a compiler error saying the "_rt0_amd64_freebsd_lib" symbol doesn't exist. I copied "_rt0_amd64_linux_lib" from runtime/rt0_linux_amd64.s into runtime/rt0_freebsd_amd64.s with the necessary dependencies and names. This is way out of my league and I recognize there's not necessarily any reason this code copied from Linux should work at all on Freebsd. However, at this point, compiling with buildmode shared succeeded, but attempting to dlopen and call the symbol produced a segfault:
Perhaps this all would be clearer if the right tests are enabled. I realize that it is hard to discuss this without the changes I made, but I am confused about the best way to share my changes because I don't want to submit a code review for broken code. Maybe link to a forked repository? |
In cmd/dist/test.go you need to change the supportedBuildMode method. Copying To be clear, it's not expected that you will be able to It's fine to send in a code review for broken code, just put "DO NOT REVIEW" in the description. See https://golang.org/wiki/CodeReview . |
It'd be extremely nice if compiling as a Position-Independent Executable (PIE) was supported as well on FreeBSD. HardenedBSD has implemented ASLR for FreeBSD, but golang applications can't take full advantage of ASLR since golang applications aren't compiled as PIEs. |
As above, it's probably just a matter of finding the bits of code that allow the flags, changing them to allow freebsd, and testing it. |
I have. I tested it and things went horrible. The application compiled fine, but when I tried to run it, I got segfaults. I've since lost the logs, but I"ll redo the work (it was pretty simple) and grab fresh logs when I have time. |
Well OK. I can try to offer hints if you can post the logs (maybe in a more specific freebsd/PIE specific issue?) |
I can help testing, need c-shared on BSD too. |
Same here: would love to see c-shared support for freebsd/amd64 in order to build pam modules. Made the changes to cmd/dist/test.go, cmd/go/build.go and runtime/rt0_freebsd_amd64.s, but writing a proper newosproc0 function for runtime/os_freebsd.go is far beyond my skillset. |
@wclarie could you share your changes? Here's what I've come up with for "enabling" -buildmode=pie for FreeBSD/HardenedBSD: http://ix.io/1Odk But attempting to run a simple "hello world" application compiled as a PIE results in this: http://ix.io/1Odl |
I'm trying to see what it will take to get Now I'm running the tests, but the
and then it hangs forever. Any advice on what things I should look at, and/or I'll have to change to get this to pass? |
To avoid the linker warning you may have to modify cmd/link/internal/ld/data.go to not create the .init_array section if it would be empty. To get more information about what is not working try When you say that the shared library don't have any symbols they're supposed to have, what do you mean? |
It looks like it's the first test that's hanging:
I've added debug prints to
When I say it doesn't have the symbols it's supposed to have, I meant that I build a shared library with go, but when I examine it with I got the linker warnings to go away, thanks. It seems like the init function should be in a |
My first hypotheses for why the test program hangs is that the I don't know what is going on with the symbol table issues you mention. |
The |
Look for calls to |
This seems to lead back to what @eatonphil ran into above - the That said, I've looked at the |
I am successfully compiling with the modifications, but the test is still failing. Now it's crashing instead of hanging.
Running the first test in LLDB:
I'm pretty sure that my I'm also not sure about whether the recursion happening in the above stack trace is supposed to happen or is an artifact of my incorrect My code is currently at https://github.com/jakob223/go/tree/freebsd-cshared |
I've made some more headway on this, but we're still crashing, just further along...
So this is the call to malloc() in x_cgo_thread_start in copying the ThreadStart, and it's dieing in the libc malloc because that's trying to use TLS and somehow our TLS for this thread is not right.
By the time we get to the where we crash, fs_base has been modified and the first tcb pointer is now pointing at runtime.g0 which isn't a valid thread control block. |
Lots of learning. So, of course, %fs (fs_base) is different for each thread (something I didn't immediately consider). So the "modifed" fs_base is nothing of the sort. The fs register for the initial thread is the same and the tcb values are unchanged. When the second thread is created, initially, the tcb values there are also OK, but the first one gets smashed/overwritten by runtime/asm_amd64.s:235
So I posit that get_tls() is doing the wrong thing here (it's effectively returning &tcb[0], but there are already 3 extant tcbs so it should be returning the fourth). Maybe need to play similar tricks to the Linux and Darwin code? |
gdb evidence:
|
../misc/cgo/testcsharedPASS Needed to emit the 2-instruction versions of the TLS access calls when building shared. Fortunately, it's identical to Linux so I just needed to add the cases. I'll be preparing a PR for the changes.
|
Change https://golang.org/cl/93875 mentions this issue: |
Change https://golang.org/cl/99077 mentions this issue: |
This normalizes the Linux code to act like other targets. The size argument to the rt_sigaction system call is pushed to a single function, sysSigaction. This is intended as a simplification step for CL 93875 for #14327. Change-Id: I594788e235f0da20e16e8a028e27ac8c883907c4 Reviewed-on: https://go-review.googlesource.com/99077 Run-TryBot: Ian Lance Taylor <iant@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Austin Clements <austin@google.com>
It would be nice to have Freebsd/amd64 support for the shared (and c-shared, archive and c-archive) build modes. What would it take for me (or anyone) to get this started?
The text was updated successfully, but these errors were encountered: