Exemple #1
0
Elf *
elf_begin(int fd, Elf_Cmd c, Elf *a)
{
	Elf *e;

	e = NULL;

	if (LIBELF_PRIVATE(version) == EV_NONE) {
		LIBELF_SET_ERROR(SEQUENCE, 0);
		return (NULL);
	}

	switch (c) {
	case ELF_C_NULL:
		return (NULL);

	case ELF_C_WRITE:
		/*
		 * The ELF_C_WRITE command is required to ignore the
		 * descriptor passed in.
		 */
		a = NULL;
		break;

	case ELF_C_RDWR:
		if (a != NULL) { /* not allowed for ar(1) archives. */
			LIBELF_SET_ERROR(ARGUMENT, 0);
			return (NULL);
		}
		/*FALLTHROUGH*/
	case ELF_C_READ:
		/*
		 * Descriptor `a' could be for a regular ELF file, or
		 * for an ar(1) archive.  If descriptor `a' was opened
		 * using a valid file descriptor, we need to check if
		 * the passed in `fd' value matches the original one.
		 */
		if (a &&
		    ((a->e_fd != -1 && a->e_fd != fd) || c != a->e_cmd)) {
			LIBELF_SET_ERROR(ARGUMENT, 0);
			return (NULL);
		}
		break;

	default:
		LIBELF_SET_ERROR(ARGUMENT, 0);
		return (NULL);

	}

	if (a == NULL)
		e = _libelf_open_object(fd, c);
	else if (a->e_kind == ELF_K_AR)
		e = _libelf_ar_open_member(a->e_fd, c, a);
	else
		(e = a)->e_activations++;

	return (e);
}
Exemple #2
0
Elf *
elf_begin(int fd, Elf_Cmd c, Elf *a)
{
        Elf *e;

        e = NULL;

        if (LIBELF_PRIVATE(version) == EV_NONE) {
                LIBELF_SET_ERROR(SEQUENCE, 0);
                return (NULL);
        }

        switch (c) {
        case ELF_C_NULL:
                return (NULL);

        case ELF_C_WRITE:

                if (a != NULL) { /* not allowed for ar(1) archives. */
                        LIBELF_SET_ERROR(ARGUMENT, 0);
                        return (NULL);
                }

                /*
                 * Check writeability of `fd' immediately and fail if
                 * not writeable.
                 */
                if (ftruncate(fd, (off_t) 0) < 0) {
                        LIBELF_SET_ERROR(IO, errno);
                        return (NULL);
                }

                if ((e = _libelf_allocate_elf()) != NULL) {
                        _libelf_init_elf(e, ELF_K_ELF);
                        e->e_byteorder = LIBELF_PRIVATE(byteorder);
                        e->e_fd = fd;
                        e->e_cmd = c;
                }
                return (e);

        case ELF_C_RDWR:
                if (a != NULL) { /* not allowed for ar(1) archives. */
                        LIBELF_SET_ERROR(ARGUMENT, 0);
                        return (NULL);
                }
                /*FALLTHROUGH*/
        case ELF_C_READ:
                /*
                 * Descriptor `a' could be for a regular ELF file, or
                 * for an ar(1) archive.
                 */
                if (a && (a->e_fd != fd || c != a->e_cmd)) {
                        LIBELF_SET_ERROR(ARGUMENT, 0);
                        return (NULL);
                }

                break;

        default:
                LIBELF_SET_ERROR(ARGUMENT, 0);
                return (NULL);

        }

        if (a == NULL)
                e = _libelf_open_object(fd, c);
        else if (a->e_kind == ELF_K_AR)
                e = _libelf_ar_open_member(fd, c, a);
        else
                (e = a)->e_activations++;

        return (e);
}