How CGO create _Ctype_char and other default C types? I can not build my program on machine with char being unsigned by default

198 views
Skip to first unread message

Mahdi Hosseini

unread,
Jul 20, 2020, 3:28:15 PM7/20/20
to golang-nuts
Hi,
I try to recompile Go for a new platform which is a s390x platform using Clang instead of GCC. I can not work for C string in CGO. Apparently char in unsigned by default on my platform and building even the simple CGO program always fails with this error:

/home/user/tmp/go-build468743286/b001/_cgo_gotypes.go:175:25: undefined: _Ctype_char

the code is:

package main

//#include <stdio.h>
//char* callC() {
// return "Calling C code!";
//}
import "C"

import "fmt"

func main() {
        fmt.Println("Convert C String to Go String")
        str := C.GoString(C.callC())
        fmt.Println(str)
}

to overcome this I modified the gcc.go file in src/cmd/cgo and added this:

if s == "uchar" {
                s = "char"
            }
            name := c.Ident("_Ctype_" + s)
I don't now how CGO always convert my char* to *_Ctype_uchar instead of *_Ctype_char.
Anyone have a clue on this?



Ian Lance Taylor

unread,
Jul 20, 2020, 3:45:00 PM7/20/20
to Mahdi Hosseini, golang-nuts
cgo is just using the debug information generated by the C compiler.
It expects to see debug info for the type "char". For example, if I
compile this C file

#include <stdio.h>
char* callC() {
return "Calling C code!";
}

with clang on my system and run "readelf --debug" on the resulting
object file, I see

<1><48>: Abbrev Number: 4 (DW_TAG_base_type)
<49> DW_AT_name : (indirect string, offset: 0x4c): char
<4d> DW_AT_encoding : 6 (signed char)
<4e> DW_AT_byte_size : 1

cgo will use this to define the type "C.char".

This is independent of whether char is signed or unsigned. Given that
your code uses "char", it surprises me that there is no named entry
for it in the debug info.

Ian

Mahdi Hosseini

unread,
Jul 20, 2020, 4:39:35 PM7/20/20
to golang-nuts
Thanks that was a valuable information. It might be the debugging flag that caused the issue. I debugged the cgo file and found that in char* mached with dwarf.UcharType instead of dwarf.chatType

On Monday, July 20, 2020 at 3:45:00 PM UTC-4, Ian Lance Taylor wrote:

Ian Lance Taylor

unread,
Jul 20, 2020, 5:04:40 PM7/20/20
to Mahdi Hosseini, golang-nuts
On Mon, Jul 20, 2020 at 1:40 PM Mahdi Hosseini <m.hos...@gmail.com> wrote:
>
> Thanks that was a valuable information. It might be the debugging flag that caused the issue. I debugged the cgo file and found that in char* mached with dwarf.UcharType instead of dwarf.chatType

I think that that in itself shouldn't matter, the question is what is
the DW_AT_name attribute of the unsigned character type.

Ian
> --
> You received this message because you are subscribed to the Google Groups "golang-nuts" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/f4a72340-018e-4172-b702-af4f03ad48f1o%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages