libobjc2 with build changes and Darling additions
Go to file
theraven 2e2856ce73 Build a static library from the Makefile.
Patch by Pete French.
2010-10-05 21:49:40 +00:00
GCKit Remove usage of -march=native since it doesn't work with most compilers, and 2010-09-08 09:12:13 +00:00
objc Fixed @synchronize() with a class that has had an instance used for @synchronize(). 2010-10-04 17:44:17 +00:00
opts Added missing files. 2010-05-19 14:16:26 +00:00
Test Fixed crash when resolving user-allocated classes (e.g. KVO) revealed by r31482. 2010-10-05 14:57:07 +00:00
toydispatch Tweaked the GNUmakefile to (more or less) work with gcc. 2010-10-01 13:11:48 +00:00
abi_version.c Rewrote ABI compatibility testing. Makes it easier to define new, incompatible ABIs in future without breaking everything. 2010-06-02 13:18:24 +00:00
ANNOUNCE Added tarball location to libobjc2 ANNOUNCE. 2010-10-04 15:44:08 +00:00
API Added property_getName(). Added non-portable API documentation. 2010-09-21 13:51:47 +00:00
blocks_runtime.m Tweaked the GNUmakefile to (more or less) work with gcc. 2010-10-01 13:11:48 +00:00
buffer.h Finished rewriting the loader. It's now cleanly separated into logically-separate components, so there's a chance it might actually be maintainable... 2010-06-02 22:25:12 +00:00
caps.c Add feature test APIs to libobjc2. 2010-09-08 14:02:03 +00:00
category_loader.c More tweaks to +load. Now defers +load a little bit more. 2010-07-20 10:53:53 +00:00
category.h Finished rewriting the loader. It's now cleanly separated into logically-separate components, so there's a chance it might actually be maintainable... 2010-06-02 22:25:12 +00:00
class_table.c Fix metametaclass setup. 2010-10-05 12:37:05 +00:00
class.h Corrected doc in few places to refer to @synchronized() and not @synchronize() 2010-10-04 19:01:16 +00:00
COPYING Removed last bits of legacy code from libobjc2. Added new exception handling implementation. 2010-06-29 21:46:20 +00:00
dtable.c Fixed @synchronize() with a class that has had an instance used for @synchronize(). 2010-10-04 17:44:17 +00:00
dtable.h Fixed @synchronize() with a class that has had an instance used for @synchronize(). 2010-10-04 17:44:17 +00:00
dwarf_eh.h Various tidies. 2010-09-09 10:43:42 +00:00
eh_personality.c Some source tidies. 2010-08-15 11:17:00 +00:00
encoding2.c Fix off-by-one error in encoding parsing. 2010-09-29 18:18:45 +00:00
GNUmakefile Tweaked the GNUmakefile to (more or less) work with gcc. 2010-10-01 13:11:48 +00:00
hash_table.c Added missing file. 2010-02-01 14:48:40 +00:00
hash_table.h Rewrote some of the class table code to be slightly cleaner. 2010-06-01 12:59:57 +00:00
hooks.c Include the correct headers in hooks.c. 2010-09-09 10:43:16 +00:00
ivar.c Various tidies. 2010-09-09 10:43:42 +00:00
ivar.h More refactoring. 2010-05-13 23:27:17 +00:00
legacy_malloc.c libobjc2: tweaks to build on Windows 2010-09-03 21:08:06 +00:00
loader.c Corrected doc in few places to refer to @synchronized() and not @synchronize() 2010-10-04 19:01:16 +00:00
loader.h Finished rewriting the loader. It's now cleanly separated into logically-separate components, so there's a chance it might actually be maintainable... 2010-06-02 22:25:12 +00:00
lock.h Rewrote some of the class table code to be slightly cleaner. 2010-06-01 12:59:57 +00:00
magic_objects.h Made magic object header use #define instead of const (gets rid of unused variable warnings). 2010-03-06 13:46:48 +00:00
Makefile Build a static library from the Makefile. 2010-10-05 21:49:40 +00:00
method_list.h Tidied up some bits by creating private headers for private data structures. 2010-05-13 22:29:56 +00:00
module.h Finished rewriting the loader. It's now cleanly separated into logically-separate components, so there's a chance it might actually be maintainable... 2010-06-02 22:25:12 +00:00
mutation.m Added new runtime library, based on GCC 4.4 libobjc, libobjc_tr and Objective2.framework. 2009-09-08 16:18:59 +00:00
NSBlocks.m Lots of tidying, removing legacy stuff. 2010-06-06 21:11:25 +00:00
pool.h Tidied up some bits by creating private headers for private data structures. 2010-05-13 22:29:56 +00:00
properties.h Finished rewriting the loader. It's now cleanly separated into logically-separate components, so there's a chance it might actually be maintainable... 2010-06-02 22:25:12 +00:00
properties.m Stylistic fixes 2010-09-26 19:39:39 +00:00
Protocol2.m Make sure both protocol classes are available before resolving protocols. 2010-07-19 14:13:51 +00:00
protocol.c libobjc2: Add a pile of NULL checks. In general, any public function should be able to handle NULL for any object/class/selector/pointer to opaque structure without blowing up. 2010-09-25 20:56:19 +00:00
protocol.h Added missing file. 2010-06-08 16:20:30 +00:00
README Corrected doc in few places to refer to @synchronized() and not @synchronize() 2010-10-04 19:01:16 +00:00
runtime.c Fixed crash when resolving user-allocated classes (e.g. KVO) revealed by r31482. 2010-10-05 14:57:07 +00:00
sarray2.c Lots of tidying, removing legacy stuff. 2010-06-06 21:11:25 +00:00
sarray2.h Finished rewrite of message sending. Deleted lots of legacy stuff. 2010-06-03 15:17:12 +00:00
selector_table.c Stylistic fixes 2010-09-26 19:39:39 +00:00
selector.h In some irritating circumstances, the load order can cause a +load message to trigger other code, which sends messages with selectors that have not been mapped yet. 2010-08-31 20:40:08 +00:00
sendmsg2.c undo accidentally commited stuff 2010-09-16 04:57:56 +00:00
slot_pool.h Finished rewrite of message sending. Deleted lots of legacy stuff. 2010-06-03 15:17:12 +00:00
statics_loader.c Finished rewriting the loader. It's now cleanly separated into logically-separate components, so there's a chance it might actually be maintainable... 2010-06-02 22:25:12 +00:00
string_hash.h Added support for type-dependent dispatch to libobjc2. 2010-06-29 12:42:20 +00:00
sync.m Properly resolve the metaclass's isa pointer. 2010-10-04 22:33:30 +00:00
type_encoding_cases.h Added file missing in last commit. 2010-06-04 23:40:30 +00:00
unwind.h Added missing header (unwind.h, from libunwind) 2010-09-29 18:15:37 +00:00

