objc-file-old.mm 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /*
  2. * Copyright (c) 1999-2007 Apple Inc. All Rights Reserved.
  3. *
  4. * @APPLE_LICENSE_HEADER_START@
  5. *
  6. * This file contains Original Code and/or Modifications of Original Code
  7. * as defined in and that are subject to the Apple Public Source License
  8. * Version 2.0 (the 'License'). You may not use this file except in
  9. * compliance with the License. Please obtain a copy of the License at
  10. * http://www.opensource.apple.com/apsl/ and read it before using this
  11. * file.
  12. *
  13. * The Original Code and all software distributed under the License are
  14. * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
  15. * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
  16. * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
  18. * Please see the License for the specific language governing rights and
  19. * limitations under the License.
  20. *
  21. * @APPLE_LICENSE_HEADER_END@
  22. */
  23. // Copyright 1988-1996 NeXT Software, Inc.
  24. #if !__OBJC2__
  25. #include "objc-private.h"
  26. #include "objc-runtime-old.h"
  27. #include "objc-file-old.h"
  28. #if TARGET_OS_WIN32
  29. /*
  30. Module
  31. _getObjcModules(const header_info *hi, size_t *nmodules)
  32. {
  33. if (nmodules) *nmodules = hi->moduleCount;
  34. return hi->modules;
  35. }
  36. */
  37. SEL *
  38. _getObjcSelectorRefs(const header_info *hi, size_t *nmess)
  39. {
  40. if (nmess) *nmess = hi->selrefCount;
  41. return hi->selrefs;
  42. }
  43. struct old_protocol **
  44. _getObjcProtocols(const header_info *hi, size_t *nprotos)
  45. {
  46. if (nprotos) *nprotos = hi->protocolCount;
  47. return hi->protocols;
  48. }
  49. Class*
  50. _getObjcClassRefs(const header_info *hi, size_t *nclasses)
  51. {
  52. if (nclasses) *nclasses = hi->clsrefCount;
  53. return (Class*)hi->clsrefs;
  54. }
  55. // __OBJC,__class_names section only emitted by CodeWarrior rdar://4951638
  56. const char *
  57. _getObjcClassNames(const header_info *hi, size_t *size)
  58. {
  59. if (size) *size = 0;
  60. return NULL;
  61. }
  62. #else
  63. #define GETSECT(name, type, segname, sectname) \
  64. type *name(const headerType *mhdr, size_t *outCount) \
  65. { \
  66. unsigned long byteCount = 0; \
  67. type *data = (type *) \
  68. getsectiondata(mhdr, segname, sectname, &byteCount); \
  69. *outCount = byteCount / sizeof(type); \
  70. return data; \
  71. } \
  72. type *name(const header_info *hi, size_t *outCount) \
  73. { \
  74. return name(hi->mhdr(), outCount); \
  75. }
  76. GETSECT(_getObjcModules, objc_module, "__OBJC", "__module_info");
  77. GETSECT(_getObjcSelectorRefs, SEL, "__OBJC", "__message_refs");
  78. GETSECT(_getObjcClassRefs, Class, "__OBJC", "__cls_refs");
  79. GETSECT(_getObjcClassNames, const char, "__OBJC", "__class_names");
  80. // __OBJC,__class_names section only emitted by CodeWarrior rdar://4951638
  81. GETSECT(getLibobjcInitializers, UnsignedInitializer, "__DATA", "__objc_init_func");
  82. objc_image_info *
  83. _getObjcImageInfo(const headerType *mhdr, size_t *outBytes)
  84. {
  85. unsigned long byteCount = 0;
  86. objc_image_info *info = (objc_image_info *)
  87. getsectiondata(mhdr, SEG_OBJC, "__image_info", &byteCount);
  88. *outBytes = byteCount;
  89. return info;
  90. }
  91. struct old_protocol **
  92. _getObjcProtocols(const header_info *hi, size_t *nprotos)
  93. {
  94. unsigned long size = 0;
  95. struct old_protocol *protos = (struct old_protocol *)
  96. getsectiondata(hi->mhdr(), SEG_OBJC, "__protocol", &size);
  97. *nprotos = size / sizeof(struct old_protocol);
  98. if (!hi->proto_refs && *nprotos) {
  99. size_t i;
  100. header_info *whi = (header_info *)hi;
  101. whi->proto_refs = (struct old_protocol **)
  102. malloc(*nprotos * sizeof(*hi->proto_refs));
  103. for (i = 0; i < *nprotos; i++) {
  104. hi->proto_refs[i] = protos+i;
  105. }
  106. }
  107. return hi->proto_refs;
  108. }
  109. static const segmentType *
  110. getsegbynamefromheader(const headerType *head, const char *segname)
  111. {
  112. const segmentType *sgp;
  113. unsigned long i;
  114. sgp = (const segmentType *) (head + 1);
  115. for (i = 0; i < head->ncmds; i++){
  116. if (sgp->cmd == SEGMENT_CMD) {
  117. if (strncmp(sgp->segname, segname, sizeof(sgp->segname)) == 0) {
  118. return sgp;
  119. }
  120. }
  121. sgp = (const segmentType *)((char *)sgp + sgp->cmdsize);
  122. }
  123. return NULL;
  124. }
  125. bool
  126. _hasObjcContents(const header_info *hi)
  127. {
  128. // Look for an __OBJC,* section other than __OBJC,__image_info
  129. const segmentType *seg = getsegbynamefromheader(hi->mhdr(), "__OBJC");
  130. const sectionType *sect;
  131. uint32_t i;
  132. for (i = 0; i < seg->nsects; i++) {
  133. sect = ((const sectionType *)(seg+1))+i;
  134. if (0 != strncmp(sect->sectname, "__image_info", 12)) {
  135. return YES;
  136. }
  137. }
  138. return NO;
  139. }
  140. #endif
  141. #endif