Skip to content
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

cgo/darwin-amd64: Contant float/double values incorrectly initialized for statically linked objects. #1658

Closed
mkrautz opened this issue Apr 3, 2011 · 5 comments

Comments

@mkrautz
Copy link
Contributor

mkrautz commented Apr 3, 2011

I stumbled on this issue while trying to fix
https://golang.org/issue/1429.

Once linked with Go code, constant floats and doubles no longer have their correct
values.

GCC (i686-apple-darwin10-gcc-4.2.1 (GCC) 4.2.1 (Apple Inc. build 5666) (dot 3)) seems to
put them in the following seg/sect:

Section
  sectname __literal8
   segname __TEXT
      addr 0x0000000000000340
      size 0x0000000000000008
    offset 2320
     align 2^3 (8)
    reloff 0
    nreloc 0
     flags 0x00000004
 reserved1 0
 reserved2 0

As seen by:

static/staticC.o:
(__TEXT,__text) section
_printval:
0000000000000000    pushq   %rbp
0000000000000001    movq    %rsp,%rbp
0000000000000004    movsd   0x00000334(%rip),%xmm0
[...]

I've attached a simple program that demonstrates this.

What is the expected output?
float: 42.0

What do you see instead?
float: 0.0

Which compiler are you using (5g, 6g, 8g, gccgo)?
6g

Which operating system are you using?
Mac OS X 10.6.6

Which revision are you using?  (hg identify)
f782663275a7 tip

Attachments:

  1. cgo-darwin-float.tar.bz2 (1301 bytes)
@peterGo
Copy link
Contributor

peterGo commented Apr 3, 2011

Comment 1:

linux/amd64 hg id 11611373ac8a tip
$ ./run
float: 42.00

@robpike
Copy link
Contributor

robpike commented Apr 4, 2011

Comment 2:

Labels changed: added cgo.

Owner changed to @rsc.

Status changed to Accepted.

@mkrautz
Copy link
Contributor Author

mkrautz commented Apr 4, 2011

Comment 3:

A little mind dump:
After adding the __rodata section to the output file, it seems to almost work.
(https://gist.github.com/901976)
However, the addends that are calculated for relocations of what was previously the
__literal8 section are wrong.  I tried to ignore the addend for these cases (explicitly
zero it), but this just makes it always reference the first float value.
Also, the symnum of the relocation points to the __literal8 section and not a particular
symbol, so I'm guessing that the addend in this case should be the offset into the
__literal8 section, somehow...

@mkrautz
Copy link
Contributor Author

mkrautz commented Apr 4, 2011

Comment 4:

OK, the reason it references a section and not a symbol is that rel->extrn == 0.   I
guess that case isn't handled correctly in the linker?
/usr/include/mach-o/x86_64/reloc.h has this to say about the relocations that accessing
__literal8 data generates:
       lea L0(%rip), %rax
               r_type=X86_64_RELOC_SIGNED, r_length=2, r_extern=0, r_pcrel=1, r_symbolnum=3
               48 8d 05 56 00 00 00
               // assumes L0 is in third section and there is no previous non-local label.
               // The rip-relative-offset of 0x00000056 is L0-address_of_next_instruction.
               // address_of_next_instruction is the address of the relocation + 4.

@rsc
Copy link
Contributor

rsc commented Apr 7, 2011

Comment 5:

This issue was closed by revision 2aceea6.

Status changed to Fixed.

@golang golang locked and limited conversation to collaborators Jun 24, 2016
@rsc rsc removed their assignment Jun 22, 2022
This issue was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

5 participants