GNUstep Objective-C Runtime
===========================

The GNUstep Objective-C runtime is designed as a drop-in replacement for the
GCC runtime.  It supports both a legacy and a modern ABI, allowing code
compiled with old versions of GCC to be supported without requiring
recompilation.  The modern ABI adds the following features:

- Non-fragile instance variables.
- Protocol uniquing.
- Object planes support.
- Declared property introspection.

Both ABIs support the following feature above and beyond the GCC runtime:

- The modern Objective-C runtime APIs, introduced with OS X 10.5.
- Blocks (closures).
- Low memory profile for platforms where memory usage is more important than
  speed.
- Synthesised property accessors.
- Efficient support for @synchronized()
- Type-dependent dispatch, eliminating stack corruption from mismatched
  selectors.

History
-------

Early work on the GNUstep runtime combined code from the GCC Objective-C
runtime, the Étoilé Objective-C runtime, Remy Demarest's blocks runtime for OS
X 10.5, and the Étoilé Objective-C 2 API compatibility framework.  All of these
aside from the GCC runtime were MIT licensed, although the GPL'd code present
in the GCC runtime meant that the combined work had to remain under the GPL.

Since then, all of the GCC code has been removed, leaving the remaining files
all MIT licensed, and allowing the entire work to be MIT licensed.  

