71 </codeblock> <p>Compile this with <i>armcc -S eabi_thunk_offset_problem.cpp</i> to get an assembly listing. Compile it again with an extra argument "-DCOUNT=2" to change the size of the base class, and compare the two files: there will be various differences in the code, but also differences in the _ZTh symbols - including the differences used in the "typical symptom" above. </p> <p>If you use virtual inheritance, then you may see another version of the problem. With virtual inheritance, there are two offsets involved and the thunk symbols will begin with _ZTv. The same symbol may appear in several thunks, each with different offsets. </p> </section> <section id="GUID-E0F45E5F-F254-5D8B-A8E9-9D22C59DF2E4"><title>The Shared DEF File Problem</title> <p><b>What is the Problem with Shared DEF Files?</b> </p> <p>The class exporting rules (see <xref href="GUID-CCE5DBCC-41D6-53D0-B929-ADB478B53F12.dita#GUID-CCE5DBCC-41D6-53D0-B929-ADB478B53F12/GUID-A6382EAB-EEF5-56AC-B15E-B3740BB3C102">Class Exporting Rules</xref>) by default will generate non-callable exports for all classes that are not marked non-sharable in source. Say two DLLs, A and B share one DEF file, in effect implementing similar but different functionality towards the same public interface. Further say, no classes are marked non-sharable. Say there are some classes that are shared between DLL A and DLL B and that these classes have names of the form CShared<xyz>. Classes that are specific to DLL A have names of the form CA<xyz>, classes specific to DLL B have names of the form CB<xyz>. When DLL A is built, DEF file entries for non-callable exports from CShared<xyz> and CA<XYZ> are automatically added to the DEF file. When DLL B is built, exports from CShared<xyz> and CB<XYZ> are added. So in fact the DEF file would be the sum of all non-callable exports from CShared<xyz>, CA<xyz> and CB<XYZ>. It also will contain symbols from functions that are marked for export using EXPORT_C. However, this means that neither A or B can be linked. This is because when A is built, the code linking against the non-callable exports of CB<XYZ> do not exist in A and vice versa. </p> <fig id="GUID-E2B571A3-0463-5886-AD08-D50F0420EAF9"><image href="GUID-D205622E-FB0F-59C4-A039-B418B551CBFA_d0e9285_href.png" placement="inline"/></fig> <p><b>Use-cases for Fixing Shared DEF Files</b> </p> <p> <b>Use-Case 1:</b> <b> Polymorphic “Plug-ins”</b> </p> <p>Several DLLs are built using the same DLL interface (DEF file). Typically the DEF file has very few entries (1 or 2) and is maintained manually. This means that new functions are added by editing the shared DEF file. Also typically no import libraries are needed as the knowledge about the DLL interface is hard-coded into the client code of the "plug-in". The plug-ins do not have to be loaded at run-time. Some are always built but not always included in the ROM. </p> <p> <b>The Fix:</b> </p> <ol id="GUID-A5D72F88-AF26-57FB-823F-7331992EB0F8"><li id="GUID-DE2A9DBD-EF23-56AA-997A-90B717406DBB"><p>If the shared DEF file is in \epoc32\include\def\EABI then locate the original DEF file by searching all BLD.INF files for the appropriate line in PRJ_EXPORTS </p> </li> <li id="GUID-1BE3AEA4-B629-5273-A370-DC3A9DABBA80"><p>Remove all non-callable exports that have caused warnings or errors from the original DEF file. </p> </li> <li id="GUID-CADA2F46-96C3-50DD-99AE-847D2EC70CFE"><p>Add NOEXPORTLIBRARY to all MMP files that share that component, ensuring that the build system does NOT try and re-freeze these automatically the next time you build. Otherwise the build system will re-introduce these non-callable exports. </p> </li> </ol> <p> <b>Note:</b> </p> <p>If you want to use the re-freeze mechanism – say to add a new export, you have to temporarily remove NOEXPORTLIBRARY from the MMP file, then generate a new DEF file by re-building the component, re-freeze, possibly edit (to remove unwanted non-callable exports) and then insert the keyword NOEXPORTLIBRARY into the MMP file again. </p> <p> <b> Use-Case 2:</b> <b> Polymorphic “Plug-ins” on which Other Components Depend</b> </p> <p>This is very similar to use-case 1, except that some other component depends on one of the plug-ins. This means that an import library is required. </p> <p> <b>The Fix: </b> The build structure must be such that </p> <ul><li id="GUID-9B61065B-E448-5B32-910C-4F8356B30E63"><p>One MMP file generates the import library from the shared DEF file using the target type IMPLIB. It may be necessary to create a new MMP file which does this. </p> </li> <li id="GUID-4CB6CB0F-E690-5AAB-8B0C-1A6D64ECBC31"><p>All the other MMP files use NOEXPORTLIBRARY as described in use-case 1 </p> </li> </ul> <p> <b>Note:</b> </p> <p>If you want to use the re-freeze mechanism – say to add a new export, you have to temporarily remove NOEXPORTLIBRARY from the MMP file, then generate a new DEF file by re-building the component, re-freeze, possibly edit (to remove unwanted non-callable exports) and then insert the keyword NOEXPORTLIBRARY into the MMP file again. </p> <p> <b>Use-Case 3:</b> <b> Annotate Classes as Non-sharable</b> </p> <p>Where a DEF file must be shared between components for whatever reason and none of the above use-cases can be applied, the build would fail for at least one component. An example of this may be a class MyPrivateClass that exists in the debug build (UDEV) of the OS, but not in the release build (UREL). </p> <p>In such a case all classes that should not contribute to the DEF file, i.e. that are really private to the implementation of a component, must be annotated in the source as NONSHARABLE_CLASS(X) or NONSHARABLE_STRUCT(X). As a result no non-callable exports will be generated for such a class. Say for example, class MyPrivateClass is truly private to a component that must share a DEF file with another component. Then it should be declared: </p> <codeblock id="GUID-8E236162-F6BA-5A77-9A50-8546C49A7FFC" xml:space="preserve"> NONSHARABLE_CLASS(MyPrivateClass) |
71 </codeblock> <p>Compile this with <i>armcc -S eabi_thunk_offset_problem.cpp</i> to get an assembly listing. Compile it again with an extra argument "-DCOUNT=2" to change the size of the base class, and compare the two files: there will be various differences in the code, but also differences in the _ZTh symbols - including the differences used in the "typical symptom" above. </p> <p>If you use virtual inheritance, then you may see another version of the problem. With virtual inheritance, there are two offsets involved and the thunk symbols will begin with _ZTv. The same symbol may appear in several thunks, each with different offsets. </p> </section> <section id="GUID-E0F45E5F-F254-5D8B-A8E9-9D22C59DF2E4"><title>The Shared DEF File Problem</title> <p><b>What is the Problem with Shared DEF Files?</b> </p> <p>The class exporting rules (see <xref href="GUID-CCE5DBCC-41D6-53D0-B929-ADB478B53F12.dita#GUID-CCE5DBCC-41D6-53D0-B929-ADB478B53F12/GUID-A6382EAB-EEF5-56AC-B15E-B3740BB3C102">Class Exporting Rules</xref>) by default will generate non-callable exports for all classes that are not marked non-sharable in source. Say two DLLs, A and B share one DEF file, in effect implementing similar but different functionality towards the same public interface. Further say, no classes are marked non-sharable. Say there are some classes that are shared between DLL A and DLL B and that these classes have names of the form CShared<xyz>. Classes that are specific to DLL A have names of the form CA<xyz>, classes specific to DLL B have names of the form CB<xyz>. When DLL A is built, DEF file entries for non-callable exports from CShared<xyz> and CA<XYZ> are automatically added to the DEF file. When DLL B is built, exports from CShared<xyz> and CB<XYZ> are added. So in fact the DEF file would be the sum of all non-callable exports from CShared<xyz>, CA<xyz> and CB<XYZ>. It also will contain symbols from functions that are marked for export using EXPORT_C. However, this means that neither A or B can be linked. This is because when A is built, the code linking against the non-callable exports of CB<XYZ> do not exist in A and vice versa. </p> <fig id="GUID-E2B571A3-0463-5886-AD08-D50F0420EAF9"><image href="GUID-D205622E-FB0F-59C4-A039-B418B551CBFA_d0e10560_href.png" placement="inline"/></fig> <p><b>Use-cases for Fixing Shared DEF Files</b> </p> <p> <b>Use-Case 1:</b> <b> Polymorphic “Plug-ins”</b> </p> <p>Several DLLs are built using the same DLL interface (DEF file). Typically the DEF file has very few entries (1 or 2) and is maintained manually. This means that new functions are added by editing the shared DEF file. Also typically no import libraries are needed as the knowledge about the DLL interface is hard-coded into the client code of the "plug-in". The plug-ins do not have to be loaded at run-time. Some are always built but not always included in the ROM. </p> <p> <b>The Fix:</b> </p> <ol id="GUID-A5D72F88-AF26-57FB-823F-7331992EB0F8"><li id="GUID-DE2A9DBD-EF23-56AA-997A-90B717406DBB"><p>If the shared DEF file is in \epoc32\include\def\EABI then locate the original DEF file by searching all BLD.INF files for the appropriate line in PRJ_EXPORTS </p> </li> <li id="GUID-1BE3AEA4-B629-5273-A370-DC3A9DABBA80"><p>Remove all non-callable exports that have caused warnings or errors from the original DEF file. </p> </li> <li id="GUID-CADA2F46-96C3-50DD-99AE-847D2EC70CFE"><p>Add NOEXPORTLIBRARY to all MMP files that share that component, ensuring that the build system does NOT try and re-freeze these automatically the next time you build. Otherwise the build system will re-introduce these non-callable exports. </p> </li> </ol> <p> <b>Note:</b> </p> <p>If you want to use the re-freeze mechanism – say to add a new export, you have to temporarily remove NOEXPORTLIBRARY from the MMP file, then generate a new DEF file by re-building the component, re-freeze, possibly edit (to remove unwanted non-callable exports) and then insert the keyword NOEXPORTLIBRARY into the MMP file again. </p> <p> <b> Use-Case 2:</b> <b> Polymorphic “Plug-ins” on which Other Components Depend</b> </p> <p>This is very similar to use-case 1, except that some other component depends on one of the plug-ins. This means that an import library is required. </p> <p> <b>The Fix: </b> The build structure must be such that </p> <ul><li id="GUID-9B61065B-E448-5B32-910C-4F8356B30E63"><p>One MMP file generates the import library from the shared DEF file using the target type IMPLIB. It may be necessary to create a new MMP file which does this. </p> </li> <li id="GUID-4CB6CB0F-E690-5AAB-8B0C-1A6D64ECBC31"><p>All the other MMP files use NOEXPORTLIBRARY as described in use-case 1 </p> </li> </ul> <p> <b>Note:</b> </p> <p>If you want to use the re-freeze mechanism – say to add a new export, you have to temporarily remove NOEXPORTLIBRARY from the MMP file, then generate a new DEF file by re-building the component, re-freeze, possibly edit (to remove unwanted non-callable exports) and then insert the keyword NOEXPORTLIBRARY into the MMP file again. </p> <p> <b>Use-Case 3:</b> <b> Annotate Classes as Non-sharable</b> </p> <p>Where a DEF file must be shared between components for whatever reason and none of the above use-cases can be applied, the build would fail for at least one component. An example of this may be a class MyPrivateClass that exists in the debug build (UDEV) of the OS, but not in the release build (UREL). </p> <p>In such a case all classes that should not contribute to the DEF file, i.e. that are really private to the implementation of a component, must be annotated in the source as NONSHARABLE_CLASS(X) or NONSHARABLE_STRUCT(X). As a result no non-callable exports will be generated for such a class. Say for example, class MyPrivateClass is truly private to a component that must share a DEF file with another component. Then it should be declared: </p> <codeblock id="GUID-8E236162-F6BA-5A77-9A50-8546C49A7FFC" xml:space="preserve"> NONSHARABLE_CLASS(MyPrivateClass) |
74 };</codeblock> <p>This will prevent the compiler from exporting non-callables for MyPrivateClass. However this means that it is not possible to DLL-derive (for the definition of DLL-derive see <xref href="GUID-CCE5DBCC-41D6-53D0-B929-ADB478B53F12.dita#GUID-CCE5DBCC-41D6-53D0-B929-ADB478B53F12/GUID-884B1DF5-ECC8-5243-85B4-9B07BA52C58B">Terminology and Background</xref>) from MyPrivateClass and that all classes derived from MyPrivateClass must also be marked non-sharable (see <xref href="GUID-CCE5DBCC-41D6-53D0-B929-ADB478B53F12.dita#GUID-CCE5DBCC-41D6-53D0-B929-ADB478B53F12/GUID-884B1DF5-ECC8-5243-85B4-9B07BA52C58B">Terminology and Background</xref>). </p> <p> <b>Use-Case 4:</b> <b> Optimisation</b> </p> <p>A consequence of the Simple Rule (see <xref href="GUID-CCE5DBCC-41D6-53D0-B929-ADB478B53F12.dita#GUID-CCE5DBCC-41D6-53D0-B929-ADB478B53F12/GUID-3DA52D31-E3D7-5061-8D15-1F1D69AE2ED1">The Simple Rule – Sharable Classes</xref>) is that some components may emit entries in their DEF files which are not needed. In the worst case the overhead is 8 bytes of ROM size per class (2 DLL entry points). In the typical case an increase of 4 bytes will occur and in some cases no increase at all. </p> <p>For code that is private to an implementation, i.e. where it is known that a class would never be used outside of that component, this footprint increase is unnecessary. In order to avoid the footprint increase mark all private classes (and classes derived from them) in the source as NONSHARABLE_CLASS(X) or NONSHARABLE_STRUCT(X) as described for use-case 3. </p> <p> <b> Use-Case 5:</b> <b> The Build Tools Automatically Ignore Non-callable Exports</b> </p> <p>For most components the build tools automatically ignore <b>all</b> non-callable exports. This is the case because the build tools know the situations when non-callable exports cannot be needed. Non-callable exports are only needed, if: </p> <ul><li id="GUID-BF53F4C7-14B3-5FEB-8CAE-F71383DFAB2E"><p>The target type is either DLL, EXEDLL or EXEXP and the MMP file has no NOEXPORTLIBRARY keyword </p> </li> <li id="GUID-622E7A6F-0618-5331-9AE8-E2ABDD7FA190"><p>If the MMP file contains the DEFFILE keyword and the MMP file has no NOEXPORTLIBRARY keyword </p> </li> </ul> <p>The reason for this is that target types, such as APP, always map directly onto one of the above use-cases. For example the target type APP is an example of use-case 1, i.e. the APP is a DLL that always has the same DEF file. However no other DLL but the APP loader will ever link against this binary, unless its MMP file contains the DEFFILE keyword. </p> <p> <b>Use-Case 6:</b> <b> Best Practice</b> </p> <p>Note that it is good practice to avoid unnecessary footprint increases by marking private classes as non-sharable as outlined in use-case 4. Further note, that at some point in the future Symbian may add this to the Symbian Coding Standards or withdraw tools support for some of the cases described above. </p> <p><b>Optimisation</b> </p> <p>This section discusses advantages of marking “private” classes as described in use-cases 4 and 6 in the previous section as non-sharable </p> <p>Reasons for Optimisation: </p> <ul><li id="GUID-50EBEA63-092D-5B01-8371-890947D05E57"><p>Small footprint saving </p> </li> <li id="GUID-509DD311-4E9E-5A32-8194-25F9DA554417"><p>DEF files are more “pretty”, i.e. they will have fewer entries for non-callable exports in it and may have fewer holes in them. </p> </li> <li id="GUID-7DD701A6-E55A-5ECD-B905-416281014E4D"><p>When changing code that is private to a module as described in use-case 4 and not marked non-sharable, DEF file changes are required when: </p> <ul><li id="GUID-CA989C12-3138-53F3-A5C0-7092A31E9867"><p>Renaming a private class </p> </li> <li id="GUID-835872C3-C047-57F8-9221-E356B2D09A5A"><p>Removing a private class </p> </li> <li id="GUID-20632617-A0DB-5DF5-9C39-EF14A39CC8DF"><p>Adding a private class </p> </li> </ul> <p>This makes it harder to maintain DEF files and will ultimately lead to less “pretty” DEF files when Binary Compatibility must be maintained. </p> </li> <li id="GUID-7B1CAE8B-3CB9-5782-AE6E-7B4AE2E2307E"><p>More non-sharable classes mean that it is less likely to have problems with shared DEF files as outlined in use-cases 1 and 2 in the previous section. </p> </li> </ul> <p> <b>When not to use Optimisation:</b> </p> <p>It is not advisable to use optimisation in the following circumstances, as the build tools suppress non-callable exports automatically in these cases: </p> <ul><li id="GUID-0CD3582F-3976-599D-B1F1-A16C8025E50E"><p>The target type of the component containing private classes is neither DLL, EXEDLL nor EXEXP and no DEFFILE keyword is contained the components MMP file </p> </li> <li id="GUID-37D22C06-234B-57D8-9605-343E28EBB674"><p>The MMP file of the component contains the NOEXPORTLIBRARY keyword. </p> <p>If the NOEXPORTLIBRARY keyword has been introduced to work around problems introduced by shared DEF files and the Simple Rule it may be better to remove the NOEXPORTLIBRARY keyword and mark private classes as non-sharable instead. </p> </li> </ul> </section> </conbody></concept> |
74 };</codeblock> <p>This will prevent the compiler from exporting non-callables for MyPrivateClass. However this means that it is not possible to DLL-derive (for the definition of DLL-derive see <xref href="GUID-CCE5DBCC-41D6-53D0-B929-ADB478B53F12.dita#GUID-CCE5DBCC-41D6-53D0-B929-ADB478B53F12/GUID-884B1DF5-ECC8-5243-85B4-9B07BA52C58B">Terminology and Background</xref>) from MyPrivateClass and that all classes derived from MyPrivateClass must also be marked non-sharable (see <xref href="GUID-CCE5DBCC-41D6-53D0-B929-ADB478B53F12.dita#GUID-CCE5DBCC-41D6-53D0-B929-ADB478B53F12/GUID-884B1DF5-ECC8-5243-85B4-9B07BA52C58B">Terminology and Background</xref>). </p> <p> <b>Use-Case 4:</b> <b> Optimisation</b> </p> <p>A consequence of the Simple Rule (see <xref href="GUID-CCE5DBCC-41D6-53D0-B929-ADB478B53F12.dita#GUID-CCE5DBCC-41D6-53D0-B929-ADB478B53F12/GUID-3DA52D31-E3D7-5061-8D15-1F1D69AE2ED1">The Simple Rule – Sharable Classes</xref>) is that some components may emit entries in their DEF files which are not needed. In the worst case the overhead is 8 bytes of ROM size per class (2 DLL entry points). In the typical case an increase of 4 bytes will occur and in some cases no increase at all. </p> <p>For code that is private to an implementation, i.e. where it is known that a class would never be used outside of that component, this footprint increase is unnecessary. In order to avoid the footprint increase mark all private classes (and classes derived from them) in the source as NONSHARABLE_CLASS(X) or NONSHARABLE_STRUCT(X) as described for use-case 3. </p> <p> <b> Use-Case 5:</b> <b> The Build Tools Automatically Ignore Non-callable Exports</b> </p> <p>For most components the build tools automatically ignore <b>all</b> non-callable exports. This is the case because the build tools know the situations when non-callable exports cannot be needed. Non-callable exports are only needed, if: </p> <ul><li id="GUID-BF53F4C7-14B3-5FEB-8CAE-F71383DFAB2E"><p>The target type is either DLL, EXEDLL or EXEXP and the MMP file has no NOEXPORTLIBRARY keyword </p> </li> <li id="GUID-622E7A6F-0618-5331-9AE8-E2ABDD7FA190"><p>If the MMP file contains the DEFFILE keyword and the MMP file has no NOEXPORTLIBRARY keyword </p> </li> </ul> <p>The reason for this is that target types, such as APP, always map directly onto one of the above use-cases. For example the target type APP is an example of use-case 1, i.e. the APP is a DLL that always has the same DEF file. However no other DLL but the APP loader will ever link against this binary, unless its MMP file contains the DEFFILE keyword. </p> <p> <b>Use-Case 6:</b> <b> Best Practice</b> </p> <p>Note that it is good practice to avoid unnecessary footprint increases by marking private classes as non-sharable as outlined in use-case 4. Further note, that at some point in the future Symbian may add this to the Symbian Coding Standards or withdraw tools support for some of the cases described above. </p> <p><b>Optimisation</b> </p> <p>This section discusses advantages of marking “private” classes as described in use-cases 4 and 6 in the previous section as non-sharable </p> <p>Reasons for Optimisation: </p> <ul><li id="GUID-50EBEA63-092D-5B01-8371-890947D05E57"><p>Small footprint saving </p> </li> <li id="GUID-509DD311-4E9E-5A32-8194-25F9DA554417"><p>DEF files are more “pretty”, i.e. they will have fewer entries for non-callable exports in it and may have fewer holes in them. </p> </li> <li id="GUID-7DD701A6-E55A-5ECD-B905-416281014E4D"><p>When changing code that is private to a module as described in use-case 4 and not marked non-sharable, DEF file changes are required when: </p> <ul><li id="GUID-CA989C12-3138-53F3-A5C0-7092A31E9867"><p>Renaming a private class </p> </li> <li id="GUID-835872C3-C047-57F8-9221-E356B2D09A5A"><p>Removing a private class </p> </li> <li id="GUID-20632617-A0DB-5DF5-9C39-EF14A39CC8DF"><p>Adding a private class </p> </li> </ul> <p>This makes it harder to maintain DEF files and will ultimately lead to less “pretty” DEF files when Binary Compatibility must be maintained. </p> </li> <li id="GUID-7B1CAE8B-3CB9-5782-AE6E-7B4AE2E2307E"><p>More non-sharable classes mean that it is less likely to have problems with shared DEF files as outlined in use-cases 1 and 2 in the previous section. </p> </li> </ul> <p> <b>When not to use Optimisation:</b> </p> <p>It is not advisable to use optimisation in the following circumstances, as the build tools suppress non-callable exports automatically in these cases: </p> <ul><li id="GUID-0CD3582F-3976-599D-B1F1-A16C8025E50E"><p>The target type of the component containing private classes is neither DLL, EXEDLL nor EXEXP and no DEFFILE keyword is contained the components MMP file </p> </li> <li id="GUID-37D22C06-234B-57D8-9605-343E28EBB674"><p>The MMP file of the component contains the NOEXPORTLIBRARY keyword. </p> <p>If the NOEXPORTLIBRARY keyword has been introduced to work around problems introduced by shared DEF files and the Simple Rule it may be better to remove the NOEXPORTLIBRARY keyword and mark private classes as non-sharable instead. </p> </li> </ul> </section> </conbody></concept> |