[Dwarf-discuss] Virtual Methods/Array Of Structs and Anonymous Structs

Lorenzo Gomez lgomez@windhoverlabs.com
Tue Dec 17 23:32:49 GMT 2024


Hello there,


hope you are all doing well.


While working with libdwarf/dwarfdump,  I noticed the following behavior.

Suppose we have the following code:


```C++
#ifndef CCHANNEL_HPP_
#define CCHANNEL_HPP_

class CChannel
{
    public:
     typedef enum
     {
         UNUSED   = 0,
         INACTIVE = 1,
         ACTIVE   = 2,
     } TState;

     typedef struct
     {
         TState State;
         int    data;
     } SHK;

     CChannel();
     virtual void getData();
     ~CChannel();

    private:
     SHK Hk;
};

#endif /* CCHANNEL_HPP_ */
```

It seems that if there is a virtual method inside of a class, the DWARF 
outputs `SHK` struct as an anonymous struct. This means that an 
instantiation of SHK will show a structure with all the info, except the 
name itself("SHK" in this case).


Interestingly enough if the struct declaration is  written as the 
following, it does not treat it as a an anonymous struct:


```C++
     typedef struct SHK
     {
         TState State;
         int    data;
     } SHK;
```

Assuming this is how it is used:
```C++
#ifndef VC_MSGS_H
#define VC_MSGS_H

#include "cchannel.hpp"
#ifdef __cplusplus
extern "C" {
#endif
typedef struct
{
     int           data;
     CChannel::SHK Channel[1];
} HkTlm_t;

#ifdef __cplusplus
}
#endif

#endif /* VC_MSG_H */
```

It also does not happen if `Channel` is just a member instead of an 
array (even if there is a virtual method in CChannel):
```C++
#ifndef VC_MSGS_H
#define VC_MSGS_H

#include "cchannel.hpp"
#ifdef __cplusplus
extern "C" {
#endif
typedef struct
{
     int           data;
     CChannel::SHK Channel;
} HkTlm_t;

#ifdef __cplusplus
}
#endif

#endif /* VC_MSG_H */

```


I'm just looking for some clarification to make sure I understand what 
is going on here and not going down the wrong path....

Here is some info about my machine

gcc (Ubuntu 9.4.0-1ubuntu1~20.04.2) 9.4.0
Copyright (C) 2019 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

NAME="Pop!_OS"
VERSION="20.04 LTS"
ID=pop
ID_LIKE="ubuntu debian"
PRETTY_NAME="Pop!_OS 20.04 LTS"
VERSION_ID="20.04"
HOME_URL="https://pop.system76.com"
SUPPORT_URL="https://support.system76.com"
BUG_REPORT_URL="https://github.com/pop-os/pop/issues"
PRIVACY_POLICY_URL="https://system76.com/privacy"
VERSION_CODENAME=focal
UBUNTU_CODENAME=focal
LOGO=distributor-logo-pop-os


Hope I was clear enough about my report. I know it's a bit all over the 
place, but my main concern is making sure /when//under what 
circumstances will gcc link the variable to the anonymous struct  
instead of typedef so I can document a tool I work on that relies on 
libdwarf correctly.


Thanks in advance

Lorenzo


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.dwarfstd.org/pipermail/dwarf-discuss/attachments/20241217/0a877822/attachment.htm>


More information about the Dwarf-discuss mailing list