The exception handling code uses a header file implementing the generic parts
of the Itanium EH ABI.  This file comes from PathScale's libc++rt.  PathScale
kindly allowed it to be MIT licensed for inclusion here.

Non-Fragile Instance Variables
------------------------------

When a class is compiled to support non-fragile instance variables, the
instance_size field in the class is set to 0 - the size of the instance
variables declared on that class (excluding those inherited.  For example, an
NSObject subclass declaring an int ivar would have its instance_size set to 0 -
sizeof(int)).  The offsets of each instance variable in the class's ivar_list
field are then set to the offset from the start of the superclass's ivars.

When the class is loaded, the runtime library uses the size of the superclass
to calculate the correct size for this new class and the correct offsets.  Each
instance variable should have two other variables exported as global symbols.
Consider the following class:

@interface NewClass : SuperClass {
	int anIvar;
}
@end

This would have its instance_size initialized to 0-sizeof(int), and anIvar's
offset initialized to 0.  It should also export the following two symbols:

int __objc_ivar_offset_value_NewClass.anIvar;
int *__objc_ivar_offset_NewClass.anIvar;

The latter should point to the former or to the ivar_offset field in the ivar
metadata.  The former should be pointed to by the only element in the
ivar_offsets array in the class structure.  

In other compilation units referring to this ivar, the latter symbol should be
exported as a weak symbol pointing to an internal symbol containing the
compiler's guess at the ivar offset.  The ivar will then work as a fragile ivar
when NewClass is compiled with the old ABI.  If NewClass is compiled with the
new ABI, then the linker will replace the weak symbol with the version in the
class's compilation unit and references which use this offset will function
correctly.

If the compiler can guarantee that NewClass is compiled with the new ABI, for
example if it is declared in the same compilation unit, by finding the symbol
during a link-time optimization phase, or as a result of a command-line
argument, then it may use the __objc_ivar_offset_value_NewClass.anIvar symbol
as the ivar offset.  This eliminates the need for one load for every ivar
access.  

Protocols
---------

The runtime now provides a __ObjC_Protocol_Holder_Ugly_Hack class.  All
protocols that are referenced but not defined should be registered as
categories on this class.  This ensures that every protocol is registered with
the runtime.  

In the near future, the runtime will ensure that protocols can be looked up by
name at run time and that empty protocol definitions have their fields updated
to match the defined version.

Protocols have been extended to provide space for introspection on properties
and optional methods.  These fields only exist on protocols compiled with a
compiler that supports Objective-C 2.  To differentiate the two, the isa
pointer for new protocols will be set to the Protocol2 class.

Blocks
------

The GNUstep runtime provides the run time support required for Apple's blocks
(closures) extension to C.  

Fast Proxies and Cacheable Lookups
----------------------------------

The new runtime provides two mechanisms for faster lookup.  The older
Vobjc_msg_lookup() function, which returns an IMP, is still supported, however
it is no longer recommended.  The new lookup functions is:

Slot_t objc_msg_lookup_sender(id *receiver, SEL selector, id sender)

The receiver is passed by pointer, and so may be modified during the lookup
process.  The runtime itself will never modify the receiver.  The following
hook is provided to allow fast proxy support:

id (*objc_proxy_lookup)(id receiver, SEL op);

This function takes an object and selector as arguments and returns a new
objects.  The lookup will then be re-run and the final message should be sent to
the new object.

The returned Slot_t from the new lookup function is a pointer to a structure
which contains both an IMP and a version (among other things).  The version is
incremented every time the method is overridden, allowing this to be cached by
the caller.  User code wishing to perform IMP caching may use the old mechanism
if it can guarantee that the IMP will not change between calls, or the newer
mechanism.  Note that a modern compiler should insert caching automatically,
ideally with the aid of run-time profiling results.  To support this, a new hook
has been added:

Slot_t objc_msg_forward3(id receiver, SEL op);

