Quais são as diferenças entre os níveis de visibilidade dos símbolos ELF?

2

Os documentos da NASM em " elf Extensões da Diretiva GLOBAL" dizem,

Optionally, you can control the ELF visibility of the symbol. Just add one of the visibility keywords: default, internal, hidden, or protected. The default is default of course.

Onde estão estes definidos? e como ld os usa? Vejo níveis de acesso mencionados com frequência em C ++, que incluem protected , public , e private , mas não sei se é isso que ELF está referenciando?

Meu caso de uso é C e Assembly, portanto, se você puder tornar isso relevante para esses dois idiomas e para o vinculador, pontos extras.

    
por Evan Carroll 02.10.2018 / 01:47

1 resposta

1

Parece que a fonte NASM parece corresponder com os documentos do Oracle "Linker and Libraries Guide" , estes parecem corresponder a STV_DEFAULT , STV_INTERNAL , STV_HIDDEN e STV_PROTECTED .

A Oracle diz isso:

  • STV_DEFAULT The visibility of symbols with the STV_DEFAULT attribute is as specified by the symbol's binding type. That is, global and weak symbols are visible outside of their defining component, the executable file or shared object. Local symbols are hidden. Global and weak symbols can also be preempted, that is, they may by interposed by definitions of the same name in another component.

  • STV_PROTECTED A symbol defined in the current component is protected if it is visible in other components but cannot be preempted. Any reference to such a symbol from within the defining component must be resolved to the definition in that component, even if there is a definition in another component that would interpose by the default rules. A symbol with STB_LOCAL binding will not have STV_PROTECTED visibility.

  • STV_HIDDEN A symbol defined in the current component is hidden if its name is not visible to other components. Such a symbol is necessarily protected. This attribute is used to control the external interface of a component. An object named by such a symbol may still be referenced from another component if its address is passed outside.

    A hidden symbol contained in a relocatable object is either removed or converted to STB_LOCAL binding by the link-editor when the relocatable object is included in an executable file or shared object.

  • STV_INTERNAL This visibility attribute is currently reserved.

Quanto ao efeito sobre C e Assembly, o Oracle docs prossegue dizendo

None of the visibility attributes affects the resolution of symbols within an executable or shared object during link-editing. Such resolution is controlled by the binding type. Once the link-editor has chosen its resolution, these attributes impose two requirements. Both requirements are based on the fact that references in the code being linked may have been optimized to take advantage of the attributes.

  • First, all of the non-default visibility attributes, when applied to a symbol reference, imply that a definition to satisfy that reference must be provided within the current executable or shared object. If this type of symbol reference has no definition within the component being linked, then the reference must have STB_WEAK binding and is resolved to zero.

  • Second, if any reference to or definition of a name is a symbol with a non-default visibility attribute, the visibility attribute must be propagated to the resolving symbol in the linked object. If different visibility attributes are specified for distinct references to or definitions of a symbol, the most constraining visibility attribute must be propagated to the resolving symbol in the linked object. The attributes, ordered from least to most constraining, are STV_PROTECTED, STV_HIDDEN and STV_INTERNAL.

Veja também

por 02.10.2018 / 03:39