This is identical to objc_msg_forward2(), but returns a pointer to a slot,
instead of an IMP.  The slot should have its version set to 0, to prevent
caching.

Object Planes
-------------

Object planes provide interception points for messages between groups of
related objects.  They can be thought of as similar to processes, with mediated
inter-plane communication.  A typical use-case for an object plane is to
automatically queue messages sent to a thread, or to record every message sent
to model objects.  Planes can dramatically reduce the number of proxy objects
required for this kind of activity.

The GNUstep runtime adds a flag to class objects indicating that their
instances are present in the global plane.  All constant strings, protocols,
and classes are in the global plane, and may therefore be sent and may receive
messages bypassing the normal plane interception mechanism.  

The runtime library does not provide direct support for planes, it merely
provides the core components required to implement support for planes in
another framework.  Two objects are regarded as being in the same plane when
they words immediately before their isa pointers are the same.  In this case,
the runtime's usual dispatch mechanisms will be used.  In all other cases, the
runtime will delegate message lookup to another library via the following hook:

Slot_t (*objc_plane_lookup)(id *receiver, SEL op, id sender);

From the perspective of the runtime, the plane identifier is opaque.  In
GNUstep, it is a pointer to an NSZone structure.

Threading
---------

The threading APIs from GCC libobjc are not present in this runtime.  They were
buggy, badly supported, inadequately tested, and irrelevant now that there are
well tested thread abstraction layers, such as the POSIX thread APIs and the
C1x thread functions.  The library always runs in thread-safe mode.  The same
functions for locking the runtime mutex are still supported, but their use any
mutex not exported by the runtime library is explicitly not supported.  The
(private) lock.h header is used to abstract the details of different threading
systems sufficiently for the runtime.  This provides mechanisms for locking,
unlocking, creating, and destroying mutex objects.  

Type-Dependent Dispatch
-----------------------

Traditionally, Objective-C method lookup is done entirely on the name of the
method.  This is problematic when the sender and receiver of the method
disagree on the types of a method.  

For example, consider a trivial case where you have two methods with the same
name, one taking an integer, the other taking a floating point value.  Both
will pass their argument in a register on most platforms, but not the same
register.  If the sender thinks it is calling one, but is really calling the
other, then the receiver will look in the wrong register and use a nonsense
value.  The compiler will often not warn about this.

This is a relatively benign example, but if the mismatch is between methods
taking or returning a structure and those only using scalar arguments and
return then the call frame layout will be so different that the result will be
stack corruption, possibly leading to security holes.

If you compile the GNUstep runtime with type-dependent dispatch enabled, then
sending a message with a typed selector will only ever invoke a method with the
same types.  Sending a message with an untyped selector will invoke any method
with a matching name, although the slot returned from the lookup function will
contain the types, allowing the caller to check them and construct a valid call
frame, if required.

If a lookup with a typed selector matches a method with the wrong types, the
runtime will call a handler.  This handler, by default, prints a helpful
message and exits.  LanguageKit provides an alternative version which
dynamically generates a new method performing the required boxing and calling
the original.

Low Memory Profile
------------------

The dispatch tables for each class, in spite of using a more space-efficient
sparse array implementation than GCC libobjc, can still use quite a lot of
memory.  The NeXT runtime avoided this problem by not providing dispatch tables
at all.  Instead, it did a linear search of the method lists, caching a few
results.  Although this is O(n), it performed reasonably well.  Most message
sends are to a small number of methods.  For example, an NSMutableDictionary is
most often sent -objectForKey: and -setObject:forKey:.  If these two methods
are in the cache, then the O(n) algorithm is almost never used.

If you compile the GNUstep runtime with the low memory profile, it uses a
similar strategy.  The caches use the same slot-caching mechanism described
above and can be combined with caching at the call site.  The runtime will not
create dispatch tables, which can save a few MB of RAM in projects with a lot
of classes, but can make message sending a lot slower in the worst case.

Objective-C 2 Features
----------------------

The runtime now provides implementations of the functions required for the
@synchronized directive, for property accessors, and for fast enumeration.  The
public runtime function interfaces now match those of OS X.